@webda/async 3.9.3 → 4.0.0-beta.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.
Files changed (41) hide show
  1. package/README.md +92 -8
  2. package/lib/asyncaction.model.d.ts +207 -0
  3. package/lib/asyncaction.model.d.ts.map +1 -0
  4. package/lib/asyncaction.model.js +269 -0
  5. package/lib/asyncaction.model.js.map +1 -0
  6. package/lib/index.d.ts +7 -5
  7. package/lib/index.d.ts.map +1 -0
  8. package/lib/index.js +6 -5
  9. package/lib/index.js.map +1 -1
  10. package/lib/services/asyncevents.service.d.ts +154 -0
  11. package/lib/services/asyncevents.service.d.ts.map +1 -0
  12. package/lib/services/asyncevents.service.js +219 -0
  13. package/lib/services/asyncevents.service.js.map +1 -0
  14. package/lib/services/{asyncjobservice.d.ts → asyncjobservice.service.d.ts} +81 -87
  15. package/lib/services/asyncjobservice.service.d.ts.map +1 -0
  16. package/lib/services/asyncjobservice.service.js +596 -0
  17. package/lib/services/asyncjobservice.service.js.map +1 -0
  18. package/lib/services/{localrunner.d.ts → localrunner.service.d.ts} +24 -9
  19. package/lib/services/localrunner.service.d.ts.map +1 -0
  20. package/lib/services/localrunner.service.js +87 -0
  21. package/lib/services/localrunner.service.js.map +1 -0
  22. package/lib/services/{runner.d.ts → runner.service.d.ts} +17 -11
  23. package/lib/services/runner.service.d.ts.map +1 -0
  24. package/lib/services/{runner.js → runner.service.js} +16 -13
  25. package/lib/services/runner.service.js.map +1 -0
  26. package/lib/services/{servicerunner.d.ts → servicerunner.service.d.ts} +39 -37
  27. package/lib/services/servicerunner.service.d.ts.map +1 -0
  28. package/lib/services/servicerunner.service.js +131 -0
  29. package/lib/services/servicerunner.service.js.map +1 -0
  30. package/package.json +35 -39
  31. package/webda.module.json +1910 -868
  32. package/lib/models.d.ts +0 -169
  33. package/lib/models.js +0 -177
  34. package/lib/models.js.map +0 -1
  35. package/lib/services/asyncjobservice.js +0 -521
  36. package/lib/services/asyncjobservice.js.map +0 -1
  37. package/lib/services/localrunner.js +0 -70
  38. package/lib/services/localrunner.js.map +0 -1
  39. package/lib/services/runner.js.map +0 -1
  40. package/lib/services/servicerunner.js +0 -152
  41. package/lib/services/servicerunner.js.map +0 -1
