Created
June 5, 2012 15:52
-
-
Save thiagopnts/2875881 to your computer and use it in GitHub Desktop.
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
package models | |
import dispatch._ | |
import dispatch.mime.Mime._ | |
import play.api.libs.json._ | |
import play.api.Logger | |
import play.api.Play.current | |
import play.api.libs.concurrent._ | |
import akka.actor.{Actor, ActorLogging} | |
import akka.util.duration._ | |
import akka.util.Timeout | |
class Worker extends Actor with ActorLogging { | |
var configuration = Configuration.getCurrentConfiguration | |
var lastFile: String = _ | |
protected def receive = { | |
case "start" => this.run | |
} | |
private def clean(documentId: Int) = { | |
var server = configuration.host + Configuration.DELETE_PATH | |
Logger.info("Deleting document...") | |
Http(url(server).as_!(configuration.username, configuration.password) << Map("document_ids" -> documentId.toString) as_str) | |
server = configuration.host + Configuration.PURGE_PATH | |
Logger.info("Purging document...") | |
Http(url(server).as_!(configuration.username, configuration.password) << Map("document_ids" -> documentId.toString) as_str) | |
} | |
private def getDocumentStatus(documentId: Int): JsValue = { | |
val server = configuration.host + Configuration.DOCUMENT_PATH + documentId + Configuration.EXTENSION | |
Logger.info("Checking status for document id: %d".format(documentId)) | |
val document = Json.parse(Http(url(server).as_!(configuration.username, configuration.password) as_str)) | |
Logger.info("The current status for document %d is %s".format(documentId, (document \ "status").as[String])) | |
document | |
} | |
private def generateNewFile: java.io.File = { | |
val number = scala.util.Random.nextLong | |
val newFilename = if(number < 0) (number * -1).toString + ".pdf" else number.toString + ".pdf" | |
val newFile = new java.io.File("resources/" + newFilename) | |
newFile | |
} | |
private def doUpload: Int = { | |
this.configuration = Configuration.getCurrentConfiguration | |
log.info("Username: %s".format(configuration.username)) | |
val file: java.io.File = if (this.lastFile == null) { | |
val files = new java.io.File("resources/").listFiles | |
files(0) | |
} else new java.io.File(this.lastFile) | |
val newFile = this.generateNewFile | |
file.renameTo(newFile) | |
this.lastFile = newFile.getPath | |
val server = configuration.host + Configuration.UPLOAD_PATH | |
Logger.info("Uploading file: " + newFile.getName) | |
val document = Json.parse(Http(url(server).as_!(configuration.username, configuration.password) << Map("file" -> newFile.toString) <<* ("file", newFile, "application/pdf") as_str)) | |
(document \ "id").as[Int] | |
} | |
private def check(documentId: Int): Unit = { | |
val time = 2.minutes.fromNow | |
var filename: String = this.lastFile.split("/")(1) | |
while(time.timeLeft > 0.seconds) { | |
var marked = false | |
if(!marked) { | |
JobReport.addJobReport(JobReport( (2.minutes - time.timeLeft).toSeconds, "WAITING", filename, configuration.username)) | |
marked = true | |
} | |
val document = this.getDocumentStatus(documentId) | |
val status = (document \ "status").as[String] | |
filename = (document \ "name").as[String] | |
if(status == "CONVERTED") { | |
val timeTotal = (2.minutes - time.timeLeft).toSeconds | |
Logger.info("Document %d converted, took %s seconds".format(documentId, timeTotal.toString)) | |
JobReport.addJobReport(JobReport(timeTotal, status, filename, configuration.username)) | |
this.clean(documentId) | |
return | |
} | |
} | |
JobReport.addJobReport(JobReport(2.minutes.toSeconds, "FAILED", filename, configuration.username)) | |
Logger.info("Document %d conversion timeout".format(documentId)) | |
this.clean(documentId) | |
} | |
def run = { | |
while(true) { | |
val documentId = this.doUpload(workerName) | |
this.check(documentId) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment