Skip to content
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

feat(workflow): add upsert memo command #1321

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/test/src/integration-tests-old.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,20 @@ export function runIntegrationTests(codec?: PayloadCodec): void {
);
});

test('Workflow can upsert memo', async (t) => {
const { client } = t.context;
const workflow = await client.start(workflows.upsertAndReadMemo, {
taskQueue: 'test',
workflowId: uuid4(),
args: [{ note2: 'bar' }],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the newer test-integration-workflows.ts and test adding, updating and removing.

});
const result = await workflow.result();
t.deepEqual(result, {
note: 'foo',
note2: 'bar',
});
});

test('Workflow can read WorkflowInfo', async (t) => {
const { client } = t.context;
const workflowId = uuid4();
Expand Down
38 changes: 38 additions & 0 deletions packages/test/src/test-sinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,44 @@ if (RUN_INTEGRATION_TESTS) {
]);
});

test('Sink functions contains upserted memo', async (t) => {
const taskQueue = `${__filename}-${t.title}`;

const recordedMessages = Array<{ message: string; memo: Record<string, unknown> | undefined }>();
const sinks = asSdkLoggerSink(async (info, message, _attrs) => {
recordedMessages.push({
message,
memo: info.memo,
});
});

const client = new WorkflowClient();

const worker = await Worker.create({
...defaultOptions,
taskQueue,
sinks,
});
await worker.runUntil(
client.execute(workflows.upsertAndReadMemo, {
taskQueue,
workflowId: uuid4(),
args: [{ note: 'foo' }],
})
);

t.deepEqual(recordedMessages, [
{
message: 'Workflow started',
memo: undefined,
},
{
message: 'Workflow completed',
memo: { note: 'foo' },
},
]);
});

test('Core issue 589', async (t) => {
const taskQueue = `${__filename}-${t.title}`;

Expand Down
1 change: 1 addition & 0 deletions packages/test/src/workflows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,4 @@ export * from './upsert-and-read-search-attributes';
export * from './url-whatwg';
export * from './wait-on-user';
export * from './workflow-cancellation-scenarios';
export * from './upsert-and-read-memo';
6 changes: 6 additions & 0 deletions packages/test/src/workflows/upsert-and-read-memo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { upsertMemo, workflowInfo } from '@temporalio/workflow';

export async function upsertAndReadMemo(memo: Record<string, unknown>): Promise<Record<string, unknown> | undefined> {
upsertMemo(memo);
return workflowInfo().memo;
}
50 changes: 50 additions & 0 deletions packages/workflow/src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,56 @@ export function upsertSearchAttributes(searchAttributes: SearchAttributes): void
});
}

/**
* Updates this Workflow's Memo by merging the provided `memo` with the existing Memo,
* `workflowInfo().memo`.
*
* For example, this Workflow code:
*
* ```ts
* upsertMemo({
* key1: value,
* });
* upsertMemo({
* key2: value,
* });
* ```
*
* would result in the Workflow having these Memo:
*
* ```ts
* {
* key1: value,
* key2: value,
* }
* ```
*
* @param memo The Record to merge. Use a value of `null` to clear a key from the Memo.
*/
export function upsertMemo(memo: Record<string, unknown>): void {
const activator = assertInWorkflowContext('Workflow.upsertMemo(...) may only be used from a Workflow Execution.');

if (memo == null) {
throw new Error('memo must be a non-null Record');
}

activator.pushCommand({
modifyWorkflowProperties: {
upsertedMemo: mapToPayloads(activator.payloadConverter, memo),
},
});

activator.mutateWorkflowInfo((info: WorkflowInfo): WorkflowInfo => {
return {
...info,
memo: {
...info.memo,
...memo,
},
};
});
}

export const stackTraceQuery = defineQuery<string>('__stack_trace');
export const enhancedStackTraceQuery = defineQuery<EnhancedStackTrace>('__enhanced_stack_trace');
export const workflowMetadataQuery = defineQuery<temporal.api.sdk.v1.IWorkflowMetadata>('__temporal_workflow_metadata');
Loading