@@ -0,0 +1,154 @@
1
+ import { Queue, Service, ServiceParameters } from "@webda/core";
2
+ import type { ModelClass } from "@webda/core";
3
+ /**
4
+ * AsyncEvent representation
5
+ */
6
+ export declare class AsyncEvent {
7
+ /**
8
+ * Service emitted the event
9
+ */
10
+ service: string;
11
+ /**
12
+ * Type of event
13
+ */
14
+ type: string;
15
+ /**
16
+ * Payload of the event
17
+ */
18
+ payload: any;
19
+ /**
20
+ * Time
21
+ */
22
+ time: Date;
23
+ /**
24
+ * Used when serializing a service
25
+ */
26
+ static ServiceTag: string;
27
+ /**
28
+ * @param service - the emitter: a service, a model class or its serialized name
29
+ * @param type - the event type
30
+ * @param payload - the event payload
31
+ */
32
+ constructor(service: string | Service | ModelClass, type: string, payload?: any);
33
+ /**
34
+ * Allow payload to contain Service but do not serialize them
35
+ * replacing them by a #Webda:Service:${service.getName()} so it
36
+ * can be revived
37
+ *
38
+ * @returns the serializable event
39
+ */
40
+ toJSON(): this & {
41
+ payload: string;
42
+ };
43
+ /**
44
+ * Deserialize from the queue, reviving any detected service
45
+ *
46
+ * @param data - the serialized event
47
+ * @returns the event
48
+ */
49
+ static fromQueue(data: any): AsyncEvent;
50
+ /**
51
+ * Mapper name
52
+ * @returns the mapper key
53
+ */
54
+ getMapper(): string;
55
+ }
56
+ /**
57
+ * Queues by name
58
+ */
59
+ interface QueueMap {
60
+ [key: string]: Queue;
61
+ }
62
+ /**
63
+ * @inheritdoc
64
+ */
65
+ export declare class EventServiceParameters extends ServiceParameters {
66
+ /**
67
+ * Queues to post async events to
68
+ */
69
+ queues?: {
70
+ [key: string]: string;
71
+ };
72
+ /**
73
+ * Make the event sending asynchronous
74
+ */
75
+ sync?: boolean;
76
+ /**
77
+ * @override
78
+ * @param params - the input parameters
79
+ * @returns this
80
+ */
81
+ load(params?: any): this;
82
+ }
83
+ /**
84
+ * @category CoreServices
85
+ * @WebdaModda AsyncEvents
86
+ */
87
+ declare class EventService<T extends EventServiceParameters = EventServiceParameters> extends Service<T> {
88
+ _callbacks: any;
89
+ _queues: QueueMap;
90
+ _defaultQueue: string;
91
+ _async: boolean;
92
+ /**
93
+ * Resolve the queues
94
+ * @returns this
95
+ */
96
+ resolve(): this;
97
+ /**
98
+ * Bind a asynchronous event
99
+ *
100
+ * ```mermaid
101
+ * sequenceDiagram
102
+ * participant S as Service
103
+ * participant As as AsyncEventService
104
+ * participant Q as Queue
105
+ * participant Aw as AsyncEventService Worker
106
+ *
107
+ * As->>S: Bind event to a sendQueue listener
108
+ * activate As
109
+ * S->>As: Emit event
110
+ * As->>Q: Push the event to the queue
111
+ * deactivate As
112
+ * Aw->>Q: Consume queue
113
+ * Aw->>Aw: Call the original listener
114
+ * ```
115
+ *
116
+ * @param service - the service or model class emitting the event
117
+ * @param event - the event name
118
+ * @param callback - the listener
119
+ * @param queue - the queue to use, default queue if not specified
120
+ */
121
+ bindAsyncListener(service: Service | ModelClass, event: string, callback: any, queue?: string): void;
122
+ /**
123
+ * Synchronous Listener to proxy to async
124
+ *
125
+ * @param service - the emitter
126
+ * @param type - the event type
127
+ * @param queue - the queue to use
128
+ * @param payload - the event payload
129
+ * @returns the send or handle promise
130
+ */
131
+ pushEvent(service: Service | ModelClass, type: string, queue: string, payload: any): Promise<void>;
132
+ /**
133
+ * Process one event
134
+ *
135
+ * @param event - the event
136
+ * @returns a promise resolved once all listeners are done
137
+ */
138
+ protected handleEvent(event: AsyncEvent): Promise<void>;
139
+ /**
140
+ * Process a serialized event
141
+ * @param event serialized event
142
+ * @returns the handle promise
143
+ */
144
+ protected handleRawEvent(event: AsyncEvent): Promise<void>;
145
+ /**
146
+ * Process asynchronous event on queue
147
+ * @param queue - the queue to consume
148
+ * @returns the consumer promise
149
+ */
150
+ worker(queue?: string): Promise<void>;
151
+ }
152
+ export { EventService };
153
+ export type { QueueMap };
154
+ //# sourceMappingURL=asyncevents.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"asyncevents.service.d.ts","sourceRoot":"","sources":["../../src/services/asyncevents.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAoC,MAAM,aAAa,CAAC;AAClG,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C;;GAEG;AACH,qBAAa,UAAU;IACrB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,OAAO,EAAE,GAAG,CAAC;IACb;;OAEG;IACH,IAAI,EAAE,IAAI,CAAC;IAEX;;OAEG;IACH,MAAM,CAAC,UAAU,SAAqB;IAEtC;;;;OAIG;IACH,YAAY,OAAO,EAAE,MAAM,GAAG,OAAO,GAAG,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,GAAQ,EAWlF;IAED;;;;;;OAMG;IACH,MAAM;;MAYL;IAED;;;;;OAKG;IACH,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,GAAG,cAazB;IAED;;;OAGG;IACH,SAAS,WAER;CACF;AAED;;GAEG;AACH,UAAU,QAAQ;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK,CAAC;CACtB;AAED;;GAEG;AACH,qBAAa,sBAAuB,SAAQ,iBAAiB;IAC3D;;OAEG;IACH,MAAM,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;IACnC;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;IAEf;;;;OAIG;IACH,IAAI,CAAC,MAAM,GAAE,GAAQ,GAAG,IAAI,CAK3B;CACF;AAED;;;GAGG;AACH,cAAM,YAAY,CAAC,CAAC,SAAS,sBAAsB,GAAG,sBAAsB,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAC;IAC9F,UAAU,EAAE,GAAG,CAAM;IACrB,OAAO,EAAE,QAAQ,CAAM;IACvB,aAAa,EAAE,MAAM,CAAM;IAC3B,MAAM,EAAE,OAAO,CAAC;IAEhB;;;OAGG;IACH,OAAO,IAAI,IAAI,CAiBd;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,iBAAiB,CAAC,OAAO,EAAE,OAAO,GAAG,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,KAAA,EAAE,KAAK,CAAC,EAAE,MAAM,QAiBvF;IAED;;;;;;;;OAQG;IACG,SAAS,CAAC,OAAO,EAAE,OAAO,GAAG,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,iBAOvF;IAED;;;;;OAKG;IACH,UAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAY5D;IAED;;;;OAIG;IACH,UAAgB,cAAc,CAAC,KAAK,EAAE,UAAU,iBAE/C;IAED;;;;OAIG;IACH,MAAM,CAAC,KAAK,GAAE,MAA2B,GAAG,OAAO,CAAC,IAAI,CAAC,CAIxD;CACF;AAED,OAAO,EAAE,YAAY,EAAE,CAAC;AACxB,YAAY,EAAE,QAAQ,EAAE,CAAC"}
@@ -0,0 +1,219 @@
1
+ import { Service, ServiceParameters, useDynamicService, useRepository } from "@webda/core";
2
+ /**
3
+ * AsyncEvent representation
4
+ */
5
+ export class AsyncEvent {
6
+ /**
7
+ * @param service - the emitter: a service, a model class or its serialized name
8
+ * @param type - the event type
9
+ * @param payload - the event payload
10
+ */
11
+ constructor(service, type, payload = {}) {
12
+ if (service instanceof Service) {
13
+ this.service = `service:${service.getName()}`;
14
+ }
15
+ else if (typeof service === "string") {
16
+ this.service = service;
17
+ }
18
+ else {
19
+ this.service = `model:${service.name}`;
20
+ }
21
+ this.type = type;
22
+ this.payload = payload;
23
+ this.time = new Date();
24
+ }
25
+ /**
26
+ * Allow payload to contain Service but do not serialize them
27
+ * replacing them by a #Webda:Service:${service.getName()} so it
28
+ * can be revived
29
+ *
30
+ * @returns the serializable event
31
+ */
32
+ toJSON() {
33
+ return {
34
+ ...this,
35
+ payload: JSON.stringify(this.payload, function serviceReplacer(key, value) {
36
+ // Service.toJSON is applied before the replacer, check the raw value
37
+ const raw = key === "" ? value : this[key];
38
+ if (raw instanceof Service) {
39
+ return `${AsyncEvent.ServiceTag}${raw.getName()}`;
40
+ }
41
+ return value;
42
+ })
43
+ };
44
+ }
45
+ /**
46
+ * Deserialize from the queue, reviving any detected service
47
+ *
48
+ * @param data - the serialized event
49
+ * @returns the event
50
+ */
51
+ static fromQueue(data) {
52
+ const evt = new AsyncEvent(data.service, data.type, JSON.parse(data.payload, (_key, value) => {
53
+ if (typeof value === "string" && value.startsWith(AsyncEvent.ServiceTag)) {
54
+ return useDynamicService(value.substring(AsyncEvent.ServiceTag.length));
55
+ }
56
+ return value;
57
+ }));
58
+ evt.time = data.time;
59
+ return evt;
60
+ }
61
+ /**
62
+ * Mapper name
63
+ * @returns the mapper key
64
+ */
65
+ getMapper() {
66
+ return this.service + "_" + this.type;
67
+ }
68
+ }
69
+ /**
70
+ * Used when serializing a service
71
+ */
72
+ AsyncEvent.ServiceTag = "#Webda:Service:";
73
+ /**
74
+ * @inheritdoc
75
+ */
76
+ export class EventServiceParameters extends ServiceParameters {
77
+ /**
78
+ * @override
79
+ * @param params - the input parameters
80
+ * @returns this
81
+ */
82
+ load(params = {}) {
83
+ super.load(params);
84
+ this.queues ?? (this.queues = {});
85
+ this.sync ?? (this.sync = false);
86
+ return this;
87
+ }
88
+ }
89
+ /**
90
+ * @category CoreServices
91
+ * @WebdaModda AsyncEvents
92
+ */
93
+ class EventService extends Service {
94
+ constructor() {
95
+ super(...arguments);
96
+ this._callbacks = {};
97
+ this._queues = {};
98
+ this._defaultQueue = "";
99
+ }
100
+ /**
101
+ * Resolve the queues
102
+ * @returns this
103
+ */
104
+ resolve() {
105
+ super.resolve();
106
+ Object.keys(this.parameters.queues).forEach(key => {
107
+ // Define default as first queue
108
+ if (!this._defaultQueue) {
109
+ this._defaultQueue = key;
110
+ }
111
+ this._queues[key] = useDynamicService(this.parameters.queues[key]);
112
+ });
113
+ this._async = !this.parameters.sync;
114
+ // Check we have at least one queue to handle asynchronous
115
+ if (this._async && Object.keys(this._queues).length < 1) {
116
+ this.log("ERROR", "Need at least one queue for async to be ready", this.parameters);
117
+ throw Error("Need at least one queue for async to be ready");
118
+ }
119
+ return this;
120
+ }
121
+ /**
122
+ * Bind a asynchronous event
123
+ *
124
+ * ```mermaid
125
+ * sequenceDiagram
126
+ * participant S as Service
127
+ * participant As as AsyncEventService
128
+ * participant Q as Queue
129
+ * participant Aw as AsyncEventService Worker
130
+ *
131
+ * As->>S: Bind event to a sendQueue listener
132
+ * activate As
133
+ * S->>As: Emit event
134
+ * As->>Q: Push the event to the queue
135
+ * deactivate As
136
+ * Aw->>Q: Consume queue
137
+ * Aw->>Aw: Call the original listener
138
+ * ```
139
+ *
140
+ * @param service - the service or model class emitting the event
141
+ * @param event - the event name
142
+ * @param callback - the listener
143
+ * @param queue - the queue to use, default queue if not specified
144
+ */
145
+ bindAsyncListener(service, event, callback, queue) {
146
+ if (!this._async) {
147
+ throw Error("EventService is not configured for asynchronous");
148
+ }
149
+ if (!queue) {
150
+ queue = this._defaultQueue;
151
+ }
152
+ const mapper = new AsyncEvent(service, event).getMapper();
153
+ if (!this._callbacks[mapper]) {
154
+ if (service instanceof Service) {
155
+ service.on(event, data => this.pushEvent(service, event, queue, data));
156
+ }
157
+ else {
158
+ useRepository(service).on(event, data => this.pushEvent(service, event, queue, data));
159
+ }
160
+ this._callbacks[mapper] = [];
161
+ }
162
+ this._callbacks[mapper].push(callback);
163
+ }
164
+ /**
165
+ * Synchronous Listener to proxy to async
166
+ *
167
+ * @param service - the emitter
168
+ * @param type - the event type
169
+ * @param queue - the queue to use
170
+ * @param payload - the event payload
171
+ * @returns the send or handle promise
172
+ */
173
+ async pushEvent(service, type, queue, payload) {
174
+ const event = new AsyncEvent(service, type, payload);
175
+ if (this._async) {
176
+ return this._queues[queue].sendMessage(event);
177
+ }
178
+ else {
179
+ return this.handleEvent(event);
180
+ }
181
+ }
182
+ /**
183
+ * Process one event
184
+ *
185
+ * @param event - the event
186
+ * @returns a promise resolved once all listeners are done
187
+ */
188
+ async handleEvent(event) {
189
+ if (!this._callbacks[event.getMapper()]) {
190
+ return Promise.reject("Callbacks should not be empty, possible application version mismatch between emitter and worker");
191
+ }
192
+ const promises = [];
193
+ this._callbacks[event.getMapper()].map(executor => {
194
+ promises.push(executor(event.payload, event));
195
+ });
196
+ // Need to handle the failure
197
+ await Promise.all(promises);
198
+ }
199
+ /**
200
+ * Process a serialized event
201
+ * @param event serialized event
202
+ * @returns the handle promise
203
+ */
204
+ async handleRawEvent(event) {
205
+ return this.handleEvent(AsyncEvent.fromQueue(event));
206
+ }
207
+ /**
208
+ * Process asynchronous event on queue
209
+ * @param queue - the queue to consume
210
+ * @returns the consumer promise
211
+ */
212
+ worker(queue = this._defaultQueue) {
213
+ // Avoid loops
214
+ this._async = false;
215
+ return this._queues[queue].consume(this.handleRawEvent.bind(this));
216
+ }
217
+ }
218
+ export { EventService };
219
+ //# sourceMappingURL=asyncevents.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"asyncevents.service.js","sourceRoot":"","sources":["../../src/services/asyncevents.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAS,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAGlG;;GAEG;AACH,MAAM,OAAO,UAAU;IAuBrB;;;;OAIG;IACH,YAAY,OAAsC,EAAE,IAAY,EAAE,OAAO,GAAQ,EAAE;QACjF,IAAI,OAAO,YAAY,OAAO,EAAE,CAAC;YAC/B,IAAI,CAAC,OAAO,GAAG,WAAW,OAAO,C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QAC9C,OAAO,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;IACvD,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,KAAK,GAAW,IAAI,CAAC,aAAa;QACvC,cAAc;QACd,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;IACrE,CAAC;CACF;AAED,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -1,8 +1,11 @@
1
- import { CancelableLoopPromise, CancelablePromise, CloudBinary, Constructor, CoreModelDefinition, CronDefinition, OperationContext, Queue, RequestFilter, Service, ServiceParameters, Store, WebContext, WebdaQL } from "@webda/core";
1
+ import { CronDefinition, OperationContext, Queue, RequestFilter, Service, ServiceParameters, WebContext } from "@webda/core";
2
+ import type { ModelClass } from "@webda/core";
3
+ import * as WebdaQL from "@webda/ql";
4
+ import { CancelableLoopPromise, CancelablePromise } from "@webda/utils";
2
5
  import { WorkerLogLevel } from "@webda/workout";
