The Task Scheduler supports cron-like entries that create tasks on demand.
A separate supervisor process evaluates the configured schedules and creates the tasks. The supervisor is built into the ajc binary and is deployable in any container manager.
The scheduler supports highly-available clusters. Members perform leader election, and one member schedules tasks at a time. Restarts and signals are not required when schedules are added, removed, or updated.
Deploying to Kubernetes is supported through Helm charts. The scheduler can also run anywhere else, as described below.
Schedule overview
A scheduled task combines a cron-like schedule with task properties. The scheduler creates new jobs on demand, using those properties as templates.
// ScheduledTask represents a cron like schedule and task properties that will// result in regular new tasks to be created machine scheduletypeScheduledTaskstruct {
// Name is a unique name for the scheduled taskNamestring`json:"name"`// Schedule is a cron specification for the scheduleSchedulestring`json:"schedule"`// Queue is the name of a queue to enqueue the task intoQueuestring`json:"queue"`// TaskType is the type of task to createTaskTypestring`json:"task_type"`// Payload is the task payload for the enqueued tasksPayload []byte`json:"payload"`// Deadline is the time after scheduling that the deadline would beDeadlinetime.Duration`json:"deadline,omitempty"`// MaxTries is how many times the created task could be triedMaxTriesint`json:"max_tries"`// CreatedAt is when the schedule was createdCreatedAttime.Time`json:"created_at"`}
Valid schedules use the common unix cron format, including syntax like */5. Shortcuts such as @yearly, @monthly, @weekly, @daily, and @hourly are supported, along with @every 10m, where 10m is a Go standard duration.
Go API
Adding and loading scheduled tasks resembles working with a normal task:
client, _:=asyncjobs.NewClient(asyncjobs.NatsContext("AJC"))
// The deadline being an hour from now results in a Scheduled Task with a 1 hour deadline settask, _:=asyncjobs.NewTask("email:monthly", nil, asyncjobs.TaskDeadline(time.Now().Add(time.Hour)))
// Create the scheduleerr:=client.NewScheduledTask("EMAIL_MONTHLY_UPDATE", "@monthly", "EMAIL", task)
// Load itst, _:=client.LoadScheduledTaskByName("EMAIL_MONTHLY_UPDATE")
// Remove iterr = client.RemoveScheduledTask("EMAIL_MONTHLY_UPDATE")
CLI management
The CLI is new, and some aspects may change.
Adding and removing scheduled tasks
$ ajc task cron add EMAIL_MONTHLY_UPDATE "0 0 1 * *" email:monthly --queue EMAIL --deadline 12h
Scheduled Task EMAIL_MONTHLY_UPDATE created at 17 Feb 22 17:40:37 UTC
Schedule: 0 0 1 * *
Queue: EMAIL
Task Type: email:monthly
Payload: 0 B
Scheduling Deadline: 12h0m0s
The command adds a scheduled task that runs monthly. The shortcut monthly also works. Each run creates a task of type email:monthly in the EMAIL queue with a 12-hour deadline from creation.
To enter the @yearly schedule, use just yearly. The CLI interprets the @.
Remove a scheduled task by name with ajc task cron delete EMAIL_MONTHLY_UPDATE. The -f flag makes the command non-interactive.
Viewing schedules
List all schedules:
$ ajc task cron list
╭───────────────────────────────────────────────────────────────────────────────────╮
│ 1 Scheduled Task(s) │
├──────────────────────┬───────────┬───────┬───────────────┬────────────────────────┤
│ Name │ Schedule │ Queue │ Task Type │ Created │
├──────────────────────┼───────────┼───────┼───────────────┼────────────────────────┤
│ EMAIL_MONTHLY_UPDATE │ 0 0 1 * * │ EMAIL │ email:monthly │ 17 Feb 22 17:40:37 UTC │
╰──────────────────────┴───────────┴───────┴───────────────┴────────────────────────╯
$ ajc task cron list --names
EMAIL_MONTHLY_UPDATE