Skip to content

Commit 1f54636

Browse files
committed
add infrequent polling sample, with benign application failure
1 parent 6123449 commit 1f54636

19 files changed

Lines changed: 334 additions & 0 deletions

.scripts/list-of-samples.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"nestjs-exchange-rates",
2727
"nextjs-ecommerce-oneclick",
2828
"patching-api",
29+
"polling",
2930
"production",
3031
"protobufs",
3132
"query-subscriptions",

polling/infrequent/.eslintignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
coverage
3+
jest.config.js
4+
lib
5+
.eslintrc.js

polling/infrequent/.eslintrc.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const { builtinModules } = require('module');
2+
3+
const ALLOWED_NODE_BUILTINS = new Set(['assert']);
4+
5+
module.exports = {
6+
root: true,
7+
parser: '@typescript-eslint/parser',
8+
parserOptions: {
9+
project: './tsconfig.json',
10+
tsconfigRootDir: __dirname,
11+
},
12+
plugins: ['@typescript-eslint', 'deprecation'],
13+
extends: [
14+
'eslint:recommended',
15+
'plugin:@typescript-eslint/eslint-recommended',
16+
'plugin:@typescript-eslint/recommended',
17+
'prettier',
18+
],
19+
rules: {
20+
// recommended for safety
21+
'@typescript-eslint/no-floating-promises': 'error', // forgetting to await Activities and Workflow APIs is bad
22+
'deprecation/deprecation': 'warn',
23+
24+
// code style preference
25+
'object-shorthand': ['error', 'always'],
26+
27+
// relaxed rules, for convenience
28+
'@typescript-eslint/no-unused-vars': [
29+
'warn',
30+
{
31+
argsIgnorePattern: '^_',
32+
varsIgnorePattern: '^_',
33+
},
34+
],
35+
'@typescript-eslint/no-explicit-any': 'off',
36+
},
37+
overrides: [
38+
{
39+
files: ['src/workflows.ts', 'src/workflows-*.ts', 'src/workflows/*.ts'],
40+
rules: {
41+
'no-restricted-imports': [
42+
'error',
43+
...builtinModules.filter((m) => !ALLOWED_NODE_BUILTINS.has(m)).flatMap((m) => [m, `node:${m}`]),
44+
],
45+
},
46+
},
47+
],
48+
};

polling/infrequent/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib
2+
node_modules

polling/infrequent/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

polling/infrequent/.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
22

polling/infrequent/.post-create

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
To begin development, install the Temporal CLI:
2+
3+
Mac: {cyan brew install temporal}
4+
Other: Download and extract the latest release from https://github.com/temporalio/cli/releases/latest
5+
6+
Start Temporal Server:
7+
8+
{cyan temporal server start-dev}
9+
10+
Use Node version 18+ (v22.x is recommended):
11+
12+
Mac: {cyan brew install node@22}
13+
Other: https://nodejs.org/en/download/
14+
15+
Then, in the project directory, using two other shells, run these commands:
16+
17+
{cyan npm run start.watch}
18+
{cyan npm run workflow}

polling/infrequent/.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
lib

polling/infrequent/.prettierrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
printWidth: 120
2+
singleQuote: true

polling/infrequent/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Frequently Polling Activity
2+
3+
This sample shows how we can implement frequent polling (1 second or faster) inside our Activity. The implementation is a loop that polls our service and then sleeps for the poll interval (1 second in the sample).
4+
5+
To ensure that polling Activity is restarted in a timely manner, we make sure that it heartbeats on every iteration. Note that heartbeating only works if we set the `heartbeat_timeout` to a shorter value than the Activity `start_to_close_timeout` timeout.
6+
7+
To run, first see [README.md](../../README.md) for prerequisites.
8+
9+
The Workflow will continue to poll the service and heartbeat on every iteration until it succeeds.
10+
11+
Note that with frequent polling, the Activity may execute for a long time, and it may be beneficial to set a Timeout on the Activity to avoid long-running Activities that are not heartbeating.
12+
13+
If the polling interval needs to be changed during runtime, the Activity needs to be canceled and a new instance with the updated arguments needs to be started.
14+
15+
### Testing
16+
17+
- Jest: `npm run test`:
18+
- [`src/test/workflows.test.ts`](./src/test/workflows.test.ts)
19+
20+
### Running this sample
21+
22+
1. `temporal server start-dev` to start [Temporal Server](https://github.com/temporalio/cli/#installation).
23+
2. `npm install` to install dependencies.
24+
3. `npm run start.watch` to start the Worker.
25+
4. In another shell, `npm run workflow` to run the Workflow Client.

0 commit comments

Comments
 (0)