3
6
  import { JSONSchema7 } from "json-schema";
4
- import { AsyncAction, AsyncActionQueueItem, AsyncOperationAction, AsyncWebdaAction } from "../models.js";
5
- import { Runner } from "./runner.js";
7
+ import { AsyncAction, AsyncActionQueueItem, AsyncOperationAction, AsyncWebdaAction } from "../asyncaction.model.js";
8
+ import { Runner } from "./runner.service.js";
6
9
  /**
7
10
  * Represent a Job information as you will find in env
8
11
  */
@@ -16,14 +19,6 @@ export interface JobInfo {
16
19
  * @inheritdoc
17
20
  */
18
21
  export declare class AsyncJobServiceParameters extends ServiceParameters {
19
- /**
20
- * If we want to expose a way to upload/download binary for the job
21
- *
22
- * It will expose a /download and /upload additional url
23
- *
24
- * @deprecated will be removed in 4.0 to only use object AsyncAction
25
- */
26
- binaryStore?: string;
27
22
  /**
28
23
  * If set runner will be called without queue
29
24
  *
@@ -38,8 +33,7 @@ export declare class AsyncJobServiceParameters extends ServiceParameters {
38
33
  /**
39
34
  * URL to expose job status report hook
40
35
  *
41
- * @default /async/jobs
42
- * @deprecated will be removed in 4.0 to only use object AsyncAction
36
+ * @default /async
43
37
  */
44
38
  url: string;
45
39
  /**
@@ -88,18 +82,6 @@ export declare class AsyncJobServiceParameters extends ServiceParameters {
88
82
  * @default 500
89
83
  */
90
84
  logsLimit: number;
91
- /**
92
- * Define the log format
93
- *
94
- * @default ConsoleLoggerDefaultFormat
95
- */
96
- logFormat?: string;
97
- /**
98
- * How long before saving logs (in ms)
99
- *
100
- * @default 5000
101
- */
102
- logSaveDelay?: number;
103
85
  /**
104
86
  * Model to use when launching async action
105
87
  *
@@ -118,22 +100,23 @@ export declare class AsyncJobServiceParameters extends ServiceParameters {
118
100
  * Generated with `webda operations operations.json`
119
101
  */
120
102
  asyncOperationDefinition?: string;
121
- constructor(params: any);
103
+ /**
104
+ * @override
105
+ * @param params - the input parameters
106
+ * @returns this
107
+ */
108
+ load(params?: any): this;
122
109
  }
123
110
  /**
124
111
  * AsyncService allows you to launch jobs and report status
125
112
  *
126
113
  * @WebdaModda
127
114
  */
128
- export default class AsyncJobService<T extends AsyncJobServiceParameters = AsyncJobServiceParameters> extends Service<T> implements RequestFilter {
115
+ export default class AsyncJobService<T extends AsyncJobServiceParameters = AsyncJobServiceParameters> extends Service<T> implements RequestFilter<WebContext> {
129
116
  /**
130
117
  * Queue for execution
131
118
  */
132
119
  protected queue: Queue<AsyncActionQueueItem>;
133
- /**
134
- * Store for async actions
135
- */
136
- protected store: Store<AsyncAction>;
137
120
  /**
138
121
  * Runner to use
139
122
  */
@@ -142,18 +125,14 @@ export default class AsyncJobService<T extends AsyncJobServiceParameters = Async
142
125
  * HMAC Algorithm used for simple auth
143
126
  */
144
127
  static HMAC_ALGO: string;
145
- /**
146
- * If we want job to be able to upload/download
147
- */
148
- protected binaryStore: CloudBinary;
149
128
  /**
150
129
  * Model to use when launching action
151
130
  */
152
- model: Constructor<AsyncWebdaAction, [string, string, ...any[]]> & CoreModelDefinition<AsyncAction>;
131
+ model: (new (serviceName?: string, method?: string, ...args: any[]) => AsyncWebdaAction) & ModelClass<AsyncWebdaAction>;
153
132
  /**
154
- * Operation Model to use when launching action
133
+ * Model to use when launching operation
155
134
  */
156
- operationModel: Constructor<AsyncOperationAction, [string, OperationContext, WorkerLogLevel?]>;
135
+ operationModel: new (operationId: string, context: OperationContext, logLevel?: WorkerLogLevel) => AsyncOperationAction;
157
136
  /**
158
137
  * Operations definition
159
138
  */
@@ -182,133 +161,148 @@ export default class AsyncJobService<T extends AsyncJobServiceParameters = Async
182
161
  };
183
162
  /**
184
163
  * @inheritdoc
185
- */
186
- loadParameters(params: any): ServiceParameters;
187
- /**
188
- * @inheritdoc
164
+ * @returns this
189
165
  */
190
166
  resolve(): this;
191
167
  /**
192
168
  * Register new operations
193
- * @param operations
169
+ * @param operations - the operations definition
194
170
  */
195
171
  registerOperations(operations: typeof this.operations): void;
196
172
  /**
197
173
  * Allow job status report url
174
+ * @param context - the web context
175
+ * @returns true if the request is a job status report
198
176
  */
199
177
  checkRequest(context: WebContext): Promise<boolean>;
200
178
  /**
201
179
  * Status hook for job report
202
180
  *
203
181
  * Only updates specific fields: status, errorMessage, statusDetails, results, logs (appending)
182
+ * @param context - the web context
204
183
  */
205
184
  protected statusHook(context: WebContext): Promise<void>;
206
185
  /**
207
186
  * Worker
208
187
  *
209
188
  * This will launch actions based on defined runners
189
+ * @returns a cancelable promise
210
190
  */
211
191
  worker(): CancelablePromise<void>;
212
192
  /**
213
193
  * Handle one event from the queue and launch the job
214
- * @param event
194
+ * @param event - the queue item
195
+ * @returns the job promise if any
215
196
  */
216
197
  protected handleEvent(event: AsyncActionQueueItem): Promise<void>;
198
+ /**
199
+ * Absolute url of the status hook remote runners POST their reports to
200
+ *
201
+ * It is this service status route: its request filter lets the job headers through
202
+ * and the HMAC is then verified by the action
203
+ * @returns the hook url
204
+ */
205
+ getHookUrl(): string;
217
206
  /**
218
207
  * Get the job info
219
- * @param action
220
- * @returns
208
+ * @param action - the action
209
+ * @returns the job information
221
210
  */
222
211
  getJobInfo(action: AsyncAction): JobInfo;
223
212
  /**
224
213
  * List available operations through this service
225
- * @param context
214
+ * @param context - the web context
226
215
  */
227
216
  listOperations(context: WebContext<void, {
228
- application: {
229
- name: string;
230
- version: string;
231
- };
232
- operations: {
233
- [key: string]: {
234
- id: string;
235
- input?: string;
236
- output?: string;
237
- };
238
- };
239
- schemas: {
240
- [key: string]: JSONSchema7;
241
- };
242
- } | string[]>): Promise<void>;
217
+ full?: boolean;
218
+ }>): Promise<void>;
243
219
  /**
244
220
  * Call an operation for an external system
245
- * @param context
246
- * @returns
221
+ * @param context - the operation context
222
+ * @returns the launched action
247
223
  */
