Skip to content

Commit

Permalink
Update sample
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Jan 30, 2024
1 parent 7283b47 commit b4b992c
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
1 change: 1 addition & 0 deletions .scripts/list-of-samples.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"state",
"timer-examples",
"timer-progress",
"update",
"vscode-debugger",
"worker-specific-task-queues",
"worker-versioning"
Expand Down
13 changes: 13 additions & 0 deletions update/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Update Sample

This workflow represents a counter that can be mutated via an update called
`fetchAndAdd`. The update adds its argument to the counter and returns the
previous value of the counter. The update validator is used to reject negative
arguments (rejected updates are not included in workflow history).

### Running this sample

1. `temporal server start-dev` to start [Temporal Server](https://github.com/temporalio/cli/#installation).
1. `npm install` to install dependencies.
1. `npm run start.watch` to start the Worker.
1. In another shell, `npm run workflow` to run the Workflow Client.
47 changes: 47 additions & 0 deletions update/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "temporal-update",
"version": "0.1.0",
"private": true,
"scripts": {
"build": "tsc --build",
"build.watch": "tsc --build --watch",
"lint": "eslint .",
"start": "ts-node src/worker.ts",
"start.watch": "nodemon src/worker.ts",
"workflow": "ts-node src/client.ts",
"format": "prettier --config .prettierrc 'src/**/*.ts' --write",
"test": "mocha --exit --require ts-node/register --require source-map-support/register src/mocha/*.test.ts"
},
"nodemonConfig": {
"execMap": {
"ts": "ts-node"
},
"ext": "ts",
"watch": [
"src"
]
},
"dependencies": {
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0",
"nanoid": "3.x"
},
"devDependencies": {
"@temporalio/testing": "^1.9.0",
"@tsconfig/node16": "^1.0.0",
"@types/mocha": "8.x",
"@types/node": "^16.11.43",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-deprecation": "^1.2.1",
"mocha": "8.x",
"nodemon": "^2.0.12",
"prettier": "^2.8.8",
"ts-node": "^10.8.1",
"typescript": "^4.4.2"
}
}
37 changes: 37 additions & 0 deletions update/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @@@SNIPSTART typescript-update-client
import { Connection, Client } from '@temporalio/client';
import { counter, fetchAndAdd, done } from './workflows';
import { nanoid } from 'nanoid';

async function run() {
const connection = await Connection.connect({ address: 'localhost:7233' });
const client = new Client({ connection });

const handle = await client.workflow.start(counter, {
taskQueue: 'my-task-queue',
args: [],
workflowId: 'workflow-' + nanoid(),
});
console.log(`Started workflow ${handle.workflowId}`);

const prevValue = await handle.executeUpdate(fetchAndAdd, { args: [1] });
console.log(`incrementing counter; previous value was ${prevValue}`);

const invalidArg = -1;
try {
await handle.executeUpdate(fetchAndAdd, { args: [invalidArg] });
} catch (error) {
console.log(`Update argument ${invalidArg} was rejected: ${error}`);
}

// Use the signal to allow the workflow to complete.
await handle.signal(done);

console.log(`Final counter value is ${await handle.result()}`);
}

run().catch((err) => {
console.error(err);
process.exit(1);
});
// @@@SNIPEND
21 changes: 21 additions & 0 deletions update/src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @@@SNIPSTART typescript-hello-worker
import { NativeConnection, Worker } from '@temporalio/worker';

async function run() {
const connection = await NativeConnection.connect({
address: 'localhost:7233',
});
const worker = await Worker.create({
connection,
namespace: 'default',
taskQueue: 'my-task-queue',
workflowsPath: require.resolve('./workflows'),
});
await worker.run();
}

run().catch((err) => {
console.error(err);
process.exit(1);
});
// @@@SNIPEND
31 changes: 31 additions & 0 deletions update/src/workflows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @@@SNIPSTART typescript-update-workflow
import * as wf from '@temporalio/workflow';

export const fetchAndAdd = wf.defineUpdate<number, [number]>('fetchAndAdd');
export const done = wf.defineSignal('done');

export async function counter(): Promise<number> {
let count = 0;
let shouldExit = false;

const validator = (arg: number) => {
if (arg < 0) {
throw new Error('Argument must not be negative');
}
};

const handler = (arg: number) => {
const prevCount = count;
count += arg;
return prevCount;
};

wf.setHandler(fetchAndAdd, handler, { validator });
wf.setHandler(done, () => {
shouldExit = true;
});

await wf.condition(() => shouldExit);
return count;
}
// @@@SNIPEND
12 changes: 12 additions & 0 deletions update/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@tsconfig/node16/tsconfig.json",
"version": "4.4.2",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"rootDir": "./src",
"outDir": "./lib"
},
"include": ["src/**/*.ts"]
}

0 comments on commit b4b992c

Please sign in to comment.