@betterinternship/tasks 2.4.7 → 2.4.8

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.
@@ -1,90 +1,90 @@
1
- import { processes, tasks } from '@betterinternship/db';
2
- export const defineProcessTemplate = (template) => {
3
- return {
4
- defineMaps: (maps) => template.map((d) => ({
5
- ...d,
6
- map: maps[d.internalId],
7
- })),
8
- };
9
- };
10
- export class ProcessFactory {
11
- name;
12
- template;
13
- constructor(name, template) {
14
- this.name = name;
15
- this.template = template;
16
- this.template.map((taskDescriptor) => {
17
- taskDescriptor.initiator.registerHandler(async (task, response) => {
18
- if (response.status === 'failed') {
19
- await processes.update({
20
- where: { id: task.process_id },
21
- data: { status: 'failed' },
22
- });
23
- }
24
- else if (response.status === 'handled') {
25
- const process = await processes.findFirst({
26
- where: { id: task.process_id },
27
- });
28
- const childTaskDescriptors = this.template.filter((childTaskDescriptor) => childTaskDescriptor.parentIds?.includes(taskDescriptor.internalId));
29
- if (!childTaskDescriptors.length) {
30
- const processTasks = await tasks.findMany({
31
- where: { process_id: task.process_id },
32
- });
33
- const handled = processTasks.every((processTask) => processTask.status === 'handled');
34
- if (handled) {
35
- await processes.update({
36
- where: { id: task.process_id },
37
- data: { status: 'handled' },
38
- });
39
- }
40
- }
41
- for (const childTaskDescriptor of childTaskDescriptors) {
42
- const parentTasks = await tasks.findMany({
43
- where: {
44
- process_id: task.process_id,
45
- internal_id: { in: childTaskDescriptor.parentIds },
46
- },
47
- select: {
48
- id: true,
49
- internal_id: true,
50
- status: true,
51
- result: true,
52
- },
53
- });
54
- const shouldPublish = parentTasks.every((parentTask) => parentTask.status === 'handled');
55
- const internalInputs = parentTasks.reduce((acc, cur) => ((acc[cur.internal_id] = cur.result), acc), {});
56
- const baseInputs = process?.base_inputs;
57
- const td = taskDescriptor;
58
- const mappedInputs = td.map
59
- ? td.map(internalInputs, baseInputs)
60
- : baseInputs;
61
- await childTaskDescriptor.initiator.sendTaskRequest(process, mappedInputs, {
62
- internalId: childTaskDescriptor.internalId,
63
- parentIds: childTaskDescriptor.parentIds,
64
- maxRetries: childTaskDescriptor.maxRetries,
65
- skipPublish: !shouldPublish,
66
- });
67
- }
68
- }
69
- });
70
- });
71
- }
72
- async create(baseInputs) {
73
- const process = await processes.create({
74
- data: { name: this.name, base_inputs: baseInputs },
75
- });
76
- this.template.forEach((taskDescriptor) => {
77
- const shouldPublish = !taskDescriptor.parentIds || taskDescriptor.parentIds.length === 0;
78
- const td = taskDescriptor;
79
- const mappedInputs = td.map ? td.map({}, baseInputs) : baseInputs;
80
- void taskDescriptor.initiator.sendTaskRequest(process, mappedInputs, {
81
- internalId: taskDescriptor.internalId,
82
- parentIds: taskDescriptor.parentIds,
83
- maxRetries: taskDescriptor.maxRetries,
84
- skipPublish: !shouldPublish,
85
- });
86
- });
87
- return process.id;
88
- }
89
- }
1
+ import { processes, tasks } from '@betterinternship/db';
2
+ export const defineProcessTemplate = (template) => {
3
+ return {
4
+ defineMaps: (maps) => template.map((d) => ({
5
+ ...d,
6
+ map: maps[d.internalId],
7
+ })),
8
+ };
9
+ };
10
+ export class ProcessFactory {
11
+ name;
12
+ template;
13
+ constructor(name, template) {
14
+ this.name = name;
15
+ this.template = template;
16
+ this.template.map((taskDescriptor) => {
17
+ taskDescriptor.initiator.registerHandler(async (task, response) => {
18
+ if (response.status === 'failed') {
19
+ await processes.update({
20
+ where: { id: task.process_id },
21
+ data: { status: 'failed' },
22
+ });
23
+ }
24
+ else if (response.status === 'handled') {
25
+ const process = await processes.findFirst({
26
+ where: { id: task.process_id },
27
+ });
28
+ const childTaskDescriptors = this.template.filter((childTaskDescriptor) => childTaskDescriptor.parentIds?.includes(taskDescriptor.internalId));
29
+ if (!childTaskDescriptors.length) {
30
+ const processTasks = await tasks.findMany({
31
+ where: { process_id: task.process_id },
32
+ });
33
+ const handled = processTasks.every((processTask) => processTask.status === 'handled');
34
+ if (handled) {
35
+ await processes.update({
36
+ where: { id: task.process_id },
37
+ data: { status: 'handled' },
38
+ });
39
+ }
40
+ }
41
+ for (const childTaskDescriptor of childTaskDescriptors) {
42
+ const parentTasks = await tasks.findMany({
43
+ where: {
44
+ process_id: task.process_id,
45
+ internal_id: { in: childTaskDescriptor.parentIds },
46
+ },
47
+ select: {
48
+ id: true,
49
+ internal_id: true,
50
+ status: true,
51
+ result: true,
52
+ },
53
+ });
54
+ const shouldPublish = parentTasks.every((parentTask) => parentTask.status === 'handled');
55
+ const internalInputs = parentTasks.reduce((acc, cur) => ((acc[cur.internal_id] = cur.result), acc), {});
56
+ const baseInputs = process?.base_inputs;
57
+ const td = taskDescriptor;
58
+ const mappedInputs = td.map
59
+ ? td.map(internalInputs, baseInputs)
60
+ : baseInputs;
61
+ await childTaskDescriptor.initiator.sendTaskRequest(process, mappedInputs, {
62
+ internalId: childTaskDescriptor.internalId,
63
+ parentIds: childTaskDescriptor.parentIds,
64
+ maxRetries: childTaskDescriptor.maxRetries,
65
+ skipPublish: !shouldPublish,
66
+ });
67
+ }
68
+ }
69
+ });
70
+ });
71
+ }
72
+ async create(baseInputs) {
73
+ const process = await processes.create({
74
+ data: { name: this.name, base_inputs: baseInputs },
75
+ });
76
+ this.template.forEach((taskDescriptor) => {
77
+ const shouldPublish = !taskDescriptor.parentIds || taskDescriptor.parentIds.length === 0;
78
+ const td = taskDescriptor;
79
+ const mappedInputs = td.map ? td.map({}, baseInputs) : baseInputs;
80
+ void taskDescriptor.initiator.sendTaskRequest(process, mappedInputs, {
81
+ internalId: taskDescriptor.internalId,
82
+ parentIds: taskDescriptor.parentIds,
83
+ maxRetries: taskDescriptor.maxRetries,
84
+ skipPublish: !shouldPublish,
85
+ });
86
+ });
87
+ return process.id;
88
+ }
89
+ }
90
90
  //# sourceMappingURL=process.js.map
