Skip to content

Commit

Permalink
modify bulk task status update into a build flow (#971)
Browse files Browse the repository at this point in the history
* modify bulk task status update into build flow

* run formatter

* Add some logging and check if the Future completes (#972)

Co-authored-by: Lucas Burson <[email protected]>
  • Loading branch information
jschwarz2030 and ljdelight committed Sep 21, 2022
1 parent adc19cf commit 323c349
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
86 changes: 82 additions & 4 deletions app/org/maproulette/controllers/api/TaskController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ import org.maproulette.exception.{
StatusMessage
}
import org.maproulette.framework.model._
import org.maproulette.framework.model.{Challenge}
import org.maproulette.framework.model.Challenge
import org.maproulette.framework.psql.Paging
import org.maproulette.framework.service.{ServiceManager, TagService, TaskClusterService}
import org.maproulette.framework.mixins.TagsControllerMixin
import org.maproulette.framework.repository.TaskRepository
import org.maproulette.metrics.Metrics
import org.maproulette.models.dal.mixin.TagDALMixin
import org.maproulette.models.dal.{DALManager, TaskDAL}
import org.maproulette.provider.osm._
Expand Down Expand Up @@ -527,10 +528,87 @@ class TaskController @Inject() (
params = p.copy(location = Some(SearchLocation(-180, -90, 180, 90)))
}
val (count, tasks) = this.taskClusterService.getTasksInBoundingBox(user, params, Paging(-1))
tasks.foreach(task => {
val taskJson = Json.obj("id" -> task.id, "status" -> newStatus)
this.dal.update(taskJson, user)(task.id)
val challengeIds = params.challengeParams.challengeIds.getOrElse(List()).distinct.sorted

// Update challenge status to building
challengeIds.foreach(challengeId => {
this.dalManager.challenge.update(
Json.obj(
"status" -> Challenge.STATUS_BUILDING,
"statusMessage" -> Challenge.STATUS_MESSAGE_UPDATING_TASK_STATUSES
),
user
)(challengeId)
})

def resetStatuses(): Unit = {
challengeIds.foreach(challengeId => {
val challenge = this.serviceManager.challenge.retrieve(challengeId).get

// We need to check if the Challenge status changed during the bulk task update
// and ensure we don't overwrite it.
// If it didn't get updated, change status to Ready.
if (challenge.status.get == Challenge.STATUS_BUILDING) {
this.dalManager.challenge.update(
Json.obj(
"status" -> Challenge.STATUS_READY,
"statusMessage" -> Challenge.STATUS_MESSAGE_NONE
),
user
)(challengeId)
} else {
this.dalManager.challenge.update(
Json.obj("statusMessage" -> Challenge.STATUS_MESSAGE_NONE),
user
)(challengeId)
}
})
}

val requestId = request.id
val challengesStr = challengeIds.mkString(",")

Future {
logger.info(
"Starting bulkStatusChange for requestId={} challenges={} on {} tasks",
requestId,
challengesStr,
tasks.length
)
try {
Metrics.timer(s"bulkStatusChange_${requestId}_${challengesStr}") { () =>
tasks.foreach(task => {
val taskJson = Json.obj("id" -> task.id, "status" -> newStatus)
this.dal.update(taskJson, user)(task.id)
})
}
} finally {
logger.info(
"Finished running bulkStatusChange requestId={} challenges={} on {} tasks",
requestId,
challengesStr,
tasks.length
)
resetStatuses()
}
}.onComplete {
case Success(_) =>
logger.info(
"Successful bulkStatusChange update for requestId={} and challenges={} on {} tasks",
requestId,
challengesStr,
tasks.length
)
case Failure(e) =>
logger.warn(
"FAILED bulkStatusChange update for requestId={} and challenges={} on {} tasks",
requestId,
challengesStr,
tasks.length,
e
)
}

Ok(Json.toJson(tasks.length))
}
}
Expand Down
3 changes: 3 additions & 0 deletions app/org/maproulette/framework/model/Challenge.scala
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ object Challenge extends CommonField {
val STATUS_FINISHED = 5
val STATUS_DELETING_TASKS = 6

val STATUS_MESSAGE_NONE = ""
val STATUS_MESSAGE_UPDATING_TASK_STATUSES = "Updating Task Statuses"

// COOPERATIVE TYPES
val COOPERATIVE_NONE = 0
val COOPERATIVE_TAGS = 1
Expand Down

0 comments on commit 323c349

Please sign in to comment.