Skip to content

Add nice API to reschedule/modify an existing task's execution date #9

@Andrewangeta

Description

@Andrewangeta

Often there are scenarios where a task is queued but due to external changes (user changing their notification settings for example.) the task execution date needs to be changed.

There a 2 ways of handling this:

  1. Just remove the old task and replace it with the new task.
  2. Update the existing task with a new execution date.

I opt for option 2 since it's more efficient and can be done in a single query (findAndModify) vs a delete and insert query.

Currently there is no good API for handling this so we have to resort to using code like this

let document = try await context.mongodb["queue"].find(...) // find a scheduled Task

if let metadata = document["metadata"] as? Document,
let reminderDate = metadata["reminder_date"] as? Date,
let id = document["_id"] as? ObjectId {
    let newDate = reminderDate.addingTimeInterval(....)           
     _ = try await context.mongodb["queue"].findAndModify(where: ["_id": id],
                                                          update: ["$set": ["configuration.scheduledDate": newDate] as Document]).execute()
}

Also there is some slight discrepancy/redundancy for execution dates. In the screenshot attached there are 3 separate date fields that represent when a task should be executed.

But the one that really matters is the configuration.scheduledDate. There is a caveat that the metadata.taskExecutionDate is derived from conforming to the ScheduledTask but that value is actually never read when it's time to execute but it's rather used to create the configuration.scheduledDate. Maybe we don't have to save it to take up extra space but decoding would fail since we have to specify it anyway another problem for another day.

Also maxTaskDuration is also duplicated when it doesn't necessarily have to be.

Screen Shot 2022-08-21 at 7 40 22 AM

As far as a better API maybe there could be a function on the context that can be

func updateTaskExecutionDate(date: Date, filter: Document) async throws

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions