22
33import com .codahale .metrics .MetricRegistry ;
44import com .fasterxml .jackson .databind .ObjectMapper ;
5+ import com .fasterxml .jackson .datatype .joda .JodaModule ;
56import io .dropwizard .util .Duration ;
6- import lombok .extern .java . Log ;
7+ import lombok .extern .slf4j . Slf4j ;
78import org .apache .commons .io .FileUtils ;
89import org .bson .types .ObjectId ;
910import org .joda .time .DateTime ;
2021/**
2122 * Created by tburch on 8/16/17.
2223 */
23- @ Log
24+ @ Slf4j
2425public class TryBlobCleanupJob {
26+ private final Duration blobTtl = Duration .days (1 );
27+
28+ private File tempDir ;
29+ private FileSystemJsonBlobManager blobManager ;
2530
26- private static final File TEMP ;
27- static {
31+ @ Before
32+ public void initBlobManage () {
2833 File temp = FileUtils .getTempDirectory ();
2934 File dir = new File (temp , UUID .randomUUID ().toString ());
3035 dir .deleteOnExit ();
31- TEMP = dir ;
36+ this .tempDir = dir ;
37+
38+ ObjectMapper objectMapper = new ObjectMapper ();
39+ objectMapper .registerModule (new JodaModule ());
40+ this .blobManager = new FileSystemJsonBlobManager (tempDir , Executors .newScheduledThreadPool (10 ), Executors .newScheduledThreadPool (10 ), objectMapper , blobTtl , true , new MetricRegistry ());
3241 }
3342
34- private final Duration blobTtl = Duration .days (1 );
43+ @ Test
44+ public void testCleanupWithAccessed () throws Exception {
45+ DateTime now = DateTime .now ();
3546
36- private FileSystemJsonBlobManager blobManager ;
47+ String oldBlobId = (new ObjectId (now .minusDays ((int ) (blobTtl .toMinutes () * 2 )).toDate ())).toString ();
48+ String newBlobId = new ObjectId (now .toDate ()).toString ();
49+ log .info ("newBlobId={}, oldBlobId={}" , newBlobId , oldBlobId );
3750
38- @ Before
39- public void initBlobManage () {
40- this .blobManager = new FileSystemJsonBlobManager (TEMP , Executors .newSingleThreadScheduledExecutor (), Executors .newScheduledThreadPool (10 ), new ObjectMapper (), blobTtl , true , new MetricRegistry ());
51+ Assert .assertEquals (0 , countFiles ());
52+ blobManager .createBlob ("{\" foo\" :|\" bar\" }" , oldBlobId );
53+ Assert .assertEquals (1 , countFiles ());
54+ blobManager .createBlob ("{\" foo\" :|\" bar\" }" , newBlobId );
55+ Assert .assertEquals (2 , countFiles ());
56+
57+ blobManager .getBlob (oldBlobId );
58+
59+ blobManager .run ();
60+
61+ log .info ("Starting blob manager" );
62+ blobManager .start ();
63+
64+ Thread .sleep (2000 );
65+
66+ Assert .assertEquals (2 , countFiles ());
4167 }
4268
4369 @ Test
4470 public void testCleanup () throws Exception {
4571 DateTime now = DateTime .now ();
4672
4773 String oldBlobId = (new ObjectId (now .minusDays ((int ) (blobTtl .toMinutes () * 2 )).toDate ())).toString ();
74+ String newBlobId = new ObjectId (now .toDate ()).toString ();
75+ log .info ("newBlobId={}, oldBlobId={}" , newBlobId , oldBlobId );
4876
4977 Assert .assertEquals (0 , countFiles ());
5078 blobManager .createBlob ("{\" foo\" :|\" bar\" }" , oldBlobId );
5179 Assert .assertEquals (1 , countFiles ());
52- blobManager .createBlob ("{\" foo\" :|\" bar\" }" , ( new ObjectId ( now . toDate ())). toString () );
80+ blobManager .createBlob ("{\" foo\" :|\" bar\" }" , newBlobId );
5381 Assert .assertEquals (2 , countFiles ());
5482
5583 blobManager .run ();
@@ -63,7 +91,7 @@ public void testCleanup() throws Exception {
6391 }
6492
6593 private long countFiles () throws IOException {
66- return Files .find (TEMP .toPath (), 999 , (p , bfa ) -> bfa .isRegularFile ()).count ();
94+ return Files .find (tempDir .toPath (), 999 , (p , bfa ) -> bfa .isRegularFile ()).count ();
6795 }
6896
6997}
0 commit comments