-
Notifications
You must be signed in to change notification settings - Fork 1
Implement weekly financial report schedule #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4e7e2a3
Implement weekly financial report schedule setup in worker initializa…
anatolyshipitz 9eb335d
Enhance error logging during weekly report schedule creation
anatolyshipitz 95a45d0
Update temporal configuration documentation for clarity
anatolyshipitz 63945a5
Refine logging and documentation for schedule setup
anatolyshipitz 4a8ecda
Refactor logging implementation and update imports
anatolyshipitz 3eb305d
Update workflow type in weekly report schedule configuration
anatolyshipitz 13ff085
Update logger level and enhance schedule validation logic
anatolyshipitz 7b07b52
Remove export of logger from index.ts to streamline module exports
anatolyshipitz 6594469
Refactor schedule creation to include race condition protection
anatolyshipitz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { Client } from '@temporalio/client'; | ||
|
|
||
| import { logger } from '../logger'; | ||
| import { workerConfig } from './worker'; | ||
|
|
||
| const SCHEDULE_ID = 'weekly-financial-report-schedule'; | ||
|
|
||
| /** | ||
| * Checks if an error is a "not found" error | ||
| */ | ||
| function validateIsScheduleNotFoundError(error: unknown): boolean { | ||
| return ( | ||
| (error as { code?: number }).code === 5 || | ||
| (error instanceof Error && | ||
| error.message.toLowerCase().includes('not found')) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if schedule exists, returns true if it exists | ||
| */ | ||
| async function validateScheduleExists(client: Client): Promise<boolean> { | ||
| try { | ||
| const scheduleHandle = client.schedule.getHandle(SCHEDULE_ID); | ||
|
|
||
| await scheduleHandle.describe(); | ||
| logger.info(`Schedule ${SCHEDULE_ID} already exists, skipping creation`); | ||
|
|
||
| return true; | ||
| } catch (error) { | ||
| if (!validateIsScheduleNotFoundError(error)) { | ||
| throw error; | ||
| } | ||
| logger.info(`Schedule ${SCHEDULE_ID} not found, creating new schedule`); | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates schedule with race condition protection | ||
| */ | ||
| async function createScheduleWithRaceProtection(client: Client): Promise<void> { | ||
| try { | ||
| await client.schedule.create({ | ||
| scheduleId: SCHEDULE_ID, | ||
| spec: { | ||
| cronExpressions: ['0 13 * * 2'], | ||
| timezone: 'America/New_York', | ||
| }, | ||
| action: { | ||
| type: 'startWorkflow', | ||
| workflowType: 'weeklyFinancialReportsWorkflow', | ||
| taskQueue: workerConfig.taskQueue, | ||
| workflowId: `weekly-financial-report-scheduled`, | ||
| }, | ||
| policies: { | ||
| overlap: 'SKIP', | ||
| catchupWindow: '1 day', | ||
| }, | ||
| }); | ||
|
|
||
| logger.info( | ||
| `Successfully created schedule ${SCHEDULE_ID} for weekly financial reports`, | ||
| ); | ||
| } catch (createError) { | ||
| // Handle race condition: schedule was created by another worker | ||
| const isAlreadyExists = | ||
| (createError as { code?: number }).code === 6 || | ||
| (createError instanceof Error && | ||
| (createError.message.toLowerCase().includes('already exists') || | ||
| createError.message.toLowerCase().includes('already running'))); | ||
|
|
||
| if (isAlreadyExists) { | ||
| logger.info( | ||
| `Schedule ${SCHEDULE_ID} already exists (created by another worker), treating as success`, | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| throw createError; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Sets up the weekly financial report schedule | ||
| * Schedule runs every Tuesday at 1 PM America/New_York time (EST/EDT) | ||
| * @param client - Temporal client instance | ||
| */ | ||
| export async function setupWeeklyReportSchedule(client: Client): Promise<void> { | ||
| try { | ||
| const isScheduleExists = await validateScheduleExists(client); | ||
|
|
||
| if (isScheduleExists) { | ||
| return; | ||
| } | ||
|
|
||
| await createScheduleWithRaceProtection(client); | ||
| } catch (error) { | ||
| logger.error( | ||
| `Failed to setup schedule ${SCHEDULE_ID}: ${error instanceof Error ? error.message : String(error)}`, | ||
| ); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Schedule configuration exported for documentation and testing | ||
| */ | ||
| export const scheduleConfig = { | ||
| scheduleId: SCHEDULE_ID, | ||
| cronExpression: '0 13 * * 2', | ||
| timezone: 'America/New_York', | ||
| description: 'Runs every Tuesday at 1 PM EST/EDT', | ||
| } as const; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { DefaultLogger } from '@temporalio/worker'; | ||
|
|
||
| /** | ||
| * Shared logger instance for the worker | ||
| * Using INFO level to capture important operational messages | ||
| * including schedule setup, errors, and warnings | ||
| */ | ||
| export const logger = new DefaultLogger('INFO'); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.