@rvoh/psychic-workers 0.3.0 → 0.3.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.
Files changed (39) hide show
  1. package/dist/cjs/src/background/BaseBackgroundedModel.js +105 -0
  2. package/dist/cjs/src/background/BaseBackgroundedService.js +71 -0
  3. package/dist/cjs/src/background/BaseScheduledService.js +51 -0
  4. package/dist/cjs/src/background/helpers/nameToRedisQueueName.js +10 -0
  5. package/dist/cjs/src/background/index.js +134 -65
  6. package/dist/cjs/src/error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.js +11 -0
  7. package/dist/cjs/src/error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.js +17 -0
  8. package/dist/cjs/src/error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +11 -0
  9. package/dist/cjs/src/error/background/NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +16 -0
  10. package/dist/cjs/src/psychic-app-workers/index.js +1 -1
  11. package/dist/cjs/src/types/utils.js +2 -0
  12. package/dist/esm/src/background/BaseBackgroundedModel.js +105 -0
  13. package/dist/esm/src/background/BaseBackgroundedService.js +71 -0
  14. package/dist/esm/src/background/BaseScheduledService.js +51 -0
  15. package/dist/esm/src/background/helpers/nameToRedisQueueName.js +7 -0
  16. package/dist/esm/src/background/index.js +124 -55
  17. package/dist/esm/src/error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.js +8 -0
  18. package/dist/esm/src/error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.js +14 -0
  19. package/dist/esm/src/error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +8 -0
  20. package/dist/esm/src/error/background/NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.js +13 -0
  21. package/dist/esm/src/index.js +1 -1
  22. package/dist/esm/src/psychic-app-workers/index.js +1 -1
  23. package/dist/esm/src/types/utils.js +1 -0
  24. package/dist/types/src/background/BaseBackgroundedModel.d.ts +108 -3
  25. package/dist/types/src/background/BaseBackgroundedService.d.ts +73 -2
  26. package/dist/types/src/background/BaseScheduledService.d.ts +53 -2
  27. package/dist/types/src/background/helpers/nameToRedisQueueName.d.ts +2 -0
  28. package/dist/types/src/background/index.d.ts +115 -30
  29. package/dist/types/src/error/background/ActivatingBackgroundWorkersWithoutDefaultWorkerConnection.d.ts +3 -0
  30. package/dist/types/src/error/background/ActivatingNamedQueueBackgroundWorkersWithoutWorkerConnection.d.ts +5 -0
  31. package/dist/types/src/error/background/DefaultBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.d.ts +3 -0
  32. package/dist/types/src/error/background/NamedBullMQNativeOptionsMissingQueueConnectionAndDefaultQueueConnection.d.ts +5 -0
  33. package/dist/types/src/index.d.ts +2 -1
  34. package/dist/types/src/psychic-app-workers/index.d.ts +2 -2
  35. package/dist/types/src/types/background.d.ts +47 -0
  36. package/package.json +2 -2
  37. /package/dist/cjs/src/{background/types.js → types/background.js} +0 -0
  38. /package/dist/esm/src/{background/types.js → types/background.js} +0 -0
  39. /package/dist/types/src/{background/types.d.ts → types/utils.d.ts} +0 -0
@@ -3,13 +3,50 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const dream_1 = require("@rvoh/dream");
4
4
  const index_js_1 = require("./index.js");
