Skip to content

Commit e9c976e

Browse files
authored
fix: Do't trigger conditions for query jobs (#854)
1 parent 682003e commit e9c976e

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

packages/test/src/integration-tests.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,4 +1301,16 @@ export function runIntegrationTests(codec?: PayloadCodec): void {
13011301
// Verify only one timer was scheduled
13021302
t.is(history.events.filter(({ timerStartedEventAttributes }) => timerStartedEventAttributes != null).length, 1);
13031303
});
1304+
1305+
test('Query does not cause condition to be triggered', async (t) => {
1306+
const { client } = t.context;
1307+
const workflowId = uuid4();
1308+
const handle = await client.start(workflows.queryAndCondition, {
1309+
taskQueue: 'test',
1310+
workflowId,
1311+
});
1312+
await handle.query(workflows.mutateWorkflowStateQuery);
1313+
// Worker did not crash
1314+
t.pass();
1315+
});
13041316
}

packages/test/src/workflows/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export * from './patched-top-level';
5656
export * from './promise-all';
5757
export * from './promise-race';
5858
export * from './promise-then-promise';
59+
export * from './query-and-condition';
5960
export * from './race';
6061
export * from './random';
6162
export * from './reject-promise';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as wf from '@temporalio/workflow';
2+
3+
export const mutateWorkflowStateQuery = wf.defineQuery<void>('mutateWorkflowState');
4+
5+
export async function queryAndCondition(): Promise<void> {
6+
let mutated = false;
7+
// Not a valid query, used to verify that condition isn't triggered for query jobs
8+
wf.setHandler(mutateWorkflowStateQuery, () => void (mutated = true));
9+
await wf.condition(() => mutated);
10+
}

packages/worker/src/workflow/vm.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,10 @@ export class VMWorkflow implements Workflow {
397397
coresdk.workflow_activation.WorkflowActivation.fromObject({ ...activation, jobs }),
398398
batchIndex++
399399
);
400-
await this.tryUnblockConditions();
400+
// Only trigger conditions for non-query jobs
401+
if (!jobs[0].queryWorkflow) {
402+
await this.tryUnblockConditions();
403+
}
401404
}
402405
const completion = this.workflowModule.concludeActivation();
403406
// Give unhandledRejection handler a chance to be triggered.

packages/workflow/src/worker-interface.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ export function activate(activation: coresdk.workflow_activation.WorkflowActivat
222222
return;
223223
}
224224
state.activator[job.variant](variant as any /* TS can't infer this type */);
225-
tryUnblockConditions();
225+
// Only trigger conditions for non-query jobs
226+
if (!job.queryWorkflow) {
227+
tryUnblockConditions();
228+
}
226229
}
227230
});
228231
intercept({

0 commit comments

Comments
 (0)