Scheduling a job

In the context of a call center, customers may want to receive a scheduled callback at a later time. As such, you need to create a scheduled job in Job Router.

Prerequisites

Create a job using the ScheduleAndSuspendMode

In the following example, a job is created that is scheduled 3 minutes from now by setting the MatchingMode to ScheduleAndSuspendMode with a scheduleAt parameter. This example assumes that you created a queue with the queueId Callback and that there's an active worker registered to the queue with available capacity on the Voice channel.

await client.CreateJobAsync(new CreateJobOptions(jobId: "job1", channelId: "Voice", queueId: "Callback")
{
    MatchingMode = new ScheduleAndSuspendMode(scheduleAt: DateTimeOffset.UtcNow.Add(TimeSpan.FromMinutes(3)))
});
await client.path("/routing/jobs/{jobId}", "job1").patch({
    body: {
        channelId: "Voice",
        queueId: "Callback",
        matchingMode: {
            kind: "scheduleAndSuspend",
            scheduleAt: new Date(Date.now() + 3 * 60000)
        }
    },
    contentType: "application/merge-patch+json"
});
client.upsert_job(
    job_id = "job1",
    channel_id = "Voice",
    queue_id = "Callback",
    matching_mode = ScheduleAndSuspendMode(schedule_at = datetime.utcnow() + timedelta(minutes = 3)))
client.createJob(new CreateJobOptions("job1", "Voice", "Callback")
    .setMatchingMode(new ScheduleAndSuspendMode(OffsetDateTime.now().plusMinutes(3))));

Note

The job's status after being scheduled is initially PendingSchedule, and once Job Router successfully schedules the job, the status is updated to Scheduled.

Wait for the scheduled time to be reached, then queue the job

When the scheduled time is reached, the job's status is updated to WaitingForActivation and Job Router emits a RouterJobWaitingForActivation event to Event Grid. If this event is subscribed, some required actions may be performed, before enabling the job to be matched to a worker. For example, in the context of the contact center, such an action could be making an outbound call and waiting for the customer to accept the callback. Once the required actions are complete, the job can be queued by calling the UpdateJobAsync method with the MatchingMode set to QueueAndMatchMode and priority set to 100 to quickly find an eligible worker, which updates the job's status to queued.

// Event Grid Event Handler code omitted
if (eventGridEvent.EventType == "Microsoft.Communication.RouterJobWaitingForActivation")
{
    // Perform required actions here

    await client.UpdateJobAsync(new RouterJob(jobId: eventGridEvent.Data.JobId)
    {
        MatchingMode = new QueueAndMatchMode(),
        Priority = 100
    });
}
// Event Grid Event Handler code omitted
if (eventGridEvent.EventType == "Microsoft.Communication.RouterJobWaitingForActivation")
{
    // Perform required actions here

    await client.path("/routing/jobs/{jobId}", eventGridEvent.data.jobId).patch({
      body: {
          matchingMode: { kind: "queueAndMatch" },
          priority: 100
      },
      contentType: "application/merge-patch+json"
    });
}
# Event Grid Event Handler code omitted
if (eventGridEvent.event_type == "Microsoft.Communication.RouterJobWaitingForActivation")
{
    # Perform required actions here

    client.upsert_job(
        job_id = eventGridEvent.data.job_id,
        matching_mode = queue_and_match_mode = {},
        priority = 100)
}
// Event Grid Event Handler code omitted
if (eventGridEvent.EventType == "Microsoft.Communication.RouterJobWaitingForActivation")
{
    // Perform required actions here

    job = client.updateJob(eventGridEvent.getData().toObject(new TypeReference<Map<String, Object>>() {
}).get("JobId").toString(), BinaryData.fromObject(new RouterJob()
        .setMatchingMode(new QueueAndMatchMode())
        .setPriority(100)), null).toObject(RouterJob.class);
}

Next steps

i