5
5
  class BaseBackgroundedModel extends dream_1.Dream {
6
+ /**
7
+ * A getter meant to be overridden in child classes. This does
8
+ * not have to be explicitly provided, but if so, it would allow
9
+ * you to override the default behavior of anything backgrounded
10
+ * by this service, such as the priority or workstream.
11
+ *
12
+ * @returns {object} config - the background job config
13
+ * @returns {string} config.priority - 'default' | 'urgent' | 'not_urgent' | 'last'
14
+ * @returns {string} config.workstream - a workstream name. This would be the name of a workstream, as defined in conf/workers.ts
15
+ * @returns {string} config.queueId - the id of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
16
+ * @returns {string} config.groupId - the groupId of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
17
+ */
6
18
  static get backgroundJobConfig() {
7
19
  return {};
8
20
  }
21
+ /**
22
+ * @internal
23
+ *
24
+ * shadows the static `backgroundJobConfig` getter provided by the user.
25
+ * This should never be overridden, and is meant to provide easy access
26
+ * to the config from within an instance.
27
+ */
9
28
  get backgroundJobConfig() {
10
29
  const klass = this.constructor;
11
30
  return klass.backgroundJobConfig;
12
31
  }
32
+ /**
33
+ * runs the specified method in a background queue, driven by BullMQ,
34
+ * sending in the provided args.
35
+ *
36
+ * ```ts
37
+ * await User.background('myMethod', 'abc', 123)
38
+ * ```
39
+ * though calling background must be awaited, the resolution of the promise
40
+ * is an indication that the job was put in the queue, not that it has
41
+ * completed.
42
+ *
43
+ * NOTE: in test environments, psychic will immediately invoke the underlying
44
+ * method, preventing you from needing to explicitly wait for queues to flush
45
+ * before making assertions.
46
+ *
47
+ * @param methodName - the name of the static method you wish to run in the background
48
+ * @param args - a variadic list of arguments to be sent to your method
49
+ */
13
50
  static async background(methodName, ...args) {
14
51
  const safeThis = this;
15
52
  return await index_js_1.default.staticMethod(safeThis, methodName, {
@@ -18,6 +55,27 @@ class BaseBackgroundedModel extends dream_1.Dream {
18
55
  jobConfig: safeThis.backgroundJobConfig,
19
56
  });
20
57
  }
58
+ /**
59
+ * runs the specified method in a background queue, driven by BullMQ,
60
+ * sending in the provided args, including a delay in seconds, which
61
+ * can be used to hold off the job for a certain amount of time after
62
+ * it is entered into the queue.
63
+ *
64
+ * ```ts
65
+ * await User.backgroundWithDelay('myMethod', 'abc', 123)
66
+ * ```
67
+ * though calling background must be awaited, the resolution of the promise
68
+ * is an indication that the job was put in the queue, not that it has
69
+ * completed.
70
+ *
71
+ * NOTE: in test environments, psychic will immediately invoke the underlying
72
+ * method, preventing you from needing to explicitly wait for queues to flush
73
+ * before making assertions.
74
+ *
75
+ * @param delaySeconds - the amount of time (in seconds) you want to hold off before allowing the job to run
76
+ * @param methodName - the name of the static method you wish to run in the background
77
+ * @param args - a variadic list of arguments to be sent to your method
78
+ */
21
79
  static async backgroundWithDelay(delaySeconds, methodName, ...args) {
22
80
  const safeThis = this;
23
81
  return await index_js_1.default.staticMethod(safeThis, methodName, {
@@ -27,10 +85,35 @@ class BaseBackgroundedModel extends dream_1.Dream {
27
85
  jobConfig: safeThis.backgroundJobConfig,
28
86
  });
29
87
  }
88
+ /**
89
+ * types composed by psychic must be provided, since psychic-workers leverages
90
+ * the sync command in psychic to read your backgroundable services and extract
91
+ * metadata, which can be used to help provide types for the underlying methods
92
+ * in psychic-workers.
93
+ */
30
94
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
31
95
  get psychicTypes() {
32
96
  throw new Error('Must define psychicTypes getter in BackgroundedService class within your application');
33
97
  }
98
+ /**
99
+ * runs the specified method in a background queue, driven by BullMQ,
100
+ * sending in the provided args.
101
+ *
102
+ * ```ts
103
+ * const user = await User.lastOrFail()
104
+ * await user.background('myMethod', 'abc', 123)
105
+ * ```
106
+ * though calling background must be awaited, the resolution of the promise
107
+ * is an indication that the job was put in the queue, not that it has
108
+ * completed.
109
+ *
110
+ * NOTE: in test environments, psychic will immediately invoke the underlying
111
+ * method, preventing you from needing to explicitly wait for queues to flush
112
+ * before making assertions.
113
+ *
114
+ * @param methodName - the name of the static method you wish to run in the background
115
+ * @param args - a variadic list of arguments to be sent to your method
116
+ */
34
117
  async background(methodName, ...args) {
35
118
  const safeThis = this;
36
119
  return await index_js_1.default.modelInstanceMethod(safeThis, methodName, {
@@ -38,6 +121,28 @@ class BaseBackgroundedModel extends dream_1.Dream {
38
121
  jobConfig: safeThis.backgroundJobConfig,
39
122
  });
40
123
  }
124
+ /**
125
+ * runs the specified method in a background queue, driven by BullMQ,
126
+ * sending in the provided args, including a delay in seconds, which
127
+ * can be used to hold off the job for a certain amount of time after
128
+ * it is entered into the queue.
129
+ *
130
+ * ```ts
131
+ * const user = await User.lastOrFail()
132
+ * await user.backgroundWithDelay('myMethod', 'abc', 123)
133
+ * ```
134
+ * though calling background must be awaited, the resolution of the promise
135
+ * is an indication that the job was put in the queue, not that it has
136
+ * completed.
137
+ *
138
+ * NOTE: in test environments, psychic will immediately invoke the underlying
139
+ * method, preventing you from needing to explicitly wait for queues to flush
140
+ * before making assertions.
141
+ *
142
+ * @param delaySeconds - the amount of time (in seconds) you want to hold off before allowing the job to run
143
+ * @param methodName - the name of the static method you wish to run in the background
144
+ * @param args - a variadic list of arguments to be sent to your method
145
+ */
41
146
  async backgroundWithDelay(delaySeconds, methodName, ...args) {
42
147
  const safeThis = this;
43
148
  return await index_js_1.default.modelInstanceMethod(safeThis, methodName, {
@@ -3,18 +3,62 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const dream_1 = require("@rvoh/dream");
4
4
  const index_js_1 = require("./index.js");
5
5
  class BaseBackgroundedService {
6
+ /**
7
+ * A getter meant to be overridden in child classes. This does
8
+ * not have to be explicitly provided, but if so, it would allow
9
+ * you to override the default behavior of anything backgrounded
10
+ * by this service, such as the priority or workstream.
11
+ *
12
+ * @returns {object} config - the background job config
13
+ * @returns {string} config.priority - 'default' | 'urgent' | 'not_urgent' | 'last'
14
+ * @returns {string} config.workstream - a workstream name. This would be the name of a workstream, as defined in conf/workers.ts
15
+ * @returns {string} config.queueId - the id of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
16
+ * @returns {string} config.groupId - the groupId of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
17
+ */
6
18
  static get backgroundJobConfig() {
7
19
  return {};
8
20
  }
21
+ /**
22
+ * @internal
23
+ *
24
+ * Returns a unique global name for the given service.
25
+ *
26
+ * @returns A string representing a unique key for this service
27
+ */
9
28
  static get globalName() {
10
29
  if (!this._globalName)
11
30
  throw new dream_1.GlobalNameNotSet(this);
12
31
  return this._globalName;
13
32
  }
33
+ /**
34
+ * @internal
35
+ *
36
+ * Used by PsychicApplicationWorkers during the load process
37
+ * for services to assign unique global names to each service
38
+ * based on the file name of that model.
39
+ */
14
40
  static setGlobalName(globalName) {
15
41
  this._globalName = globalName;
16
42
  }
17
43
  static _globalName;
44
+ /**
45
+ * runs the specified method in a background queue, driven by BullMQ,
46
+ * sending in the provided args.
47
+ *
48
+ * ```ts
49
+ * await MyBackgroundableClass.background('myMethod', 'abc', 123)
50
+ * ```
51
+ * though calling background must be awaited, the resolution of the promise
52
+ * is an indication that the job was put in the queue, not that it has
53
+ * completed.
54
+ *
55
+ * NOTE: in test environments, psychic will immediately invoke the underlying
56
+ * method, preventing you from needing to explicitly wait for queues to flush
57
+ * before making assertions.
58
+ *
59
+ * @param methodName - the name of the static method you wish to run in the background
60
+ * @param args - a variadic list of arguments to be sent to your method
61
+ */
18
62
  static async background(methodName, ...args) {
19
63
  const safeThis = this;
20
64
  return await index_js_1.default.staticMethod(safeThis, methodName, {
@@ -23,6 +67,27 @@ class BaseBackgroundedService {
23
67
  jobConfig: safeThis.backgroundJobConfig,
24
68
  });
25
69
  }
70
+ /**
71
+ * runs the specified method in a background queue, driven by BullMQ,
72
+ * sending in the provided args, including a delay in seconds, which
73
+ * can be used to hold off the job for a certain amount of time after
74
+ * it is entered into the queue.
75
+ *
76
+ * ```ts
77
+ * await MyBackgroundableClass.backgroundWithDelay('myMethod', 'abc', 123)
78
+ * ```
79
+ * though calling background must be awaited, the resolution of the promise
80
+ * is an indication that the job was put in the queue, not that it has
81
+ * completed.
82
+ *
83
+ * NOTE: in test environments, psychic will immediately invoke the underlying
84
+ * method, preventing you from needing to explicitly wait for queues to flush
85
+ * before making assertions.
86
+ *
87
+ * @param delaySeconds - the amount of time (in seconds) you want to hold off before allowing the job to run
88
+ * @param methodName - the name of the static method you wish to run in the background
89
+ * @param args - a variadic list of arguments to be sent to your method
90
+ */
26
91
  static async backgroundWithDelay(delaySeconds, methodName, ...args) {
27
92
  const safeThis = this;
28
93
  return await index_js_1.default.staticMethod(safeThis, methodName, {
@@ -32,6 +97,12 @@ class BaseBackgroundedService {
32
97
  jobConfig: safeThis.backgroundJobConfig,
33
98
  });
34
99
  }
100
+ /**
101
+ * types composed by psychic must be provided, since psychic-workers leverages
102
+ * the sync command in psychic to read your backgroundable services and extract
103
+ * metadata, which can be used to help provide types for the underlying methods
104
+ * in psychic-workers.
105
+ */
35
106
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
107
  get psychicTypes() {
37
108
  throw new Error('Must define psychicTypes getter in BackgroundedService class within your application');
@@ -3,18 +3,63 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const dream_1 = require("@rvoh/dream");
4
4
  const index_js_1 = require("./index.js");
5
5
  class BaseScheduledService {
6
+ /**
7
+ * A getter meant to be overridden in child classes. This does
8
+ * not have to be explicitly provided, but if so, it would allow
9
+ * you to override the default behavior of anything backgrounded
10
+ * by this service, such as the priority or workstream.
11
+ *
12
+ * @returns {object} config - the background job config
13
+ * @returns {string} config.priority - 'default' | 'urgent' | 'not_urgent' | 'last'
14
+ * @returns {string} config.workstream - a workstream name. This would be the name of a workstream, as defined in conf/workers.ts
15
+ * @returns {string} config.queueId - the id of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
16
+ * @returns {string} config.groupId - the groupId of the BullMQ queue you wish to connect to. This can only be provided if workstream is not provided.
17
+ */
6
18
  static get backgroundJobConfig() {
7
19
  return {};
8
20
  }
21
+ /**
22
+ * @internal
23
+ *
24
+ * Returns a unique global name for the given service.
25
+ *
26
+ * @returns A string representing a unique key for this service
27
+ */
9
28
  static get globalName() {
10
29
  if (!this._globalName)
11
30
  throw new dream_1.GlobalNameNotSet(this);
12
31
  return this._globalName;
13
32
  }
33
+ /**
34
+ * @internal
35
+ *
36
+ * Used by PsychicApplicationWorkers during the load process
37
+ * for services to assign unique global names to each service
38
+ * based on the file name of that model.
39
+ */
14
40
  static setGlobalName(globalName) {
15
41
  this._globalName = globalName;
16
42
  }
17
43
  static _globalName;
44
+ /**
45
+ * Schedules a job to be run repeatedly at a certain cron interval
46
+ * sending in the provided args.
47
+ *
48
+ * ```ts
49
+ * await MySchedulableClass.schedule('0 * * * *', 'myHourlyMethod', 'abc', 123)
50
+ * ```
51
+ * though calling background must be awaited, the resolution of the promise
52
+ * is an indication that the job was put in the queue, not that it has
53
+ * completed.
54
+ *
55
+ * NOTE: in test environments, psychic will immediately invoke the underlying
56
+ * method, preventing you from needing to explicitly wait for queues to flush
57
+ * before making assertions.
58
+ *
59
+ * @param pattern - A cron string representing the time interval you wish this to run on
60
+ * @param methodName - the name of the static method you wish to run in the background
61
+ * @param args - a variadic list of arguments to be sent to your method
62
+ */
18
63
  static async schedule(pattern, methodName, ...args) {
19
64
  const safeThis = this;
20
65
  return await index_js_1.default.scheduledMethod(safeThis, pattern, methodName, {
@@ -23,6 +68,12 @@ class BaseScheduledService {
23
68
  jobConfig: safeThis.backgroundJobConfig,
24
69
  });
25
70
  }
71
+ /**
72
+ * types composed by psychic must be provided, since psychic-workers leverages
73
+ * the sync command in psychic to read your backgroundable services and extract
74
+ * metadata, which can be used to help provide types for the underlying methods
75
+ * in psychic-workers.
76
+ */
26
77
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
78
  get psychicTypes() {
28
79
  throw new Error('Must define psychicTypes getter in BackgroundedService class within your application');
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = nameToRedisQueueName;
4
+ const ioredis_1 = require("ioredis");
5
+ function nameToRedisQueueName(queueName, redis) {
6
+ queueName = queueName.replace(/\{|\}/g, '');
7
+ if (redis instanceof ioredis_1.Cluster)
8
+ return `{${queueName}}`;
9
+ return queueName;
10
+ }