本指南會列出觀察作業路由器供應項目所需的步驟。 它還概述瞭如何接受或拒絕工作機會。
先決條件
- 具有有效訂閱的 Azure 帳戶。 免費建立 Azure 帳戶。
- 已部署的 Azure 通訊服務資源。 建立通訊服務資源。
- (選擇性步驟):完成快速入門來 開始使用 Job Router。
接受工作機會
建立工作之後,請觀察工作者邀約發出的事件,其中包含工作者識別碼和工作邀約識別碼。 工作人員可以使用 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());
拒絕工作機會
工作人員可以使用 SDK 拒絕工作機會。 一旦報價被拒絕,該職位就會提供給下一個可用的工作人員。 在工人取消註冊並重新註冊之前,不會再次提供工作給已拒絕的同一工人。
// 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());
過一段時間後重試該提議
在某些情況下,工作者可能想要在一段時間後自動重試提案。 例如,工作者可以在 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);
結束工作
一旦工作人員準備好承擔新工作,工作人員就應該關閉該工作,這會將狀態更新為 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"))));
後續步驟
- 檢閱如何管理作業路由器佇列。
- 瞭解如何 訂閱 Job Router 事件。