@polarityio/integration-types 3.1.1 → 3.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/context.d.ts +10 -6
- package/dist/limiter.d.ts +21 -13
- package/dist/polling.d.ts +92 -46
- package/package.json +4 -4
package/dist/context.d.ts
CHANGED
|
@@ -264,7 +264,9 @@ export interface IntegrationContext {
|
|
|
264
264
|
/**
|
|
265
265
|
* Starts a named poller that was defined in the integration's `poll` export.
|
|
266
266
|
*
|
|
267
|
-
*
|
|
267
|
+
* Acts only on the pod (Polarity server instance) that handles this call: other
|
|
268
|
+
* pods keep their own pollers as they are. If the poller is already scheduled on
|
|
269
|
+
* this pod, for example because it has `autostart: true`, this call is a no-op.
|
|
268
270
|
*
|
|
269
271
|
* @param pollName - The key from the integration's `poll` record identifying
|
|
270
272
|
* which poller to start.
|
|
@@ -272,9 +274,9 @@ export interface IntegrationContext {
|
|
|
272
274
|
* immediately before the first scheduled interval or cron tick. When `false`,
|
|
273
275
|
* the first execution waits for the next scheduled tick.
|
|
274
276
|
*
|
|
275
|
-
* @example Start a poller and fire immediately
|
|
277
|
+
* @example Start a poller defined with `autostart: false`, and fire immediately
|
|
276
278
|
* ```typescript
|
|
277
|
-
* context.startPolling('
|
|
279
|
+
* context.startPolling('backfill');
|
|
278
280
|
* ```
|
|
279
281
|
*
|
|
280
282
|
* @example Start a poller but wait for the first scheduled tick
|
|
@@ -286,15 +288,17 @@ export interface IntegrationContext {
|
|
|
286
288
|
/**
|
|
287
289
|
* Stops a named poller that is currently running.
|
|
288
290
|
*
|
|
289
|
-
*
|
|
290
|
-
*
|
|
291
|
+
* Acts only on the pod (Polarity server instance) that handles this call: other
|
|
292
|
+
* pods keep polling, and every pod starts its `autostart` pollers again the next
|
|
293
|
+
* time the integration loads. If the poller is not running, this call is a no-op.
|
|
294
|
+
* A stopped poller can be restarted later via `startPolling()`.
|
|
291
295
|
*
|
|
292
296
|
* @param pollName - The key from the integration's `poll` record identifying
|
|
293
297
|
* which poller to stop.
|
|
294
298
|
*
|
|
295
299
|
* @example
|
|
296
300
|
* ```typescript
|
|
297
|
-
* context.stopPolling('
|
|
301
|
+
* context.stopPolling('refreshFeed');
|
|
298
302
|
* ```
|
|
299
303
|
*/
|
|
300
304
|
stopPolling: (pollName: string) => void;
|
package/dist/limiter.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Strategy for handling jobs when `highWater` is reached.
|
|
3
|
-
*
|
|
4
|
-
* - `
|
|
5
|
-
*
|
|
6
|
-
*
|
|
2
|
+
* Strategy for handling jobs when `highWater` is reached (Bottleneck's numbering).
|
|
3
|
+
* Priority runs from 0 (highest) to 9 (lowest); see {@link ScheduleOptions.priority}.
|
|
4
|
+
* - `1` (LEAK) — Drop the oldest job of the lowest priority queued, among jobs with
|
|
5
|
+
* the same or a lower priority than the incoming one. If every queued job has a
|
|
6
|
+
* higher priority, the incoming job is dropped instead.
|
|
7
|
+
* - `2` (OVERFLOW) — Drop the incoming job (reject it immediately). The platform default.
|
|
8
|
+
* - `3` (BLOCK) — Drop every queued job and reject new ones until the penalty
|
|
9
|
+
* period has passed with no new jobs. The penalty defaults to 15 × `minTime`
|
|
10
|
+
* (1.5 s with the platform's `minTime` of 100), or 5 s when `minTime` is 0.
|
|
11
|
+
* - `4` (OVERFLOW_PRIORITY) — Like LEAK, but only among jobs with a strictly lower
|
|
12
|
+
* priority than the incoming one. If there is none, the incoming job is dropped
|
|
13
|
+
* instead, so a job never displaces one of equal priority.
|
|
7
14
|
*/
|
|
8
15
|
export type ThrottleStrategy = 1 | 2 | 3 | 4;
|
|
9
16
|
/**
|
|
@@ -11,13 +18,17 @@ export type ThrottleStrategy = 1 | 2 | 3 | 4;
|
|
|
11
18
|
* job priority and identification within the throttle queue.
|
|
12
19
|
*/
|
|
13
20
|
export interface ScheduleOptions {
|
|
14
|
-
/** Priority of the job (
|
|
21
|
+
/** Priority of the job (0 = highest, 9 = lowest). Default is 5. */
|
|
15
22
|
priority?: number;
|
|
16
23
|
/** Optional identifier for the scheduled job (useful for debugging). */
|
|
17
24
|
id?: string;
|
|
18
25
|
/** Weight of the job against the `maxConcurrent` limit. Default is 1. */
|
|
19
26
|
weight?: number;
|
|
20
|
-
/**
|
|
27
|
+
/**
|
|
28
|
+
* Maximum time (ms) a job may **run** once it has started. A job still running
|
|
29
|
+
* after this time is failed with a `BottleneckError`. It does not limit time spent
|
|
30
|
+
* waiting in the queue: Bottleneck cannot remove a queued job.
|
|
31
|
+
*/
|
|
21
32
|
expiration?: number | null;
|
|
22
33
|
}
|
|
23
34
|
/**
|
|
@@ -57,12 +68,9 @@ export interface ThrottleSettings {
|
|
|
57
68
|
*/
|
|
58
69
|
highWater?: number;
|
|
59
70
|
/**
|
|
60
|
-
* Strategy for handling jobs when `highWater` is reached
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* - `3` (OVERFLOW): Drop the incoming job (reject it immediately).
|
|
64
|
-
* - `4` (BLOCK): Block until a slot opens.
|
|
65
|
-
* @default 3 (OVERFLOW)
|
|
71
|
+
* Strategy for handling jobs when `highWater` is reached: `1` LEAK, `2` OVERFLOW,
|
|
72
|
+
* `3` BLOCK or `4` OVERFLOW_PRIORITY. See {@link ThrottleStrategy} for what each does.
|
|
73
|
+
* @default 2 (OVERFLOW)
|
|
66
74
|
*/
|
|
67
75
|
strategy?: ThrottleStrategy;
|
|
68
76
|
/**
|
package/dist/polling.d.ts
CHANGED
|
@@ -9,15 +9,19 @@ import type { IntegrationContext } from './context';
|
|
|
9
9
|
*
|
|
10
10
|
* ### Execution Model
|
|
11
11
|
*
|
|
12
|
-
*
|
|
13
|
-
* integration
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
12
|
+
* Each poller is scheduled **once per Polarity server instance (pod)**, not once per
|
|
13
|
+
* integration worker. When several pods run, each pod runs its own copy of every
|
|
14
|
+
* poller, on its own schedule. During a rolling deploy, the old and new pods both run
|
|
15
|
+
* it for a short time. Poll functions must therefore be safe to run concurrently on
|
|
16
|
+
* different pods (for example, idempotent writes to `context.cache`).
|
|
17
17
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
18
|
+
* Each run checks out a worker from the same pool that serves `doLookup`, `onMessage`
|
|
19
|
+
* and `onDetails`, and holds it until the run finishes. A long poll reduces the
|
|
20
|
+
* capacity available for lookups, so keep runs short and set `timeout` on the spec
|
|
21
|
+
* (interval or cron) for heavy work.
|
|
22
|
+
*
|
|
23
|
+
* Poll functions do not share in-memory state with dispatch handlers. Use
|
|
24
|
+
* `context.cache` (or local storage) to exchange data between them.
|
|
21
25
|
*
|
|
22
26
|
* @param options - Admin-only (global) user options for the integration.
|
|
23
27
|
* @param context - The integration context providing cache, logger, limiter, and network utilities.
|
|
@@ -26,12 +30,26 @@ export type PollFunction = (options: DoLookupUserOptions, context: IntegrationCo
|
|
|
26
30
|
/**
|
|
27
31
|
* Strategy the runtime uses when a poll function throws an unhandled error.
|
|
28
32
|
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
33
|
+
* Always set `failureStrategy` explicitly: the default, `'retry'`, suits few polls.
|
|
34
|
+
*
|
|
35
|
+
* - `'retry'` (the default) — The runtime retries the failed run with a linear
|
|
36
|
+
* backoff (2 s, 4 s, 6 s, …) and returns to the normal schedule once a run
|
|
37
|
+
* succeeds. There is no retry limit, so a persistent failure (invalid
|
|
38
|
+
* credentials, an unreachable host) keeps retrying instead of waiting for the next
|
|
39
|
+
* scheduled run. A run that exceeds its `timeout` counts as a failure, so a poll
|
|
40
|
+
* that always runs too long holds a worker for its full timeout on every attempt.
|
|
41
|
+
* The one exception: when no worker is free, the runtime gives up after 5
|
|
42
|
+
* attempts and waits for the next scheduled run. A poll with no next run
|
|
43
|
+
* (`runImmediately: true` with `autostart: false`) then does not run again, and
|
|
44
|
+
* no error is shown. Use `'retry'` only for work that is expected to succeed on a
|
|
45
|
+
* later attempt, such as the one-time initialization shown on {@link Poll}.
|
|
31
46
|
* - `'shutdown'` — The runtime stops the integration entirely and surfaces the
|
|
32
47
|
* error to the Polarity frontend. Use for unrecoverable failures where continued
|
|
33
48
|
* polling would be harmful.
|
|
34
|
-
* - `'skip'` — The runtime
|
|
49
|
+
* - `'skip'` — The runtime records the error and continues polling on its normal
|
|
50
|
+
* schedule (no retry is performed for the failed run). The right choice for most
|
|
51
|
+
* recurring polls, such as refreshing a feed: a failed run keeps the current data
|
|
52
|
+
* and the next scheduled run tries again.
|
|
35
53
|
*/
|
|
36
54
|
export type PollFailureStrategy = 'retry' | 'shutdown' | 'skip';
|
|
37
55
|
/**
|
|
@@ -45,29 +63,33 @@ interface BasePollSpec {
|
|
|
45
63
|
*/
|
|
46
64
|
function: PollFunction;
|
|
47
65
|
/**
|
|
48
|
-
* Whether to
|
|
66
|
+
* Whether to schedule this poller automatically when the integration is loaded
|
|
49
67
|
* by the runtime.
|
|
50
68
|
*
|
|
51
|
-
* - `true` —
|
|
52
|
-
*
|
|
53
|
-
* - `false` (default) —
|
|
54
|
-
* `context.startPolling(pollName)`.
|
|
69
|
+
* - `true` — The poller is scheduled after the integration's `startup` hook
|
|
70
|
+
* completes.
|
|
71
|
+
* - `false` (default) — The poller is not scheduled until it is started via
|
|
72
|
+
* `context.startPolling(pollName)`. If `runImmediately` is `true`, it still
|
|
73
|
+
* runs once on load (see `runImmediately`).
|
|
55
74
|
*
|
|
56
75
|
* @default false
|
|
57
76
|
*/
|
|
58
77
|
autostart?: boolean;
|
|
59
78
|
/**
|
|
60
|
-
* Whether to
|
|
61
|
-
*
|
|
79
|
+
* Whether to run the poll function once when the integration loads, before the
|
|
80
|
+
* first interval tick or cron trigger.
|
|
62
81
|
*
|
|
63
|
-
* - `true` — The
|
|
64
|
-
* continues on its
|
|
65
|
-
*
|
|
66
|
-
*
|
|
82
|
+
* - `true` — The runtime runs the poll on load, whatever `autostart` is:
|
|
83
|
+
* - With `autostart: true`, it then continues on its schedule.
|
|
84
|
+
* - With `autostart: false`, it runs once and is **not scheduled** afterwards.
|
|
85
|
+
* It runs on a schedule only after `context.startPolling()` is called. Don't
|
|
86
|
+
* use this for one-time initialization: if the run fails for lack of a free
|
|
87
|
+
* worker, the poll does not run again. Use the pattern on {@link Poll}
|
|
88
|
+
* instead.
|
|
89
|
+
* - `false` (default) — The first run happens on the first interval or cron tick.
|
|
67
90
|
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* `runImmediately` parameter takes precedence over this spec-level value.
|
|
91
|
+
* When a poller is started manually via `context.startPolling()`, the method's
|
|
92
|
+
* own `runImmediately` parameter takes precedence over this spec-level value.
|
|
71
93
|
*
|
|
72
94
|
* @default false
|
|
73
95
|
*/
|
|
@@ -75,10 +97,30 @@ interface BasePollSpec {
|
|
|
75
97
|
/**
|
|
76
98
|
* Strategy to use when the poll function throws an unhandled error.
|
|
77
99
|
*
|
|
100
|
+
* **Always set this explicitly.** For a recurring poll, such as refreshing a feed,
|
|
101
|
+
* set `'skip'`. See {@link PollFailureStrategy} for why the `'retry'` default suits
|
|
102
|
+
* few polls.
|
|
103
|
+
*
|
|
78
104
|
* @default 'retry'
|
|
79
105
|
* @see {@link PollFailureStrategy}
|
|
80
106
|
*/
|
|
81
107
|
failureStrategy?: PollFailureStrategy;
|
|
108
|
+
/**
|
|
109
|
+
* Maximum run time for one invocation, in **seconds**.
|
|
110
|
+
*
|
|
111
|
+
* Poll functions often do heavy work (large downloads, database rebuilds) that
|
|
112
|
+
* takes longer than the standard request timeout, which applies when this is not
|
|
113
|
+
* set. Values above 300 seconds (5 minutes) are clamped to 300, because a running
|
|
114
|
+
* poll holds a worker from the shared pool. A value of 0 or less, or one that is not
|
|
115
|
+
* a number, is ignored and the standard request timeout applies.
|
|
116
|
+
*
|
|
117
|
+
* The timeout covers the whole poll function, not each HTTP request in it. A run
|
|
118
|
+
* that exceeds it counts as a failure and is handled by `failureStrategy` (see
|
|
119
|
+
* {@link PollFailureStrategy}). When a run times out, the runtime stops waiting,
|
|
120
|
+
* but requests the function started keep running until they settle, so give each
|
|
121
|
+
* request its own timeout too.
|
|
122
|
+
*/
|
|
123
|
+
timeout?: number;
|
|
82
124
|
}
|
|
83
125
|
/**
|
|
84
126
|
* A poll specification that runs on a fixed time interval.
|
|
@@ -90,11 +132,12 @@ interface BasePollSpec {
|
|
|
90
132
|
* @example
|
|
91
133
|
* ```typescript
|
|
92
134
|
* const spec: IntervalPollSpec = {
|
|
93
|
-
* function:
|
|
94
|
-
* interval:
|
|
135
|
+
* function: refreshFeed,
|
|
136
|
+
* interval: 3600, // an hour after the previous run finished
|
|
95
137
|
* autostart: true,
|
|
96
138
|
* runImmediately: true, // fire once immediately on start
|
|
97
|
-
* failureStrategy: '
|
|
139
|
+
* failureStrategy: 'skip',
|
|
140
|
+
* timeout: 300 // seconds
|
|
98
141
|
* };
|
|
99
142
|
* ```
|
|
100
143
|
*/
|
|
@@ -120,7 +163,7 @@ export interface IntervalPollSpec extends BasePollSpec {
|
|
|
120
163
|
* const spec: CronPollSpec = {
|
|
121
164
|
* function: doWeeklyReport,
|
|
122
165
|
* cron: '0 9 * * 1', // every Monday at 09:00 UTC
|
|
123
|
-
* failureStrategy: '
|
|
166
|
+
* failureStrategy: 'skip' // a failed report waits for next Monday
|
|
124
167
|
* };
|
|
125
168
|
* ```
|
|
126
169
|
*/
|
|
@@ -159,27 +202,29 @@ export type PollSpec = IntervalPollSpec | CronPollSpec;
|
|
|
159
202
|
*
|
|
160
203
|
* ### Runtime Behavior
|
|
161
204
|
*
|
|
162
|
-
*
|
|
163
|
-
* integration workers
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
205
|
+
* Each poller runs **once per Polarity server instance (pod)**, regardless of how
|
|
206
|
+
* many integration workers that pod runs. With several pods, each pod runs its own
|
|
207
|
+
* copy, so a poller is not a cluster-wide singleton. `context.startPolling()` and
|
|
208
|
+
* `context.stopPolling()` act only on the pod that handles the call. See
|
|
209
|
+
* {@link PollFunction}.
|
|
167
210
|
*
|
|
168
211
|
* @example Basic polling configuration
|
|
169
212
|
* ```typescript
|
|
170
213
|
* import type { Poll } from '@polarityio/integration-types';
|
|
171
214
|
*
|
|
172
215
|
* const poll: Poll = {
|
|
173
|
-
*
|
|
174
|
-
* function:
|
|
175
|
-
* interval:
|
|
216
|
+
* refreshFeed: {
|
|
217
|
+
* function: refreshFeed,
|
|
218
|
+
* interval: 3600, // an hour after the previous run finished
|
|
176
219
|
* autostart: true,
|
|
177
|
-
* runImmediately: true
|
|
220
|
+
* runImmediately: true,
|
|
221
|
+
* failureStrategy: 'skip',
|
|
222
|
+
* timeout: 300 // seconds
|
|
178
223
|
* },
|
|
179
224
|
* weeklyReport: {
|
|
180
225
|
* function: doWeeklyReport,
|
|
181
226
|
* cron: '0 9 * * 1', // Mondays at 09:00 UTC
|
|
182
|
-
* failureStrategy: '
|
|
227
|
+
* failureStrategy: 'skip'
|
|
183
228
|
* }
|
|
184
229
|
* };
|
|
185
230
|
*
|
|
@@ -188,10 +233,10 @@ export type PollSpec = IntervalPollSpec | CronPollSpec;
|
|
|
188
233
|
*
|
|
189
234
|
* @example One-time initialization pattern
|
|
190
235
|
*
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
*
|
|
194
|
-
*
|
|
236
|
+
* A poller with `runImmediately: true` and `autostart: true` that stops itself with
|
|
237
|
+
* `context.stopPolling()` runs once per pod each time the integration loads, not once
|
|
238
|
+
* across the deployment. With several pods, or after a restart, it runs again, so
|
|
239
|
+
* the work must be idempotent.
|
|
195
240
|
*
|
|
196
241
|
* ```typescript
|
|
197
242
|
* import type { Poll, PollFunction } from '@polarityio/integration-types';
|
|
@@ -200,16 +245,17 @@ export type PollSpec = IntervalPollSpec | CronPollSpec;
|
|
|
200
245
|
* // Perform one-time setup (e.g., seed cache, register webhooks)
|
|
201
246
|
* await seedIntegrationCache(options, context);
|
|
202
247
|
*
|
|
203
|
-
* // Stop the poller
|
|
248
|
+
* // Stop the poller on this pod
|
|
204
249
|
* context.stopPolling('initialize');
|
|
205
250
|
* };
|
|
206
251
|
*
|
|
207
252
|
* const poll: Poll = {
|
|
208
253
|
* initialize: {
|
|
209
254
|
* function: initializeOnce,
|
|
210
|
-
* interval: 60, //
|
|
255
|
+
* interval: 60, // retried a minute later if no worker was free
|
|
211
256
|
* autostart: true,
|
|
212
|
-
* runImmediately: true
|
|
257
|
+
* runImmediately: true, // fire immediately on integration load
|
|
258
|
+
* failureStrategy: 'retry' // retried until it succeeds
|
|
213
259
|
* }
|
|
214
260
|
* };
|
|
215
261
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@polarityio/integration-types",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.2.2",
|
|
4
4
|
"description": "TypeScript type definitions for Polarity Integrations",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|
|
26
|
-
"url": "git+https://github.com/
|
|
26
|
+
"url": "git+https://github.com/DMCD-Integrations/integration-types.git"
|
|
27
27
|
},
|
|
28
28
|
"author": "Polarity IO <support@polarity.io>",
|
|
29
29
|
"license": "MIT",
|
|
@@ -31,9 +31,9 @@
|
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
33
|
"bugs": {
|
|
34
|
-
"url": "https://github.com/
|
|
34
|
+
"url": "https://github.com/DMCD-Integrations/integration-types/issues"
|
|
35
35
|
},
|
|
36
|
-
"homepage": "https://github.com/
|
|
36
|
+
"homepage": "https://github.com/DMCD-Integrations/integration-types#readme",
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@eslint/js": "^10.0.1",
|
|
39
39
|
"eslint": "^10.2.0",
|