248
- callOperation(context: OperationContext): Promise<AsyncAction>;
224
+ callOperation(context: OperationContext): Promise<AsyncOperationAction>;
225
+ /**
226
+ * Check the operation exists, is allowed and its input is valid
227
+ * @param context - the operation context
228
+ * @param operationId - the operation id
229
+ */
230
+ checkOperation(context: OperationContext, operationId: string): Promise<void>;
249
231
  /**
250
232
  * Launch an operation through this service
233
+ * @param context - the web context
251
234
  */
252
235
  launchOperation(context: WebContext): Promise<void>;
253
236
  /**
254
237
  * Launch the action asynchronously
255
- * @param action
256
- * @returns
238
+ * @param action - the action
239
+ * @returns the action
257
240
  */
258
- launchAction(action: AsyncAction): Promise<AsyncAction>;
241
+ launchAction<A extends AsyncAction>(action: A): Promise<A>;
259
242
  /**
260
243
  * Schedule action for later execution
261
244
  *
262
- * @param action
263
- * @param timestamp
245
+ * @param action - the action
246
+ * @param timestamp - when to launch it
247
+ * @returns the action
264
248
  */
265
- scheduleAction(action: AsyncAction, timestamp: number): Promise<AsyncAction>;
249
+ scheduleAction<A extends AsyncAction>(action: A, timestamp: number): Promise<A>;
266
250
  /**
267
251
  * Return headers to request status hook
268
252
  *
269
- * @param jobInfo
270
- * @returns
253
+ * @param jobInfo - the job information
254
+ * @returns the headers
271
255
  */
