-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathschedule.ts
More file actions
61 lines (56 loc) · 1.85 KB
/
Copy pathschedule.ts
File metadata and controls
61 lines (56 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { ScheduleAlreadyRunning, ScheduleOverlapPolicy } from '@temporalio/client'
import { svc } from '../service'
import { ingestSecurityContacts } from '../workflows'
// Unlike workflowRunTimeout, workflowExecutionTimeout bounds the entire continueAsNew chain —
// caps a run to 24h so it can't still be going when the next day's tick fires.
const SCHEDULE_ID = 'security-contacts-ingestion'
const WORKFLOW_EXECUTION_TIMEOUT = '24 hours'
function scheduleAction() {
return {
type: 'startWorkflow' as const,
workflowType: ingestSecurityContacts,
workflowId: 'security-contacts-daily',
taskQueue: 'packages-worker',
workflowExecutionTimeout: WORKFLOW_EXECUTION_TIMEOUT,
retry: {
initialInterval: '30 seconds',
backoffCoefficient: 2,
maximumAttempts: 3,
},
args: [] as [],
}
}
export async function scheduleSecurityContactsIngestion(): Promise<void> {
const { temporal } = svc
if (!temporal) throw new Error('Temporal client not initialized')
try {
await temporal.schedule.create({
scheduleId: SCHEDULE_ID,
spec: {
cronExpressions: ['0 6 * * *'],
},
policies: {
overlap: ScheduleOverlapPolicy.SKIP,
catchupWindow: '1 hour',
},
action: scheduleAction(),
})
} catch (err) {
if (err instanceof ScheduleAlreadyRunning) {
// create() only runs once; reconcile on every startup so action/policy changes reach
// the already-existing live schedule.
svc.log.info('Schedule security-contacts-ingestion already exists, reconciling action.')
const handle = temporal.schedule.getHandle(SCHEDULE_ID)
await handle.update((prev) => ({
...prev,
policies: {
...prev.policies,
overlap: ScheduleOverlapPolicy.SKIP,
},
action: scheduleAction(),
}))
} else {
throw err
}
}
}