실행 중인 작업 제어하기
handle 안에서 @release_job(after: 30s)를 사용하면 시도 횟수를 소모하지 않고 작업을 큐에 다시 넣어 나중에 다시 실행되도록 합니다. 아직 준비되지 않은 작업에 사용하세요:
<import package="KScript.Queue" />
<job name="WaitForPayment" tries="1">
<field name="order_id" type="int" />
<field name="paid" type="bool" />
<fn name="handle">
<if not .paid>
<do>@release_job(after: 1m)</do>
</if>
<echo>Order {.order_id} is paid</echo>
</fn>
</job>import KScript.Queue
job WaitForPayment(tries: 1) {
order_id: int
paid: bool
fn handle() {
if not .paid => @release_job(after: 1m)
echo "Order {.order_id} is paid"
}
}@fail_job() 이제 작업이 실패 처리되며, 더 이상 재시도하지 않습니다. 성공할 가능성이 없는 작업이기 때문입니다. 오류나 메시지를 표시해요:
<import package="KScript.Queue" />
<job name="ImportFile" tries="5">
<field name="path" type="string" />
<fn name="handle">
<if not .path->ends_with('.csv')>
<do>@fail_job(Validation(message: 'Only CSV files can be imported'))</do>
</if>
<echo>Importing {.path}</echo>
</fn>
</job>import KScript.Queue
job ImportFile(tries: 5) {
path: string
fn handle() {
if not .path->ends_with('.csv') {
@fail_job(Validation(message: 'Only CSV files can be imported'))
}
echo "Importing {.path}"
}
}두 가지 모두 작업(job)의 handle 안에서만 동작합니다. 그 외의 곳에서 사용하면 Context 오류가 발생합니다. 핸들러 안의 exit은 해당 작업만 종료합니다: exit 또는 exit 0은 작업을 완료하고, 그 외의 코드는 오류처럼 시도를 실패 처리하므로 워커는 다음 작업을 이어서 처리합니다.