Skip to content

Commit

Permalink
Added job to delete files which are marked for deletion in every 2 hr.
Browse files Browse the repository at this point in the history
  • Loading branch information
imankitsingh committed Oct 19, 2018
1 parent 0371d09 commit 8f07168
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ classes/
.project
.settings
.classpath
out/
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies {
compile "org.grails:grails-core"
compile group: "org.apache.jclouds", name: "jclouds-core", version: "1.9.2"
compile group: "org.apache.jclouds", name: "jclouds-allblobstore", version: "1.9.2"
compile 'org.grails.plugins:quartz:2.0.13'
console "org.grails:grails-console"
profile "org.grails.profiles:plugin"
provided "org.grails:grails-plugin-services"
Expand Down
14 changes: 12 additions & 2 deletions grails-app/domain/com/wizpanda/file/StoredFile.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.jclouds.blobstore.domain.Blob

class StoredFile {

boolean markedForDeletion
Date markedForDeletion

String originalName
String name
Expand All @@ -20,8 +20,12 @@ class StoredFile {
Holders.getApplicationContext()['fileUploadService']
}

static constraints = {
markedForDeletion nullable: true
}

StoredFileBlob getBlob() {
Blob blob = getFileUploadService().getStorageApi(this.groupName).getBlob(this)
Blob blob = getFileUploadService().getStorageApi(this.groupName).getBlob(this)

return new StoredFileBlob(blob)
}
Expand All @@ -33,4 +37,10 @@ class StoredFile {
void cloneFile(String newGroupName) {
fileUploadService?.cloneFile(this, newGroupName)
}

void markForDeletion() {
this.markedForDeletion = new Date()
this.save(failOnError: true)
}

}
20 changes: 20 additions & 0 deletions grails-app/jobs/com/wizpanda/file/DeleteMarkedFileJob.groovy
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 grails-app/services/com/wizpanda/file/FileDeletionService.groovy
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 src/test/groovy/com/wizpanda/file/FileDeletionServiceSpec.groovy
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
}
}

0 comments on commit 8f07168

Please sign in to comment.