@unchainedshop/core-worker 5.0.0-alpha.1 → 5.0.0-alpha.10
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/README.md +13 -82
- package/lib/db/WorkQueueCollection.js +18 -20
- package/lib/module/configureWorkerModule.js +2 -3
- package/package.json +10 -5
package/README.md
CHANGED
|
@@ -13,96 +13,27 @@ npm install @unchainedshop/core-worker
|
|
|
13
13
|
|
|
14
14
|
## Usage
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
import { configureWorkerModule, WorkStatus } from '@unchainedshop/core-worker';
|
|
16
|
+
The platform initializes this module as `platform.unchainedAPI.modules.worker`. Register worker plugins before platform startup; the base preset includes the `EMAIL` worker.
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
```typescript
|
|
19
|
+
import { WorkStatus } from '@unchainedshop/core-worker';
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
type: '
|
|
21
|
+
const { worker } = platform.unchainedAPI.modules;
|
|
22
|
+
const work = await worker.addWork({
|
|
23
|
+
type: 'EMAIL',
|
|
24
24
|
input: {
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
from: 'shop@example.com',
|
|
26
|
+
to: 'customer@example.com',
|
|
27
|
+
subject: 'Order update',
|
|
28
|
+
text: 'Your order is ready.',
|
|
27
29
|
},
|
|
28
30
|
});
|
|
29
|
-
|
|
30
|
-
// Find pending work
|
|
31
|
-
const pendingWork = await workerModule.findWork({
|
|
32
|
-
status: WorkStatus.NEW,
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
// Process work (typically done by worker plugins)
|
|
36
|
-
await workerModule.processWork(workId, {
|
|
37
|
-
success: true,
|
|
38
|
-
result: { messageId: 'abc123' },
|
|
39
|
-
});
|
|
31
|
+
const pending = await worker.findWorkQueue({ status: [WorkStatus.NEW] });
|
|
40
32
|
```
|
|
41
33
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
### Module Configuration
|
|
45
|
-
|
|
46
|
-
| Export | Description |
|
|
47
|
-
|--------|-------------|
|
|
48
|
-
| `configureWorkerModule` | Configure and return the worker module |
|
|
49
|
-
|
|
50
|
-
### Queries
|
|
51
|
-
|
|
52
|
-
| Method | Description |
|
|
53
|
-
|--------|-------------|
|
|
54
|
-
| `findWork` | Find work item by ID |
|
|
55
|
-
| `findWorkQueue` | Find work items with filtering |
|
|
56
|
-
| `count` | Count work items matching query |
|
|
57
|
-
|
|
58
|
-
### Mutations
|
|
59
|
-
|
|
60
|
-
| Method | Description |
|
|
61
|
-
|--------|-------------|
|
|
62
|
-
| `addWork` | Add work item to queue |
|
|
63
|
-
| `allocateWork` | Allocate work to a worker |
|
|
64
|
-
| `processWork` | Mark work as processed |
|
|
65
|
-
| `rescheduleWork` | Reschedule failed work |
|
|
66
|
-
| `deleteWork` | Delete a work item |
|
|
67
|
-
|
|
68
|
-
### Constants
|
|
69
|
-
|
|
70
|
-
| Export | Description |
|
|
71
|
-
|--------|-------------|
|
|
72
|
-
| `WorkStatus` | Status values (NEW, ALLOCATED, SUCCESS, FAILED, DELETED) |
|
|
73
|
-
|
|
74
|
-
### Types
|
|
75
|
-
|
|
76
|
-
| Export | Description |
|
|
77
|
-
|--------|-------------|
|
|
78
|
-
| `Work` | Work item document type |
|
|
79
|
-
| `WorkerModule` | Module interface type |
|
|
80
|
-
|
|
81
|
-
## Work Types
|
|
82
|
-
|
|
83
|
-
Work types are linked to worker plugins. Common built-in types:
|
|
84
|
-
|
|
85
|
-
| Type | Description |
|
|
86
|
-
|------|-------------|
|
|
87
|
-
| `SEND_EMAIL` | Send email notifications |
|
|
88
|
-
| `HEARTBEAT` | Keep-alive jobs |
|
|
89
|
-
| `EXTERNAL` | External service calls |
|
|
90
|
-
|
|
91
|
-
## Worker Plugins
|
|
92
|
-
|
|
93
|
-
Workers process jobs by type. The plugin is responsible for:
|
|
94
|
-
- Handling retries on failure
|
|
95
|
-
- Processing the work input
|
|
96
|
-
- Returning success/failure results
|
|
97
|
-
|
|
98
|
-
## Events
|
|
34
|
+
`addWork` returns the work document. The platform's work loop allocates, executes, and finishes internal jobs through `WorkerDirector`. External workers claim jobs with `allocateWork` and report results with `finishWork`; allocation alone does not execute them. Higher numeric priorities are processed first.
|
|
99
35
|
|
|
100
|
-
|
|
101
|
-
|-------|-------------|
|
|
102
|
-
| `WORK_ADDED` | Work item added to queue |
|
|
103
|
-
| `WORK_ALLOCATED` | Work allocated to worker |
|
|
104
|
-
| `WORK_FINISHED` | Work processing completed |
|
|
105
|
-
| `WORK_FAILED` | Work processing failed |
|
|
36
|
+
See the [module settings](https://docs.unchained.shop/platform-configuration/modules/worker), [worker extension guide](https://docs.unchained.shop/extend/worker), [public exports](src/worker-index.ts), and [module implementation](src/module/configureWorkerModule.ts).
|
|
106
37
|
|
|
107
38
|
## License
|
|
108
39
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {} from '@unchainedshop/mongodb';
|
|
2
2
|
import { buildDbIndexes } from '@unchainedshop/mongodb';
|
|
3
3
|
const ONE_DAY_IN_SECONDS = 86400;
|
|
4
4
|
export const WorkStatus = {
|
|
@@ -10,34 +10,32 @@ export const WorkStatus = {
|
|
|
10
10
|
};
|
|
11
11
|
export const WorkQueueCollection = async (db) => {
|
|
12
12
|
const WorkQueue = db.collection('work_queue');
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
{
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
input: 2,
|
|
24
|
-
},
|
|
25
|
-
name: 'workqueue_fulltext_search',
|
|
13
|
+
await buildDbIndexes(WorkQueue, [
|
|
14
|
+
{
|
|
15
|
+
index: { originalWorkId: 'text', _id: 'text', worker: 'text', input: 'text', type: 'text' },
|
|
16
|
+
options: {
|
|
17
|
+
weights: {
|
|
18
|
+
_id: 8,
|
|
19
|
+
originalWorkId: 6,
|
|
20
|
+
type: 5,
|
|
21
|
+
worker: 4,
|
|
22
|
+
input: 2,
|
|
26
23
|
},
|
|
24
|
+
name: 'workqueue_fulltext_search',
|
|
27
25
|
},
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
},
|
|
27
|
+
]);
|
|
30
28
|
await buildDbIndexes(WorkQueue, [
|
|
31
29
|
{
|
|
32
30
|
index: { created: -1 },
|
|
33
31
|
options: { expireAfterSeconds: 30 * ONE_DAY_IN_SECONDS },
|
|
34
32
|
},
|
|
35
|
-
{ index: {
|
|
36
|
-
{ index: { finished: 1 } },
|
|
37
|
-
{ index: { scheduled: 1 } },
|
|
38
|
-
{ index: { priority: -1 } },
|
|
33
|
+
{ index: { scheduled: 1, type: 1 } },
|
|
39
34
|
{ index: { type: 1 } },
|
|
40
35
|
{ index: { originalWorkId: 1 } },
|
|
36
|
+
{ index: { started: 1 }, options: { sparse: true } },
|
|
37
|
+
{ index: { finished: 1 }, options: { sparse: true } },
|
|
38
|
+
{ index: { priority: -1 } },
|
|
41
39
|
]);
|
|
42
40
|
return WorkQueue;
|
|
43
41
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import os from 'node:os';
|
|
2
2
|
import { createLogger } from '@unchainedshop/logger';
|
|
3
|
-
import { generateDbFilterById, buildSortOptions, generateDbObjectId,
|
|
3
|
+
import { generateDbFilterById, buildSortOptions, generateDbObjectId, } from '@unchainedshop/mongodb';
|
|
4
4
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
5
5
|
import { buildObfuscatedFieldsFilter, SortDirection, } from '@unchainedshop/utils';
|
|
6
6
|
import { WorkQueueCollection, WorkStatus } from "../db/WorkQueueCollection.js";
|
|
@@ -58,7 +58,6 @@ export const buildQuerySelector = ({ created, scheduled, types, status, workId,
|
|
|
58
58
|
query = generateDbFilterById(workId, query);
|
|
59
59
|
}
|
|
60
60
|
if (queryString) {
|
|
61
|
-
assertDocumentDBCompatMode();
|
|
62
61
|
query.$text = { $search: queryString };
|
|
63
62
|
}
|
|
64
63
|
return { ...query, ...rest };
|
|
@@ -318,7 +317,7 @@ export const configureWorkerModule = async ({ db, options }) => {
|
|
|
318
317
|
try {
|
|
319
318
|
const result = await WorkQueue.findOneAndUpdate(query, {
|
|
320
319
|
$set: {
|
|
321
|
-
input,
|
|
320
|
+
input: input || {},
|
|
322
321
|
worker: null,
|
|
323
322
|
updated: created,
|
|
324
323
|
retries,
|
package/package.json
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-worker",
|
|
3
3
|
"description": "Background job and task queue module for the Unchained Engine",
|
|
4
|
-
"version": "5.0.0-alpha.
|
|
4
|
+
"version": "5.0.0-alpha.10",
|
|
5
|
+
"files": [
|
|
6
|
+
"lib"
|
|
7
|
+
],
|
|
5
8
|
"main": "lib/worker-index.js",
|
|
6
9
|
"types": "lib/worker-index.d.ts",
|
|
7
10
|
"type": "module",
|
|
@@ -35,11 +38,13 @@
|
|
|
35
38
|
},
|
|
36
39
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
37
40
|
"dependencies": {
|
|
38
|
-
"@unchainedshop/
|
|
39
|
-
"@unchainedshop/
|
|
41
|
+
"@unchainedshop/events": "^5.0.0-alpha.10",
|
|
42
|
+
"@unchainedshop/logger": "^5.0.0-alpha.10",
|
|
43
|
+
"@unchainedshop/mongodb": "^5.0.0-alpha.10",
|
|
44
|
+
"@unchainedshop/utils": "^5.0.0-alpha.10"
|
|
40
45
|
},
|
|
41
46
|
"devDependencies": {
|
|
42
|
-
"@types/node": "^
|
|
43
|
-
"typescript": "^
|
|
47
|
+
"@types/node": "^26.2.0",
|
|
48
|
+
"typescript": "^6.0.3"
|
|
44
49
|
}
|
|
45
50
|
}
|