From 755cd9f2789d24af7a946343a9c20d9bfe9e1d72 Mon Sep 17 00:00:00 2001 From: kowshikdev Date: Wed, 2 Dec 2026 23:55:20 +0530 Subject: [PATCH] fix(schedules): compute the next run from the schedule's own block After a run, the next run time used the timezone of the first schedule block in the workflow, so a second schedule in another timezone shifted after its first run. Co-Authored-By: Claude Opus 5.5 --- .../async-preprocessing-correlation.test.ts | 52 +++++++++++++++++++ apps/sim/background/schedule-execution.ts | 9 ++-- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/apps/sim/background/async-preprocessing-correlation.test.ts b/apps/sim/background/async-preprocessing-correlation.test.ts index 74f64da1bd8..b52efccd50d 100644 --- a/apps/sim/background/async-preprocessing-correlation.test.ts +++ b/apps/sim/background/async-preprocessing-correlation.test.ts @@ -862,4 +862,56 @@ describe('async preprocessing correlation threading', () => { }) ) }) + + it('advances each schedule in the timezone of its own block when a workflow has several', async () => { + mockLoadDeployedWorkflowState.mockResolvedValue({ + blocks: { + 'utc-block': { type: 'schedule', subBlocks: { timezone: { value: 'UTC' } } }, + 'tokyo-block': { type: 'schedule', subBlocks: { timezone: { value: 'Asia/Tokyo' } } }, + }, + edges: [], + loops: {}, + parallels: {}, + deploymentVersionId: 'deployment-1', + }) + mockGetScheduleTimeValues.mockImplementation((block) => ({ + timezone: block.subBlocks.timezone.value, + })) + mockPreprocessExecution.mockResolvedValueOnce({ + success: true, + actorUserId: 'actor-1', + workflowRecord: { + id: 'workflow-1', + userId: 'owner-1', + workspaceId: 'workspace-1', + variables: {}, + }, + billingAttribution, + executionTimeout: {}, + }) + mockExecuteWorkflowCore.mockResolvedValueOnce({ + success: true, + status: 'completed', + output: {}, + metadata: { duration: 10, userId: 'actor-1' }, + }) + + await executeScheduleJob({ + scheduleId: 'schedule-1', + workflowId: 'workflow-1', + workspaceId: 'workspace-1', + billingAttribution, + executionId: 'execution-own-timezone', + requestId: 'request-own-timezone', + blockId: 'tokyo-block', + cronExpression: '0 9 * * *', + timezone: 'Asia/Tokyo', + now: '2025-01-01T00:00:00.000Z', + scheduledFor: '2025-01-01T00:00:00.000Z', + }) + + const update = dbChainMockFns.set.mock.calls.at(-1)?.[0] + expect(update.failedCount).toBe(0) + expect(update.nextRunAt.getUTCHours()).toBe(0) + }) }) diff --git a/apps/sim/background/schedule-execution.ts b/apps/sim/background/schedule-execution.ts index 3b4b1202e41..67d423e63e0 100644 --- a/apps/sim/background/schedule-execution.ts +++ b/apps/sim/background/schedule-execution.ts @@ -859,12 +859,13 @@ export type ScheduleExecutionPayload = { } function calculateNextRunTime( - schedule: { cronExpression?: string; lastRanAt?: string }, + schedule: { blockId?: string; cronExpression?: string; lastRanAt?: string }, blocks: Record ): Date { - const scheduleBlock = Object.values(blocks).find( - (block) => block.type === 'starter' || block.type === 'schedule' - ) + const ownBlock = schedule.blockId ? blocks[schedule.blockId] : undefined + const scheduleBlock = + ownBlock ?? + Object.values(blocks).find((block) => block.type === 'starter' || block.type === 'schedule') if (!scheduleBlock) throw new Error('No starter or schedule block found') const scheduleType = getSubBlockValue(scheduleBlock, 'scheduleType') const scheduleValues = getScheduleTimeValues(scheduleBlock)