작업 옵션
옵션은 작업 이름 뒤의 대괄호 안에 씁니다. 시간은 따옴표 없이 기간으로 씁니다: 500ms, 30s, 10m, 2h, 1d. 숫자만 쓰면 초 단위입니다.
큐와 연결
queue는 작업이 올라가는 큐입니다(별도로 지정하지 않으면 default). connection는 작업이 전달되는 kscript.json의 연결입니다(별도로 지정하지 않으면 queue.default). 연결과 드라이버를 참고하세요.
재시도와 백오프
작업은 tries을 지정하지 않으면 3번 시도합니다. backoff는 각 재시도 전에 기다릴 시간입니다. 한 번만 기다리는 값(30s), 마지막 값이 반복되는 목록([10s, 1m, 5m]), 또는 기본값인 'exponential'가 있으며, 기본값은 2초, 4초, 8초 식으로 늘어나 최대 10분까지 기다립니다. retry_until는 작업이 디스패치된 후 일정 시간이 지나면 시도 횟수가 남아 있어도 재시도를 멈춥니다:
<import package="KScript.Queue" />
<job name="FetchRates" tries="10" backoff="[10s, 1m, 5m]" retry_until="1h">
<field name="currency" type="string" />
<fn name="handle">
<echo>Fetching {.currency} rates</echo>
</fn>
</job>import KScript.Queue
job FetchRates(tries: 10, backoff: [10s, 1m, 5m], retry_until: 1h) {
currency: string
fn handle() {
echo "Fetching {.currency} rates"
}
}시간 초과
timeout는 한 번의 시도가 실행될 수 있는 최대 시간이며, 별도로 지정하지 않으면 60초입니다. 이 시간을 넘으면 시도는 JobTimeout 오류와 함께 중단되고 한 번의 시도로 계산됩니다:
<import package="KScript.Queue" />
<job name="BuildReport" timeout="5m">
<field name="month" type="string" />
<fn name="handle">
<echo>Building the report for {.month}</echo>
</fn>
</job>import KScript.Queue
job BuildReport(timeout: 5m) {
month: string
fn handle() {
echo "Building the report for {.month}"
}
}재시도할 오류
기본적으로 모든 오류가 재시도됩니다. retry_on는 나열한 오류 유형만 재시도하고, 그 밖의 오류에서는 작업을 즉시 실패시킵니다. fail_on는 나열한 유형을 절대 재시도하지 않습니다. 오류 유형은 하위 유형도 함께 매칭되므로, retry_on: [Network]는 error ApiDown extends Network로 선언된 오류도 재시도합니다:
<import package="KScript.Queue" />
<job name="PostWebhook" retry_on="[Network, Timeout]" fail_on="[Validation]">
<field name="url" type="url" />
<fn name="handle">
<echo>Posting to {.url}</echo>
</fn>
</job>import KScript.Queue
job PostWebhook(retry_on: [Network, Timeout], fail_on: [Validation]) {
url: url
fn handle() {
echo "Posting to {.url}"
}
}고유 작업
unique_for는 같은 필드를 가진 작업이 대기 중이거나 실행 중인 동안에는, 그 시간만큼 작업을 한 번만 큐에 넣습니다. unique_by는 나열한 필드만 비교합니다:
<import package="KScript.Queue" />
<job name="RebuildSearchIndex" unique_for="10m" unique_by="user_id">
<field name="user_id" type="int" />
<fn name="handle">
<echo>Rebuilding the index for user {.user_id}</echo>
</fn>
</job>import KScript.Queue
job RebuildSearchIndex(unique_for: 10m, unique_by: [user_id]) {
user_id: int
fn handle() {
echo "Rebuilding the index for user {.user_id}"
}
}