@@ -1,129 +1,129 @@
1
- import { BrokerAPI, } from '../broker.js';
2
- import { v4 } from 'uuid';
3
- import { tasks } from '@betterinternship/db';
4
- export class TaskRequest {
5
- inputs;
6
- internal_id;
7
- handler_id;
8
- parent_ids;
9
- process_id;
10
- timestamp;
11
- }
12
- export class TaskResponse {
13
- result;
14
- error;
15
- status;
16
- }
17
- export class BaseTaskInitiator {
18
- _inputs;
19
- _results;
20
- handlers = [];
21
- handlerId;
22
- requestQueueId;
23
- responseQueueId;
24
- constructor(queueId) {
25
- this.handlerId = queueId;
26
- this.requestQueueId = `request.${queueId}`;
27
- this.responseQueueId = `response.${queueId}`;
28
- void this.attachHandlers();
29
- }
30
- registerHandler(handler) {
31
- this.handlers.push(handler);
32
- return this;
33
- }
34
- async attachHandlers() {
35
- const brokerAPI = await BrokerAPI.instance();
36
- const initiator = this;
37
- const responseHandler = async (taskId, actions) => {
38
- const task = await tasks.findFirst({ where: { id: taskId } });
39
- const handlers = initiator.handlers;
40
- for (const handler of handlers) {
41
- await handler(task, {
42
- result: task?.result,
43
- error: task?.error ?? '',
44
- status: task?.status ?? 'failed',
45
- });
46
- }
47
- if (task?.status === 'failed' || !task?.status)
48
- actions.reject();
49
- else if (task?.status === 'handled')
50
- actions.resolve();
51
- else
52
- actions.hold();
53
- };
54
- return await brokerAPI.registerQueueHandler(this.responseQueueId, `response.${this.handlerId}.#`, responseHandler);
55
- }
56
- async sendTaskRequest(process, inputs, options) {
57
- const brokerAPI = await BrokerAPI.instance();
58
- const task = await tasks.create({
59
- data: {
60
- internal_id: options?.internalId ?? `${process.id}.${v4()}`,
61
- parent_ids: options?.parentIds ?? [],
62
- handler_id: this.handlerId,
63
- process_id: process.id,
64
- process_name: process.name,
65
- inputs: inputs,
66
- },
67
- });
68
- console.log(`------ CREATING: ID: ${task.id}`);
69
- console.log(`------ CREATING: INTERNAL ID: ${task.internal_id}`);
70
- console.log(`------ CREATING: HANDLER ID: ${task.handler_id}`);
71
- console.log(`------ CREATING: PROCESS NAME: ${task.handler_id}`);
72
- if (options?.skipPublish) {
73
- return Promise.resolve(true);
74
- }
75
- const routingKey = `request.${this.handlerId}.${process.name}`;
76
- return brokerAPI.publish(routingKey, task.id, options);
77
- }
78
- }
79
- export class BaseTaskRunner {
80
- requestQueueId;
81
- responseQueueId;
82
- handlerId;
83
- handler;
84
- constructor(queueId, handler) {
85
- this.handlerId = queueId;
86
- this.requestQueueId = `request.${queueId}`;
87
- this.responseQueueId = `response.${queueId}`;
88
- this.handler = handler;
89
- }
90
- async listen(processName) {
91
- const taskRunner = async (taskId, actions) => {
92
- let task = await tasks.findFirst({ where: { id: taskId } });
93
- let response;
94
- try {
95
- response = await this.handler(task, this);
96
- }
97
- catch (err) {
98
- response = {
99
- status: 'failed',
100
- error: JSON.stringify(err),
101
- };
102
- }
103
- task = await tasks.update({
104
- where: { id: taskId },
105
- data: {
106
- result: response.result ?? {},
107
- error: response.error ?? '',
108
- status: response.status,
109
- },
110
- });
111
- console.log(`[BROKER] !${response.status}: ${task.internal_id} (${task.id})`);
112
- if (response.error) {
113
- console.log(`[BROKER] Reason: ${response.error}`);
114
- }
115
- console.log(`RESPONSE FOR ${task?.internal_id}: `, response);
116
- console.log(`RESULT FOR ${task?.internal_id}: `, response.result);
117
- console.log(`LATEST TASK FOR ${task?.internal_id}: `, task);
118
- if (response.error)
119
- actions.reject();
120
- else
121
- actions.resolve();
122
- const responseRoutingKey = `response.${this.handlerId}.${task.process_name}`;
123
- await brokerAPI.publish(responseRoutingKey, taskId);
124
- };
125
- const brokerAPI = await BrokerAPI.instance();
126
- await brokerAPI.registerQueueHandler(this.requestQueueId, `request.${this.handlerId}.${processName}.#`, taskRunner);
127
- }
128
- }
1
+ import { BrokerAPI, } from '../broker.js';
2
+ import { v4 } from 'uuid';
3
+ import { tasks } from '@betterinternship/db';
4
+ export class TaskRequest {
5
+ inputs;
6
+ internal_id;
7
+ handler_id;
8
+ parent_ids;
9
+ process_id;
10
+ timestamp;
11
+ }
12
+ export class TaskResponse {
13
+ result;
14
+ error;
15
+ status;
16
+ }
17
+ export class BaseTaskInitiator {
18
+ _inputs;
19
+ _results;
20
+ handlers = [];
21
+ handlerId;
22
+ requestQueueId;
23
+ responseQueueId;
24
+ constructor(queueId) {
25
+ this.handlerId = queueId;
26
+ this.requestQueueId = `request.${queueId}`;
27
+ this.responseQueueId = `response.${queueId}`;
28
+ void this.attachHandlers();
29
+ }
30
+ registerHandler(handler) {
31
+ this.handlers.push(handler);
32
+ return this;
33
+ }
34
+ async attachHandlers() {
35
+ const brokerAPI = await BrokerAPI.instance();
36
+ const initiator = this;
37
+ const responseHandler = async (taskId, actions) => {
38
+ const task = await tasks.findFirst({ where: { id: taskId } });
39
+ const handlers = initiator.handlers;
40
+ for (const handler of handlers) {
41
+ await handler(task, {
42
+ result: task?.result,
43
+ error: task?.error ?? '',
44
+ status: task?.status ?? 'failed',
45
+ });
46
+ }
47
+ if (task?.status === 'failed' || !task?.status)
48
+ actions.reject();
49
+ else if (task?.status === 'handled')
50
+ actions.resolve();
51
+ else
52
+ actions.hold();
53
+ };
54
+ return await brokerAPI.registerQueueHandler(this.responseQueueId, `response.${this.handlerId}.#`, responseHandler);
55
+ }
56
+ async sendTaskRequest(process, inputs, options) {
57
+ const brokerAPI = await BrokerAPI.instance();
58
+ const task = await tasks.create({
59
+ data: {
60
+ internal_id: options?.internalId ?? `${process.id}.${v4()}`,
61
+ parent_ids: options?.parentIds ?? [],
62
+ handler_id: this.handlerId,
63
+ process_id: process.id,
64
+ process_name: process.name,
65
+ inputs: inputs,
66
+ },
67
+ });
68
+ console.log(`------ CREATING: ID: ${task.id}`);
69
+ console.log(`------ CREATING: INTERNAL ID: ${task.internal_id}`);
70
+ console.log(`------ CREATING: HANDLER ID: ${task.handler_id}`);
71
+ console.log(`------ CREATING: PROCESS NAME: ${task.handler_id}`);
72
+ if (options?.skipPublish) {
73
+ return Promise.resolve(true);
74
+ }
75
+ const routingKey = `request.${this.handlerId}.${process.name}`;
76
+ return brokerAPI.publish(routingKey, task.id, options);
77
+ }
78
+ }
79
+ export class BaseTaskRunner {
80
+ requestQueueId;
81
+ responseQueueId;
82
+ handlerId;
83
+ handler;
84
+ constructor(queueId, handler) {
85
+ this.handlerId = queueId;
86
+ this.requestQueueId = `request.${queueId}`;
87
+ this.responseQueueId = `response.${queueId}`;
88
+ this.handler = handler;
89
+ }
90
+ async listen(processName) {
91
+ const taskRunner = async (taskId, actions) => {
92
+ let task = await tasks.findFirst({ where: { id: taskId } });
93
+ let response;
94
+ try {
95
+ response = await this.handler(task, this);
96
+ }
97
+ catch (err) {
98
+ response = {
99
+ status: 'failed',
100
+ error: JSON.stringify(err),
101
+ };
102
+ }
103
+ task = await tasks.update({
104
+ where: { id: taskId },
105
+ data: {
106
+ result: response.result ?? {},
107
+ error: response.error ?? '',
108
+ status: response.status,
109
+ },
110
+ });
111
+ console.log(`[BROKER] !${response.status}: ${task.internal_id} (${task.id})`);
112
+ if (response.error) {
113
+ console.log(`[BROKER] Reason: ${response.error}`);
114
+ }
115
+ console.log(`RESPONSE FOR ${task?.internal_id}: `, response);
116
+ console.log(`RESULT FOR ${task?.internal_id}: `, response.result);
117
+ console.log(`LATEST TASK FOR ${task?.internal_id}: `, task);
118
+ if (response.error)
119
+ actions.reject();
120
+ else
121
+ actions.resolve();
122
+ const responseRoutingKey = `response.${this.handlerId}.${task.process_name}`;
123
+ await brokerAPI.publish(responseRoutingKey, taskId);
124
+ };
125
+ const brokerAPI = await BrokerAPI.instance();
126
+ await brokerAPI.registerQueueHandler(this.requestQueueId, `request.${this.handlerId}.${processName}.#`, taskRunner);
127
+ }
128
+ }
129
129
  //# sourceMappingURL=base.task.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterinternship/tasks",
3
- "version": "2.4.7",
3
+ "version": "2.4.8",
4
4
  "description": "Library of common tasks, executed through RabbitMQ.",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",