Skip to content

Commit 13ff085

Browse files
Update logger level and enhance schedule validation logic
- Changed logger level from ERROR to INFO to capture important operational messages, including schedule setup, errors, and warnings. - Introduced validation functions to check if a schedule exists and handle "not found" errors more gracefully, improving error handling in the setupWeeklyReportSchedule function. These changes enhance logging clarity and improve the robustness of schedule management.
1 parent 3eb305d commit 13ff085

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

workers/main/src/configs/schedules.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,56 @@ import { workerConfig } from './worker';
55

66
const SCHEDULE_ID = 'weekly-financial-report-schedule';
77

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+
840
/**
941
* Sets up the weekly financial report schedule
1042
* Schedule runs every Tuesday at 1 PM America/New_York time (EST/EDT)
1143
* @param client - Temporal client instance
1244
*/
1345
export async function setupWeeklyReportSchedule(client: Client): Promise<void> {
1446
try {
15-
const scheduleHandle = client.schedule.getHandle(SCHEDULE_ID);
16-
17-
// Check if schedule already exists
18-
try {
19-
await scheduleHandle.describe();
20-
logger.info(`Schedule ${SCHEDULE_ID} already exists, skipping creation`);
47+
const isScheduleExists = await validateScheduleExists(client);
2148

49+
if (isScheduleExists) {
2250
return;
23-
} catch (error) {
24-
const errorMessage =
25-
error instanceof Error ? error.message : String(error);
26-
27-
// Schedule doesn't exist, create it
28-
logger.info(
29-
`Schedule ${SCHEDULE_ID} not found, creating schedule. Reason: ${errorMessage}`,
30-
);
3151
}
3252

3353
await client.schedule.create({
3454
scheduleId: SCHEDULE_ID,
3555
spec: {
36-
cronExpressions: ['0 13 * * 2'], // Every Tuesday at 1 PM
37-
timezone: 'America/New_York', // Automatically handles EST/EDT transitions
56+
cronExpressions: ['0 13 * * 2'],
57+
timezone: 'America/New_York',
3858
},
3959
action: {
4060
type: 'startWorkflow',
@@ -43,8 +63,8 @@ export async function setupWeeklyReportSchedule(client: Client): Promise<void> {
4363
workflowId: `weekly-financial-report-scheduled`,
4464
},
4565
policies: {
46-
overlap: 'SKIP', // Skip if previous run is still in progress
47-
catchupWindow: '1 day', // Catch up missed runs within 1 day
66+
overlap: 'SKIP',
67+
catchupWindow: '1 day',
4868
},
4969
});
5070

workers/main/src/logger.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DefaultLogger } from '@temporalio/worker';
22

33
/**
44
* Shared logger instance for the worker
5-
* Using ERROR level to reduce noise in production
5+
* Using INFO level to capture important operational messages
6+
* including schedule setup, errors, and warnings
67
*/
7-
export const logger = new DefaultLogger('ERROR');
8+
export const logger = new DefaultLogger('INFO');

0 commit comments

Comments
 (0)