|
| 1 | +import { Client } from '@temporalio/client'; |
| 2 | + |
| 3 | +import { logger } from '../logger'; |
| 4 | +import { workerConfig } from './worker'; |
| 5 | + |
| 6 | +const SCHEDULE_ID = 'weekly-financial-report-schedule'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Checks if an error is a "not found" error |
| 10 | + */ |
| 11 | +function validateIsScheduleNotFoundError(error: unknown): boolean { |
| 12 | + return ( |
| 13 | + (error as { code?: number }).code === 5 || |
| 14 | + (error instanceof Error && |
| 15 | + error.message.toLowerCase().includes('not found')) |
| 16 | + ); |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Checks if schedule exists, returns true if it exists |
| 21 | + */ |
| 22 | +async function validateScheduleExists(client: Client): Promise<boolean> { |
| 23 | + try { |
| 24 | + const scheduleHandle = client.schedule.getHandle(SCHEDULE_ID); |
| 25 | + |
| 26 | + await scheduleHandle.describe(); |
| 27 | + logger.info(`Schedule ${SCHEDULE_ID} already exists, skipping creation`); |
| 28 | + |
| 29 | + return true; |
| 30 | + } catch (error) { |
| 31 | + if (!validateIsScheduleNotFoundError(error)) { |
| 32 | + throw error; |
| 33 | + } |
| 34 | + logger.info(`Schedule ${SCHEDULE_ID} not found, creating new schedule`); |
| 35 | + |
| 36 | + return false; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Creates schedule with race condition protection |
| 42 | + */ |
| 43 | +async function createScheduleWithRaceProtection(client: Client): Promise<void> { |
| 44 | + try { |
| 45 | + await client.schedule.create({ |
| 46 | + scheduleId: SCHEDULE_ID, |
| 47 | + spec: { |
| 48 | + cronExpressions: ['0 13 * * 2'], |
| 49 | + timezone: 'America/New_York', |
| 50 | + }, |
| 51 | + action: { |
| 52 | + type: 'startWorkflow', |
| 53 | + workflowType: 'weeklyFinancialReportsWorkflow', |
| 54 | + taskQueue: workerConfig.taskQueue, |
| 55 | + workflowId: `weekly-financial-report-scheduled`, |
| 56 | + }, |
| 57 | + policies: { |
| 58 | + overlap: 'SKIP', |
| 59 | + catchupWindow: '1 day', |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + logger.info( |
| 64 | + `Successfully created schedule ${SCHEDULE_ID} for weekly financial reports`, |
| 65 | + ); |
| 66 | + } catch (createError) { |
| 67 | + // Handle race condition: schedule was created by another worker |
| 68 | + const isAlreadyExists = |
| 69 | + (createError as { code?: number }).code === 6 || |
| 70 | + (createError instanceof Error && |
| 71 | + (createError.message.toLowerCase().includes('already exists') || |
| 72 | + createError.message.toLowerCase().includes('already running'))); |
| 73 | + |
| 74 | + if (isAlreadyExists) { |
| 75 | + logger.info( |
| 76 | + `Schedule ${SCHEDULE_ID} already exists (created by another worker), treating as success`, |
| 77 | + ); |
| 78 | + |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + throw createError; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Sets up the weekly financial report schedule |
| 88 | + * Schedule runs every Tuesday at 1 PM America/New_York time (EST/EDT) |
| 89 | + * @param client - Temporal client instance |
| 90 | + */ |
| 91 | +export async function setupWeeklyReportSchedule(client: Client): Promise<void> { |
| 92 | + try { |
| 93 | + const isScheduleExists = await validateScheduleExists(client); |
| 94 | + |
| 95 | + if (isScheduleExists) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + await createScheduleWithRaceProtection(client); |
| 100 | + } catch (error) { |
| 101 | + logger.error( |
| 102 | + `Failed to setup schedule ${SCHEDULE_ID}: ${error instanceof Error ? error.message : String(error)}`, |
| 103 | + ); |
| 104 | + throw error; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Schedule configuration exported for documentation and testing |
| 110 | + */ |
| 111 | +export const scheduleConfig = { |
| 112 | + scheduleId: SCHEDULE_ID, |
| 113 | + cronExpression: '0 13 * * 2', |
| 114 | + timezone: 'America/New_York', |
| 115 | + description: 'Runs every Tuesday at 1 PM EST/EDT', |
| 116 | +} as const; |
0 commit comments