Permalink
Browse files

- added a compile time dependency on Quartz on the ci and development…

… environments

- added a Quartz job for clean up uploaded files older than 4 hours
  • Loading branch information...
1 parent 2527b2d commit e38f42e0221e9cd9cd98c8fef7cf5e3c051ed8ed @4np committed Jan 30, 2013
@@ -55,6 +55,8 @@ grails.project.dependency.resolution = {
":jquery:latest.integration",
":modernizr:latest.integration",
+ ":quartz:1.0-RC5",
+
":resources:latest.integration",
":hibernate:$grailsVersion"
) {
@@ -67,6 +69,7 @@ grails.project.dependency.resolution = {
// as the ci should be able to run natively, we require
// a couple of plugins to function properly
compile(":resources:latest.integration",
+ ":quartz:1.0-RC5",
":hibernate:$grailsVersion") {
export = false
}
@@ -24,3 +24,8 @@ log4j = {
}
grails.views.default.codec="none" // none, html, base64
grails.views.gsp.encoding="UTF-8"
+
+// used by the demo tag
+uploadr.defaultUploadPath="${System.getProperty('user.home')}/Downloads/uploadr"
+// used by the cleanUploadedFilesJob Quartz job (on ci and development)
+uploadr.maxAgeUploadedFile=1000 * 60 * 60 * 4 // 4 hours in milliseconds
@@ -0,0 +1,60 @@
+package hungry.wombat
+
+/**
+ * Quartz job to clean uploaded files in development and
+ * continuous integration instances
+ *
+ */
+class CleanUploadedFilesJob {
+ def grailsApplication
+
+ static triggers = {
+ // cronjob that runs every ten minutes
+ cron name: 'removeExpiredAPITokens', cronExpression: "0 */10 * * * ?"
+ }
+
+ def execute() {
+ def fileExpiry = grailsApplication.config.uploadr.maxAgeUploadedFile // in milliseconds
+ def currentDate = new Date().getTime()
+ def uploadPath = new File(grailsApplication.config.uploadr.defaultUploadPath)
+ def dirsToDelete= []
+
+ // does the upload path exist?
+ if (uploadPath.exists() && uploadPath.isDirectory()) {
+ // yes, iterate recursively over files
+ uploadPath.eachFileRecurse { file ->
+ if (file.exists()) {
+ if (file.isFile() && currentDate > (file.lastModified() + fileExpiry)) {
+ // file is older than threshold, delete it
+ def parentFile = file.getParentFile()
+
+ if (file.delete()) {
+ log.info "cleaned up file ${file}"
+
+ // is the parent path empty?
+ if (parentFile.isDirectory() && parentFile.listFiles().size() == 0) {
+ dirsToDelete << parentFile
+ }
+ } else {
+ log.error "could not cleanup file ${file}"
+ }
+ }
+ }
+ }
+
+ // add base upload path last
+ dirsToDelete << uploadPath
+
+ // delete dirs
+ dirsToDelete.each { dir ->
+ if (dir.isDirectory() && dir.listFiles().size() == 0) {
+ if (dir.deleteDir()) {
+ log.info "cleaned up directory ${dir}"
+ } else {
+ log.error "could not cleanup directory ${dir}"
+ }
+ }
+ }
+ }
+ }
+}
@@ -21,7 +21,8 @@
// controller, as in MVC your controller should never contain business logic
// and just provide the view to your data. But for clarity sake I am breaking
// this rule and defining them in here...
-def downloads = "${System.getProperty('user.home')}/Downloads/uploadr"
+
+def downloads = grailsApplication.config.uploadr.defaultUploadPath
def path1 = new File("${downloads}/myFirstUploadr")
def path2 = new File("${downloads}/mySecondUploadr")
def path3 = new File("${downloads}/myThirdUploadr")
@@ -0,0 +1,13 @@
+package hungry.wombat
+
+
+
+class CleanUploadedFilesJobTests {
+ static triggers = {
+ simple repeatInterval: 5000l // execute job once in 5 seconds
+ }
+
+ def execute() {
+ // execute job
+ }
+}

0 comments on commit e38f42e

Please sign in to comment.