@pgflow/edge-worker 0.0.23 → 0.1.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @pgflow/edge-worker
2
2
 
3
+ ## 0.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [b362364]
8
+ - @pgflow/dsl@0.1.1
9
+ - @pgflow/core@0.1.1
10
+
11
+ ## 0.1.0
12
+
13
+ ### Patch Changes
14
+
15
+ - Updated dependencies [7c40238]
16
+ - @pgflow/core@0.1.0
17
+ - @pgflow/dsl@0.1.0
18
+
3
19
  ## 0.0.23
4
20
 
5
21
  ### Patch Changes
@@ -1,18 +1,22 @@
1
1
  import type { Json } from './core/types.js';
2
2
  import { type QueueWorkerConfig } from './queue/createQueueWorker.js';
3
+ import { type FlowWorkerConfig } from './flow/createFlowWorker.js';
3
4
  import type { PlatformAdapter } from './platform/types.js';
4
5
  import { MessageHandlerFn } from './queue/types.js';
6
+ import type { AnyFlow } from '@pgflow/dsl';
5
7
  /**
6
8
  * Configuration options for the EdgeWorker.
7
9
  */
8
- export type EdgeWorkerConfig = QueueWorkerConfig;
10
+ export type EdgeWorkerConfig = Omit<QueueWorkerConfig, 'sql'> | Omit<FlowWorkerConfig, 'sql'>;
9
11
  /**
10
12
  * EdgeWorker is the main entry point for creating and starting edge workers.
11
13
  *
12
- * It provides a simple interface for starting a worker that processes messages from a queue.
14
+ * It provides a simple interface for starting a worker that processes messages from a queue
15
+ * or executes steps in a flow.
13
16
  *
14
17
  * @example
15
18
  * ```typescript
19
+ * // Queue worker example
16
20
  * import { EdgeWorker } from '@pgflow/edge-worker';
17
21
  *
18
22
  * EdgeWorker.start(async (message) => {
@@ -23,11 +27,33 @@ export type EdgeWorkerConfig = QueueWorkerConfig;
23
27
  * maxConcurrent: 5,
24
28
  * retryLimit: 3
25
29
  * });
30
+ *
31
+ * // Flow worker example
32
+ * import { EdgeWorker } from '@pgflow/edge-worker';
33
+ * import { MyFlow } from './flows.js';
34
+ *
35
+ * EdgeWorker.start(MyFlow, {
36
+ * maxConcurrent: 5
37
+ * });
26
38
  * ```
27
39
  */
