Job Router のジョブ オファーを承諾または拒否する方法を確認する

このガイドでは、Job Router オファーを監視するために必要な手順について説明します。 また、ジョブ オファーを承諾または拒否する方法についても説明します。

前提条件

ジョブ オファーを受け入れる

ジョブを作成したら、worker ID とジョブ オファー ID を含む worker オファーによって発行されたイベントを確認します。 worker は、SDK を使用してジョブ オファーを受け入れることができます。 オファーが受け入れられると、ジョブがワーカーに割り当てられ、ジョブの状態が 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());

ジョブ オファーを拒否する

worker は SDK を使用して、ジョブ オファーを拒否できます。 オファーが拒否されると、ジョブは次に使用可能な worker に提供されます。 ジョブは、その worker が登録解除されて再登録されるまで、ジョブを拒否したのと同じワーカーには提供されません。

// 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());

しばらくしてからオファーを再試行する

一部のシナリオでは、worker はしばらくしてからオファーを自動的に再試行する場合があります。 たとえば、worker は 5 分後にオファーを再試行する場合があります。 このフローを実現するために、ワーカーは SDK を使用してオファーを拒否し、retryOfferAfter プロパティを指定できます。

// 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)))));

ジョブを完了する

ワーカーがジョブに関連付けられている作業を完了すると (たとえば、呼び出しが完了した場合)、ジョブが完了し、状態が 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);

ジョブを閉じる

worker が新しいジョブを実行する準備ができたら、ワーカーはジョブを閉じる必要があり、すると状態が closed に更新されます。 必要に応じて、ワーカーは処理コードを指定してジョブの結果を示すことができます。

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"))));

次のステップ