272
256
  getHeaders(jobInfo: JobInfo): {
273
257
  [key: string]: string;
274
258
  };
275
259
  /**
276
260
  * Post hook to a remote url or use local store to update status
277
- * @param jobInfo
278
- * @param message
279
- * @returns
261
+ * @param jobInfo - the job information
262
+ * @param message - the status report
263
+ * @returns the updated action
280
264
  */
281
- postHook(jobInfo: JobInfo, message: any): Promise<AsyncWebdaAction | AsyncOperationAction>;
265
+ postHook(jobInfo: JobInfo, message: any): Promise<AsyncWebdaAction>;
282
266
  /**
283
267
  * Launch a service.method as an AsyncAction
284
- * @param serviceName
285
- * @param method
286
- * @param args
268
+ * @param serviceName - the service
269
+ * @param method - the method
270
+ * @param args - the arguments
271
+ * @returns the action
287
272
  */
288
- launchAsAsyncAction(serviceName: string, method: string, ...args: any[]): Promise<AsyncAction>;
273
+ launchAsAsyncAction(serviceName: string, method: string, ...args: any[]): Promise<AsyncWebdaAction>;
289
274
  /**
290
275
  * Execute a service.method as an AsyncAction
291
276
  *
292
277
  * Useful for crontab execution
293
- * @param serviceName
294
- * @param method
295
- * @param args
278
+ * @param serviceName - the service
279
+ * @param method - the method
280
+ * @param args - the arguments
281
+ * @returns the execution promise
296
282
  */
297
283
  executeAsAsyncAction(serviceName: string, method: string, ...args: any[]): Promise<void>;
284
+ /**
285
+ * Run the AsyncAction described by the JOB_* environment variables
286
+ * @returns the execution promise
287
+ */
288
+ runAction(): Promise<void>;
298
289
  /**
299
290
  * Wrap async job and call the job status hook
291
+ * @param jobInfo - the job information, from environment if not provided
292
+ * @returns a promise resolved once the job is reported
300
293
  */
301
294
  runAsyncOperationAction(jobInfo?: JobInfo): Promise<void>;
302
295
  /**
303
296
  * Get the cron callback function
304
- * @param cron
305
- * @returns
297
+ * @param cron - the cron definition
298
+ * @returns the cron callback
306
299
  */
307
300
  getCronExecutor(cron: CronDefinition): () => Promise<void>;
308
301
  /**
309
302
  * Manage scheduled actions and crontab actions
310
- * @returns
303
+ * @returns a cancelable loop promise
311
304
  */
312
305
  scheduler(): CancelableLoopPromise;
313
306
  }
314
307
  export { AsyncJobService };
308
+ //# sourceMappingURL=asyncjobservice.service.d.ts.map