28
40
  export declare class EdgeWorker {
29
41
  private static platform;
30
42
  private static wasCalled;
43
+ /**
44
+ * Start the EdgeWorker with a message handler function.
45
+ *
46
+ * @param handler - Function that processes each message from the queue
47
+ * @param config - Configuration options for the queue worker
48
+ */
49
+ static start<TPayload extends Json = Json>(handler: MessageHandlerFn<TPayload>, config?: Omit<QueueWorkerConfig, 'sql'>): Promise<PlatformAdapter>;
50
+ /**
51
+ * Start the EdgeWorker with a flow instance.
52
+ *
53
+ * @param flow - Flow instance that defines the workflow to execute
54
+ * @param config - Configuration options for the flow worker
55
+ */
56
+ static start<TFlow extends AnyFlow>(flow: TFlow, config?: Omit<FlowWorkerConfig, 'sql'>): Promise<PlatformAdapter>;
31
57
  /**
32
58
  * Start the EdgeWorker with the given message handler and configuration.
33
59
  *
@@ -36,7 +62,7 @@ export declare class EdgeWorker {
36
62
  *
37
63
  * @example
38
64
  * ```typescript
39
- * EdgeWorker.start(handler, {
65
+ * EdgeWorker.startQueueWorker(handler, {
40
66
  * // name of the queue to poll for messages
41
67
  * queueName: 'tasks',
42
68
  *
@@ -64,7 +90,28 @@ export declare class EdgeWorker {
64
90
  * });
65
91
  * ```
66
92
  */
67
- static start<TPayload extends Json = Json>(handler: MessageHandlerFn<TPayload>, config?: EdgeWorkerConfig): Promise<PlatformAdapter>;
93
+ static startQueueWorker<TPayload extends Json = Json>(handler: MessageHandlerFn<TPayload>, config?: QueueWorkerConfig): Promise<PlatformAdapter>;
94
+ /**
95
+ * Start the EdgeWorker with the given flow instance and configuration.
96
+ *
97
+ * @param flow - Flow instance that defines the workflow to execute
98
+ * @param config - Configuration options for the worker
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * EdgeWorker.startFlowWorker(MyFlow, {
103
+ * // how many tasks are processed at the same time
104
+ * maxConcurrent: 10,
105
+ *
106
+ * // how many connections to the database are opened
107
+ * maxPgConnections: 4,
108
+ *
109
+ * // batch size for polling messages
110
+ * batchSize: 10,
111
+ * });
112
+ * ```
113
+ */
114
+ static startFlowWorker<TFlow extends AnyFlow>(flow: TFlow, config?: FlowWorkerConfig): Promise<PlatformAdapter>;
68
115
  /**
69
116
  * Stop the EdgeWorker and clean up resources.
70
117
  */
@@ -1 +1 @@
1
- {"version":3,"file":"EdgeWorker.d.ts","sourceRoot":"","sources":["../src/EdgeWorker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAEL,KAAK,iBAAiB,EACvB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,iBAAiB,CAAC;AAEjD;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,UAAU;IACrB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAgC;IACvD,OAAO,CAAC,MAAM,CAAC,SAAS,CAAS;IAEjC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAmCG;WACU,KAAK,CAAC,QAAQ,SAAS,IAAI,GAAG,IAAI,EAC7C,OAAO,EAAE,gBAAgB,CAAC,QAAQ,CAAC,EACnC,MAAM,GAAE,gBAAqB;IA6B/B;;OAEG;WACU,IAAI;IAQjB,OAAO,CAAC,MAAM,CAAC,eAAe;CAM/B"}
1
+ {"version":3,"file":"EdgeWorker.d.ts","sourceRoot":"","sources":["../src/EdgeWorker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAEL,KAAK,iBAAiB,EACvB,MAAM,8BAA8B,CAAC;AACtC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,CAAC,eAAe;CAM/B"}
@@ -1,12 +1,15 @@
1
1
  import { createQueueWorker, } from './queue/createQueueWorker.js';
2
+ import { createFlowWorker, } from './flow/createFlowWorker.js';
2
3
  import { createAdapter } from './platform/createAdapter.js';
3
4
  /**
4
5
  * EdgeWorker is the main entry point for creating and starting edge workers.
5
6
  *
6
- * It provides a simple interface for starting a worker that processes messages from a queue.
7
+ * It provides a simple interface for starting a worker that processes messages from a queue
8
+ * or executes steps in a flow.
7
9
  *
8
10
  * @example
9
11
  * ```typescript
12
+ * // Queue worker example
10
13
  * import { EdgeWorker } from '@pgflow/edge-worker';
11
14
  *
12
15
  * EdgeWorker.start(async (message) => {
@@ -17,11 +20,35 @@ import { createAdapter } from './platform/createAdapter.js';
17
20
  * maxConcurrent: 5,
18
21
  * retryLimit: 3
19
22
  * });
23
+ *
24
+ * // Flow worker example
25
+ * import { EdgeWorker } from '@pgflow/edge-worker';
26
+ * import { MyFlow } from './flows.js';
27
+ *
28
+ * EdgeWorker.start(MyFlow, {
29
+ * maxConcurrent: 5
30
+ * });
20
31
  * ```
21
32
  */
22
33
  export class EdgeWorker {
23
34
  static platform = null;
24
35
  static wasCalled = false;
36
+ /**
37
+ * Implementation of the start method that handles both function and flow cases.
38
+ * This method automatically detects the type of the first argument and delegates
39
+ * to the appropriate worker creation method.
40
+ *
41
+ * @param handlerOrFlow - Either a message handler function or a Flow instance
42
+ * @param config - Configuration options for the worker
43
+ */
44
+ static async start(handlerOrFlow, config = {}) {
45
+ if (typeof handlerOrFlow === 'function') {
46
+ return this.startQueueWorker(handlerOrFlow, config);
47
+ }
48
+ else {
49
+ return this.startFlowWorker(handlerOrFlow, config);
50
+ }
51
+ }
25
52
  /**
26
53
  * Start the EdgeWorker with the given message handler and configuration.
27
54
  *
@@ -30,7 +57,7 @@ export class EdgeWorker {
30
57
  *
31
58
  * @example
32
59
  * ```typescript
33
- * EdgeWorker.start(handler, {
60
+ * EdgeWorker.startQueueWorker(handler, {
34
61
  * // name of the queue to poll for messages
35
62
  * queueName: 'tasks',
36
63
  *
@@ -58,7 +85,7 @@ export class EdgeWorker {
58
85
  * });
59
86
  * ```
60
87
  */
61
- static async start(handler, config = {}) {
88
+ static async startQueueWorker(handler, config = {}) {
62
89
  this.ensureFirstCall();
63
90
  // First, create the adapter
64
91
  this.platform = await createAdapter();
@@ -78,6 +105,41 @@ export class EdgeWorker {
78
105
  await this.platform.startWorker((createLoggerFn) => createQueueWorker(handler, workerConfig, createLoggerFn));
79
106
  return this.platform;
80
107
  }
108
+ /**
109
+ * Start the EdgeWorker with the given flow instance and configuration.
110
+ *
111
+ * @param flow - Flow instance that defines the workflow to execute
112
+ * @param config - Configuration options for the worker
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * EdgeWorker.startFlowWorker(MyFlow, {
117
+ * // how many tasks are processed at the same time
118
+ * maxConcurrent: 10,
119
+ *
120
+ * // how many connections to the database are opened
121
+ * maxPgConnections: 4,
122
+ *
123
+ * // batch size for polling messages
124
+ * batchSize: 10,
125
+ * });
126
+ * ```
127
+ */
128
+ static async startFlowWorker(flow, config = {}) {
129
+ this.ensureFirstCall();
130
+ // First, create the adapter
131
+ this.platform = await createAdapter();
132
+ // Apply default values to the config
133
+ const workerConfig = {
134
+ ...config,
135
+ maxConcurrent: config.maxConcurrent ?? 10,
136
+ maxPgConnections: config.maxPgConnections ?? 4,
137
+ batchSize: config.batchSize ?? 10,
138
+ connectionString: config.connectionString || this.platform.getConnectionString(),
139
+ };
140
+ await this.platform.startWorker((createLoggerFn) => createFlowWorker(flow, workerConfig, createLoggerFn));
141
+ return this.platform;
142
+ }
81
143
  /**
82
144
  * Stop the EdgeWorker and clean up resources.
83
145
  */
@@ -91,7 +153,7 @@ export class EdgeWorker {
91
153
  }
92
154
  static ensureFirstCall() {
93
155
  if (this.wasCalled) {
94
- throw new Error('EdgeWorker.start() can only be called once');
156
+ throw new Error('EdgeWorker worker can only be started once');
95
157
  }
96
158
  this.wasCalled = true;
97
159
  }
@@ -1,16 +1,34 @@
1
1
  import type { AnyFlow } from '@pgflow/dsl';
2
- import type { EdgeWorkerConfig } from '../EdgeWorker.js';
3
2
  import type { Logger } from '../platform/types.js';
4
3
  import { Worker } from '../core/Worker.js';
5
4
  import postgres from 'postgres';
6
5
  /**
7
6
  * Configuration for the flow worker
8
7
  */
9
- export type FlowWorkerConfig = EdgeWorkerConfig & {
8
+ export type FlowWorkerConfig = {
9
+ /**
10
+ * How many tasks are processed at the same time
11
+ * @default 10
12
+ */
10
13
  maxConcurrent?: number;
14
+ /**
15
+ * PostgreSQL connection string.
16
+ * If not provided, it will be read from the EDGE_WORKER_DB_URL environment variable.
17
+ */
11
18
  connectionString?: string;
19
+ /**
20
+ * Optional SQL client instance
21
+ */
12
22
  sql?: postgres.Sql;
23
+ /**
24
+ * How many connections to the database are opened
25
+ * @default 4
26
+ */
13
27
  maxPgConnections?: number;
28
+ /**
29
+ * Batch size for polling messages
30
+ * @default 10
31
+ */
14
32
  batchSize?: number;
15
33
  };
16
34
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"createFlowWorker.d.ts","sourceRoot":"","sources":["../../src/flow/createFlowWorker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAQzD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,QAAQ,MAAM,UAAU,CAAC;AAIhC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,gBAAgB,GAAG;IAChD,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,GAAG,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC;IACnB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,SAAS,OAAO,EACpD,IAAI,EAAE,KAAK,EACX,MAAM,EAAE,gBAAgB,EACxB,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,GACvC,MAAM,CAsFR"}
1
+ {"version":3,"file":"createFlowWorker.d.ts","sourceRoot":"","sources":["../../src/flow/createFlowWorker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAQ3C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC3C,OAAO,QAAQ,MAAM,UAAU,CAAC;AAIhC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B;;OAEG;IACH,GAAG,CAAC,EAAE,QAAQ,CAAC,GAAG,CAAC;IAEnB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,SAAS,OAAO,EACpD,IAAI,EAAE,KAAK,EACX,MAAM,EAAE,gBAAgB,EACxB,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,GACvC,MAAM,CAiFR"}
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgflow/edge-worker",
3
- "version": "0.0.23",
3
+ "version": "0.1.1",
4
4
  "license": "AGPL-3.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pgflow/edge-worker",
3
- "version": "0.0.23",
3
+ "version": "0.1.1",
4
4
  "license": "AGPL-3.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",