Discover how to accept or decline Job Router job offers

This guide lays out the steps you need to take to observe a Job Router offer. It also outlines how to accept or decline job offers.

Prerequisites

Accept job offers

After you create a job, observe the worker offer-issued event, which contains the worker ID and the job offer ID. The worker can accept job offers by using the SDK. Once the offer is accepted, the job is assigned to the worker, and the job's status is updated to assigned.

// Event handler logic omitted
var accept = await client.AcceptJobOfferAsync(offerIssuedEvent.Data.WorkerId, offerIssuedEvent.Data.OfferId);
// Event handler logic omitted
const accept = await client.path("/routing/workers/{workerId}/offers/{offerId}:accept",
    offerIssuedEvent.data.workerId, offerIssuedEvent.data.offerId).post();
# Event handler logic omitted
accept = client.accept_job_offer(offerIssuedEvent.data.worker_id, offerIssuedEvent.data.offer_id)
// Event handler logic omitted
AcceptJobOfferResult accept = client.acceptJobOffer(offerIssuedEvent.getData().getWorkerId(), offerIssuedEvent.getData().getOfferId());

Decline job offers

The worker can decline job offers by using the SDK. Once the offer is declined, the job is offered to the next available worker. The job is not offered to the same worker that declined the job until the worker is deregistered and registered again.

// Event handler logic omitted
await client.DeclineJobOfferAsync(new DeclineJobOfferOptions(workerId: offerIssuedEvent.Data.WorkerId,
    offerId: offerIssuedEvent.Data.OfferId));
// Event handler logic omitted
await client.path("/routing/workers/{workerId}/offers/{offerId}:decline",
    offerIssuedEvent.data.workerId, offerIssuedEvent.data.offerId).post();
# Event handler logic omitted
client.decline_job_offer(offerIssuedEvent.data.worker_id, offerIssuedEvent.data.offer_id)
// Event handler logic omitted
client.declineJobOffer(offerIssuedEvent.getData().getWorkerId(), offerIssuedEvent.getData().getOfferId());

Retry offer after some time

In some scenarios, a worker may want to automatically retry an offer after some time. For example, a worker may want to retry an offer after 5 minutes. To achieve this flow, the worker can use the SDK to decline the offer and specify the retryOfferAfter property.

// Event handler logic omitted
await client.DeclineJobOfferAsync(new DeclineJobOfferOptions(workerId: offerIssuedEvent.Data.WorkerId,
    offerId: offerIssuedEvent.Data.OfferId)
{
    RetryOfferAt = DateTimeOffset.UtcNow.AddMinutes(5)
});
// Event handler logic omitted
await client.path("/routing/workers/{workerId}/offers/{offerId}:decline",
    offerIssuedEvent.data.workerId, offerIssuedEvent.data.offerId).post({
        body: {
            retryOfferAt: new Date(Date.now() + 5 * 60 * 1000)
        }
    });
# Event handler logic omitted
client.decline_job_offer(
    worker_id = offerIssuedEvent.data.worker_id,
    offer_id = offerIssuedEvent.data.offer_id,
    retry_offer_at = datetime.utcnow() + timedelta(minutes = 5))
// Event handler logic omitted
client.declineJobOffer(
    offerIssuedEvent.getData().getWorkerId(),
    offerIssuedEvent.getData().getOfferId(),
    new RequestOptions().setBody(BinaryData.fromObject(
        new DeclineJobOfferOptions().setRetryOfferAt(OffsetDateTime.now().plusMinutes(5)))));

Complete the job

Once the worker has completed the work associated with the job (for example, completed the call), we complete the job, which updates the status to completed.

await routerClient.CompleteJobAsync(new CompleteJobOptions(jobId: accept.Value.JobId, assignmentId: accept.Value.AssignmentId));
await client.path("/routing/jobs/{jobId}/assignments/{assignmentId}:complete", accept.body.jobId, accept.body.assignmentId).post();
router_client.complete_job(job_id = job.id, assignment_id = accept.assignment_id)
routerClient.completeJobWithResponse(accept.getJobId(), accept.getAssignmentId(), null);

Close the job

Once the worker is ready to take on new jobs, the worker should close the job, which updates the status to closed. Optionally, the worker can provide a disposition code to indicate the outcome of the job.

await routerClient.CloseJobAsync(new CloseJobOptions(jobId: accept.Value.JobId, assignmentId: accept.Value.AssignmentId) {
    DispositionCode = "Resolved"
});
await client.path("/routing/jobs/{jobId}/assignments/{assignmentId}:close", accept.body.jobId, accept.body.assignmentId).post({
    body: {
        dispositionCode: "Resolved"
    }
});
router_client.close_job(job_id = job.id, assignment_id = accept.assignment_id, disposition_code = "Resolved")
routerClient.closeJobWithResponse(accept.getJobId(), accept.getAssignmentId(), 
    new RequestOptions().setBody(BinaryData.fromObject(new CloseJobOptions().setDispositionCode("Resolved"))));

Next steps