PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/dos/app/Models.scala

https://github.com/delving/culture-hub
Scala | 158 lines | 99 code | 44 blank | 15 comment | 3 complexity | c7d0820fc91747dfc19b6123848b3c96 MD5 | raw file
  1. /*
  2. * Copyright 2011 Delving B.V.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package models {
  17. import com.mongodb.casbah.Imports.{ MongoCollection, MongoDB }
  18. import com.mongodb.casbah.query.Imports._
  19. import java.util.Date
  20. import com.novus.salat.dao.SalatDAO
  21. import java.io.File
  22. import play.api.Play
  23. import play.api.Play.current
  24. package object dos extends MongoContext {
  25. def getNode = Play.configuration.getString("cultureHub.nodeName").getOrElse("defaultDosNode")
  26. }
  27. package dos {
  28. case class Log(
  29. _id: ObjectId = new ObjectId,
  30. orgId: String,
  31. task_id: ObjectId,
  32. date: Date = new Date,
  33. node: String,
  34. message: String,
  35. taskType: TaskType, // saved here for redundancy
  36. sourceItem: Option[String] = None,
  37. resultItem: Option[String] = None, // file path or URL or ID to a single item that was processed, if applicable
  38. level: LogLevel = LogLevel.INFO)
  39. object Log extends MultiModel[Log, LogDAO] {
  40. protected def connectionName: String = "Logs"
  41. protected def initIndexes(collection: MongoCollection) {}
  42. protected def initDAO(collection: MongoCollection, connection: MongoDB)(implicit configuration: OrganizationConfiguration): LogDAO = new LogDAO(collection)
  43. }
  44. class LogDAO(collection: MongoCollection) extends SalatDAO[Log, ObjectId](collection)
  45. case class LogLevel(name: String)
  46. object LogLevel {
  47. val INFO = LogLevel("info")
  48. val ERROR = LogLevel("error")
  49. val values = List(INFO, ERROR)
  50. def valueOf(what: String) = values find { _.name == what }
  51. }
  52. case class Task(
  53. _id: ObjectId = new ObjectId,
  54. node: String,
  55. orgId: String,
  56. path: String,
  57. taskType: TaskType,
  58. params: Map[String, String] = Map.empty[String, String],
  59. queuedAt: Date = new Date,
  60. startedAt: Option[Date] = None,
  61. finishedAt: Option[Date] = None,
  62. state: TaskState = TaskState.QUEUED,
  63. totalItems: Int = 0,
  64. processedItems: Int = 0) {
  65. def pathAsFile = new File(path)
  66. def pathExists = new File(path).exists()
  67. def isCancelled = Task.dao(orgId).findOne(MongoDBObject("_id" -> _id, "state.name" -> TaskState.CANCELLED.name)).isDefined
  68. override def toString = "Task[%s] type: %s, path: %s, params: %s".format(_id, taskType.name, path, params.toString)
  69. }
  70. object Task extends MultiModel[Task, TaskDAO] {
  71. protected def connectionName: String = "Tasks"
  72. protected def initIndexes(collection: MongoCollection) {}
  73. protected def initDAO(collection: MongoCollection, connection: MongoDB)(implicit configuration: OrganizationConfiguration): TaskDAO = new TaskDAO(collection)
  74. }
  75. class TaskDAO(collection: MongoCollection) extends SalatDAO[Task, ObjectId](collection) {
  76. def list(taskType: TaskType) = find(MongoDBObject("taskType.name" -> taskType.name)).toList
  77. def list(state: TaskState) = find(MongoDBObject("state.name" -> state.name, "node" -> getNode)).sort(MongoDBObject("queuedAt" -> 1)).toList
  78. def listAll() = find(MongoDBObject()).sort(MongoDBObject("queuedAt" -> 1)).toList
  79. def start(task: Task) {
  80. update(MongoDBObject("_id" -> task._id), $set("state.name" -> TaskState.RUNNING.name, "startedAt" -> new Date))
  81. }
  82. def finish(task: Task) {
  83. update(MongoDBObject("_id" -> task._id), $set("state.name" -> TaskState.FINISHED.name, "finishedAt" -> new Date))
  84. }
  85. def cancel(task: Task) {
  86. update(MongoDBObject("_id" -> task._id), $set("state.name" -> TaskState.CANCELLED.name, "finishedAt" -> new Date))
  87. }
  88. def setTotalItems(task: Task, total: Int) {
  89. update(MongoDBObject("_id" -> task._id), $set("totalItems" -> total))
  90. }
  91. def incrementProcessedItems(task: Task, amount: Int) {
  92. update(MongoDBObject("_id" -> task._id), $inc("processedItems" -> amount))
  93. }
  94. }
  95. case class TaskType(name: String)
  96. object TaskType {
  97. val THUMBNAILS_CREATE = TaskType("createThumbnails")
  98. val THUMBNAILS_DELETE = TaskType("deleteThumbnails")
  99. val NORMALIZE = TaskType("normalize")
  100. val TILES = TaskType("tiles")
  101. val values = List(THUMBNAILS_CREATE, THUMBNAILS_DELETE, NORMALIZE, TILES)
  102. def valueOf(what: String) = values find { _.name == what }
  103. }
  104. case class TaskState(name: String)
  105. object TaskState {
  106. val QUEUED = TaskState("queued")
  107. val RUNNING = TaskState("running")
  108. val FINISHED = TaskState("finished")
  109. val CANCELLED = TaskState("cancelled")
  110. val values = List(QUEUED, RUNNING, FINISHED, CANCELLED)
  111. def valueOf(what: String) = values find { _.name == what }
  112. }
  113. }
  114. }