Skip to content

Commit

Permalink
Update to TS SDK 1.9 (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjameswh authored Jan 17, 2024
1 parent 2496f37 commit c4c25de
Show file tree
Hide file tree
Showing 76 changed files with 310 additions and 364 deletions.
8 changes: 6 additions & 2 deletions .scripts/copy-shared-files.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Run with https://github.com/google/zx
const { readFileSync } = require('fs');

const STORED_SAMPLES = new Set(require('./list-of-samples.json').samples);

const yaml = require('yaml');
Expand Down Expand Up @@ -180,8 +182,10 @@ testProjectsNode.value.items = [];
lintProjectsNode.value.items = [];

for (const sample of samples) {
const hasTestScript = !!require(`../${sample}/package.json`).scripts.test;
const hasLintScript = !!require(`../${sample}/package.json`).scripts.lint;
// Don't use require, because it won't work with ESM samples
const packageJson = JSON.parse(readFileSync(`../${sample}/package.json`));
const hasTestScript = !!packageJson.scripts.test;
const hasLintScript = !!packageJson.scripts.lint;

if (hasTestScript) {
testProjectsNode.value.items.push(sample);
Expand Down
2 changes: 1 addition & 1 deletion activities-cancellation-heartbeating/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Running [`src/client.ts`](./src/client.ts) does this:
- The `fakeProgress` activity starts and heartbeats
- Waits 40s
- Uses the handle to `cancel()` the workflow
- The activity's `Context.current().sleep` receives the cancellation signal and throws a `CancelledFailure` for you to handle
- The activity's `sleep` receives the cancellation signal and throws a `CancelledFailure` for you to handle
- Activity catches the `CancelledFailure` which you can check with `isCancellation(err)`
- Prints `Fake progress activity cancelled` in the Worker terminal
- Workflow prints `Workflow cancelled along with its activity` in the Worker terminal
Expand Down
8 changes: 4 additions & 4 deletions activities-cancellation-heartbeating/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
]
},
"dependencies": {
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4"
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0"
},
"devDependencies": {
"@tsconfig/node16": "^1.0.0",
Expand Down
5 changes: 2 additions & 3 deletions activities-cancellation-heartbeating/src/activities.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// @@@SNIPSTART typescript-activity-fake-progress
import { CancelledFailure, Context } from '@temporalio/activity';
import { activityInfo, log, sleep, CancelledFailure, heartbeat } from '@temporalio/activity';

export async function fakeProgress(sleepIntervalMs = 1000): Promise<void> {
const { log, info, sleep, heartbeat } = Context.current();
try {
// allow for resuming from heartbeat
const startingPoint = info.heartbeatDetails || 1;
const startingPoint = activityInfo().heartbeatDetails || 1;
log.info('Starting activity at progress', { startingPoint });
for (let progress = startingPoint; progress <= 100; ++progress) {
// simple utility to sleep in activity for given interval or throw if Activity is cancelled
Expand Down
8 changes: 4 additions & 4 deletions activities-dependency-injection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
]
},
"dependencies": {
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4"
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0"
},
"devDependencies": {
"@tsconfig/node16": "^1.0.0",
Expand Down
12 changes: 6 additions & 6 deletions activities-examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
]
},
"dependencies": {
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4",
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0",
"axios": "^0.26.0",
"node-fetch": "2.x"
},
"devDependencies": {
"@temporalio/nyc-test-coverage": "^1.8.4",
"@temporalio/testing": "^1.8.4",
"@temporalio/nyc-test-coverage": "^1.9.0",
"@temporalio/testing": "^1.9.0",
"@tsconfig/node16": "^1.0.0",
"@types/jest": "^27.5.1",
"@types/mocha": "8.x",
Expand Down
4 changes: 2 additions & 2 deletions activities-examples/src/activities/async-completion.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @@@SNIPSTART typescript-activity-complete-async
import { CompleteAsyncError, Context } from '@temporalio/activity';
import { CompleteAsyncError, activityInfo } from '@temporalio/activity';
import { AsyncCompletionClient } from '@temporalio/client';

export async function doSomethingAsync(): Promise<string> {
const taskToken = Context.current().info.taskToken;
const taskToken = activityInfo().taskToken;
setTimeout(() => doSomeWork(taskToken), 1000);
throw new CompleteAsyncError();
}
Expand Down
6 changes: 3 additions & 3 deletions activities-examples/src/activities/cancellable-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @@@SNIPSTART typescript-activity-cancellable-fetch
import fetch from 'node-fetch';
import { Context } from '@temporalio/activity';
import { cancellationSignal, heartbeat } from '@temporalio/activity';
import type { AbortSignal as FetchAbortSignal } from 'node-fetch/externals';

export async function cancellableFetch(url: string): Promise<Uint8Array> {
const response = await fetch(url, { signal: Context.current().cancellationSignal as FetchAbortSignal });
const response = await fetch(url, { signal: cancellationSignal() as FetchAbortSignal });
const contentLengthHeader = response.headers.get('Content-Length');
if (contentLengthHeader === null) {
throw new Error('expected Content-Length header to be set');
Expand All @@ -19,7 +19,7 @@ export async function cancellableFetch(url: string): Promise<Uint8Array> {
}
bytesRead += chunk.length;
chunks.push(chunk);
Context.current().heartbeat(bytesRead / contentLength);
heartbeat(bytesRead / contentLength);
}
return Buffer.concat(chunks);
}
Expand Down
2 changes: 1 addition & 1 deletion activities-examples/src/mocha/workflows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('example workflow', async function () {
before(async function () {
// Filter INFO log messages for clearer test output
Runtime.install({ logger: new DefaultLogger('WARN') });
const env = await TestWorkflowEnvironment.createTimeSkipping();
const env = await TestWorkflowEnvironment.createLocal();

const worker = await Worker.create(
workflowCoverage.augmentWorkerOptions({
Expand Down
2 changes: 1 addition & 1 deletion activities-examples/src/workflows.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ beforeAll(async () => {
logger: new DefaultLogger('WARN', (entry: LogEntry) => console.log(`[${entry.level}]`, entry.message)),
});

testEnv = await TestWorkflowEnvironment.createTimeSkipping();
testEnv = await TestWorkflowEnvironment.createLocal();
});

afterAll(async () => {
Expand Down
8 changes: 4 additions & 4 deletions child-workflows/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
]
},
"dependencies": {
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4"
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0"
},
"devDependencies": {
"@tsconfig/node16": "^1.0.0",
Expand Down
8 changes: 4 additions & 4 deletions continue-as-new/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
]
},
"dependencies": {
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4"
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0"
},
"devDependencies": {
"@tsconfig/node16": "^1.0.0",
Expand Down
8 changes: 4 additions & 4 deletions cron-workflows/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
]
},
"dependencies": {
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4"
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0"
},
"devDependencies": {
"@tsconfig/node16": "^1.0.0",
Expand Down
6 changes: 2 additions & 4 deletions cron-workflows/src/activities.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { Context } from '@temporalio/activity';
import { log, activityInfo } from '@temporalio/activity';

export async function logTime(name: string, wfTime: string): Promise<void> {
const { log } = Context.current();
const { workflowExecution } = Context.current().info;
// just demoing usage of Context... you can also pass in the workflowId from the workflow but this is another option
const { workflowExecution } = activityInfo();
log.info(`Hello from ${workflowExecution.workflowId}, ${name}!`, { workflowTime: wfTime, activityTime: Date.now() });
}
17 changes: 0 additions & 17 deletions custom-logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,8 @@ Sample worker output
isolateExecutionTimeout: '5s',
workflowThreadPoolSize: 2,
maxCachedWorkflows: 914,
enableSDKTracing: false,
showStackTraceSources: false,
reuseV8Context: false,
debugMode: false,
interceptors: {
activityInbound: [ [Function (anonymous)] ],
workflowModules: [
'/Users/user/samples-typescript/custom-logger/node_modules/@temporalio/worker/lib/workflow-log-interceptor.js'
]
},
sinks: {
defaultWorkerLogger: {
trace: { fn: [Function: fn] },
debug: { fn: [Function: fn] },
info: { fn: [Function: fn] },
warn: { fn: [Function: fn] },
error: { fn: [Function: fn] }
}
},
workflowsPath: '/Users/user/samples-typescript/custom-logger/src/workflows/index.ts',
activities: { greet: [AsyncFunction: greet] },
taskQueue: 'custom-logger',
Expand Down
8 changes: 4 additions & 4 deletions custom-logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
]
},
"dependencies": {
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4",
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0",
"nanoid": "3.x",
"triple-beam": "^1.3.0",
"winston": "^3.3.3"
Expand Down
3 changes: 1 addition & 2 deletions custom-logger/src/activities/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Context } from '@temporalio/activity';
import { log } from '@temporalio/activity';

// @@@SNIPSTART typescript-activity-use-injected-logger
export async function greet(name: string): Promise<string> {
const { log } = Context.current();
log.info('Log from activity', { name });
return `Hello, ${name}!`;
}
Expand Down
8 changes: 4 additions & 4 deletions dsl-interpreter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
]
},
"dependencies": {
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4",
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0",
"js-yaml": "^4.1.0"
},
"devDependencies": {
Expand Down
5 changes: 1 addition & 4 deletions dsl-interpreter/src/activities.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Context } from '@temporalio/activity';
import { log } from '@temporalio/activity';

export async function activity1(arg1: string): Promise<string> {
const { log } = Context.current();
if (!arg1) throw new Error('arg1 is required');
log.info('Executing activity1', { arg1 });
return `[result from activity1: ${arg1}]`;
}
export async function activity2(arg: string): Promise<string> {
const { log } = Context.current();
if (!arg) throw new Error('arg is required');
log.info('Executing activity2', { arg });
return `[result from activity2: ${arg}]`;
Expand All @@ -20,7 +18,6 @@ export async function activity3(arg2: string, arg: string): Promise<string> {
}

export async function activity4(result1: string): Promise<string> {
const { log } = Context.current();
if (!result1) throw new Error('result1 is required');
log.info('Executing activity4', { result1 });
return `[result from activity4: ${result1}]`;
Expand Down
10 changes: 5 additions & 5 deletions ejson/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
]
},
"dependencies": {
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/common": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4",
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/common": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0",
"ejson": "^2.2.2",
"uuid": "^8.3.2"
},
Expand Down
8 changes: 0 additions & 8 deletions empty/.post-create
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ Use Node version 16+:
Mac: {cyan brew install node@16}
Other: https://nodejs.org/en/download/

Then, in the project directory:

* Add your Activity Definitions to `src/activities.ts`.
* Add your Workflow Definitions to `src/workflows.ts`.
* Set your task queue name in `src/shared.ts`.
* Modify the `src/client.ts` file and replace `YOUR_WORKFLOW` with the name of your Workflow.
* Optionally, add Activity and Workflow tests to the `src/mocha` directory in files with the extension `.test.ts`.

Then, in the project directory, using two other shells, run these commands:

{cyan npm run start.watch}
Expand Down
12 changes: 6 additions & 6 deletions empty/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "temporal-hello-world",
"name": "empty",
"version": "0.1.0",
"private": true,
"scripts": {
Expand All @@ -22,14 +22,14 @@
]
},
"dependencies": {
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4",
"@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.8.4",
"@temporalio/testing": "^1.9.0",
"@tsconfig/node16": "^1.0.0",
"@types/mocha": "8.x",
"@types/node": "^16.11.43",
Expand Down
2 changes: 0 additions & 2 deletions empty/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"extends": "@tsconfig/node16/tsconfig.json",
"version": "4.4.2",
"compilerOptions": {
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
Expand Down
10 changes: 5 additions & 5 deletions encryption/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
},
"dependencies": {
"@ronomon/crypto-async": "^5.0.1",
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/common": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4",
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/common": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0",
"cors": "^2.8.5",
"ejson": "^2.2.2",
"express": "^4.18.0",
Expand Down
8 changes: 4 additions & 4 deletions expense/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
]
},
"dependencies": {
"@temporalio/activity": "^1.8.4",
"@temporalio/client": "^1.8.4",
"@temporalio/worker": "^1.8.4",
"@temporalio/workflow": "^1.8.4",
"@temporalio/activity": "^1.9.0",
"@temporalio/client": "^1.9.0",
"@temporalio/worker": "^1.9.0",
"@temporalio/workflow": "^1.9.0",
"axios": "^0.26.0",
"express": "~4.17.1",
"uuid": "8.3.2"
Expand Down
Loading

0 comments on commit c4c25de

Please sign in to comment.