-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added job to delete files which are marked for deletion in every 2 hr.
- Loading branch information
imankitsingh
committed
Oct 19, 2018
1 parent
0371d09
commit 8f07168
Showing
6 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ classes/ | |
.project | ||
.settings | ||
.classpath | ||
out/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
grails-app/jobs/com/wizpanda/file/DeleteMarkedFileJob.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.wizpanda.file | ||
|
||
/** | ||
* Job to delete marked stored files from S3. | ||
* | ||
* @author Ankit Kumar Singh | ||
* @since 1.0.3 | ||
*/ | ||
class DeleteMarkedFileJob { | ||
|
||
FileDeletionService fileDeletionService | ||
|
||
static triggers = { | ||
simple repeatInterval: 1000l * 60 * 60 * 2 // execute job once in 2 hour | ||
} | ||
|
||
def execute() { | ||
fileDeletionService.deleteMarkedFiles() | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
grails-app/services/com/wizpanda/file/FileDeletionService.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.wizpanda.file | ||
|
||
|
||
import grails.gorm.transactions.Transactional | ||
import groovy.time.TimeCategory | ||
|
||
/** | ||
* Service class to perform delete operations on file which are marked for deletion. | ||
* | ||
* @author Ankit Kumar Singh | ||
* @since 1.0.3 | ||
*/ | ||
@Transactional | ||
class FileDeletionService { | ||
|
||
private static final int MAX = 100 | ||
|
||
FileUploadService fileUploadService | ||
|
||
/** | ||
* Method to delete stored files in every 2 hr. | ||
*/ | ||
void deleteMarkedFiles() { | ||
Date dateForDeletion | ||
|
||
use(TimeCategory) { | ||
dateForDeletion = new Date() - 2.hours | ||
} | ||
|
||
List<StoredFile> storedFileList = StoredFile.createCriteria().list { | ||
isNotNull("markedForDeletion") | ||
le("markedForDeletion", dateForDeletion) | ||
|
||
maxResult(MAX) | ||
} | ||
|
||
storedFileList.each { StoredFile storedFile -> | ||
try { | ||
fileUploadService.delete(storedFile) | ||
log.debug "$storedFile deleted from S3" | ||
storedFile.delete(flush: true) | ||
} catch (Exception e) { | ||
log.debug "File deletion failed due to $e" | ||
} | ||
} | ||
|
||
if (storedFileList.size() == MAX) { | ||
deleteMarkedFiles() | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/groovy/com/wizpanda/file/FileDeletionServiceSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.wizpanda.file | ||
|
||
import grails.testing.services.ServiceUnitTest | ||
import spock.lang.Specification | ||
|
||
class FileDeletionServiceSpec extends Specification implements ServiceUnitTest<FileDeletionService>{ | ||
|
||
def setup() { | ||
} | ||
|
||
def cleanup() { | ||
} | ||
|
||
void "test something"() { | ||
expect:"fix me" | ||
true == false | ||
} | ||
} |