-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7283b47
commit b4b992c
Showing
7 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |