keryx 0.21.9 → 0.22.0
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/classes/Action.ts
CHANGED
|
@@ -181,10 +181,12 @@ export abstract class Action {
|
|
|
181
181
|
* action's `inputs` Zod schema (falls back to `Record<string, unknown>` when no schema is
|
|
182
182
|
* defined). By the time `run` is called, all middleware `runBefore` hooks have already
|
|
183
183
|
* executed and may have mutated the params.
|
|
184
|
-
* @param connection - The connection that initiated this action.
|
|
185
|
-
* caller's session (`connection.session`), subscription state, and raw
|
|
186
|
-
*
|
|
187
|
-
*
|
|
184
|
+
* @param connection - The connection that initiated this action. Always defined. Provides
|
|
185
|
+
* access to the caller's session (`connection.session`), subscription state, and raw
|
|
186
|
+
* transport handle. For background tasks (resque worker), `connection.type === "task"`
|
|
187
|
+
* and `connection.session.data` is empty — tasks are fresh starts, so actions that
|
|
188
|
+
* need user context should receive it as an input parameter rather than reading from
|
|
189
|
+
* the session.
|
|
188
190
|
* @param abortSignal - An `AbortSignal` tied to the action's timeout. The signal is aborted
|
|
189
191
|
* when the per-action `timeout` (or the global `config.actions.timeout`, default 300 000 ms)
|
|
190
192
|
* elapses. Long-running actions should check `abortSignal.aborted` or pass the signal to
|
|
@@ -194,7 +196,7 @@ export abstract class Action {
|
|
|
194
196
|
*/
|
|
195
197
|
abstract run(
|
|
196
198
|
params: ActionParams<Action>,
|
|
197
|
-
connection
|
|
199
|
+
connection: Connection,
|
|
198
200
|
abortSignal?: AbortSignal,
|
|
199
201
|
): Promise<any>;
|
|
200
202
|
}
|
package/initializers/resque.ts
CHANGED
|
@@ -279,9 +279,11 @@ export class Resque extends Initializer {
|
|
|
279
279
|
};
|
|
280
280
|
|
|
281
281
|
/**
|
|
282
|
-
* Wrap an action as a node-resque job. Creates a
|
|
283
|
-
*
|
|
284
|
-
*
|
|
282
|
+
* Wrap an action as a node-resque job. Creates a fresh `Connection` of type `"task"`
|
|
283
|
+
* with an empty in-memory session stub (tasks are fresh starts — no Redis read/write
|
|
284
|
+
* for session), converts inputs to a plain object, and runs the action via
|
|
285
|
+
* `connection.act()`. Handles fan-out result/error collection and recurring task
|
|
286
|
+
* re-enqueue.
|
|
285
287
|
*/
|
|
286
288
|
wrapActionAsJob = (
|
|
287
289
|
action: Action,
|
|
@@ -291,18 +293,6 @@ export class Resque extends Initializer {
|
|
|
291
293
|
pluginOptions: {},
|
|
292
294
|
|
|
293
295
|
perform: async function (params: ActionParams<typeof action>) {
|
|
294
|
-
const connection = new Connection(
|
|
295
|
-
"resque",
|
|
296
|
-
`job:${api.process.name}:${SERVER_JOB_COUNTER++}}`,
|
|
297
|
-
);
|
|
298
|
-
|
|
299
|
-
const propagatedCorrelationId = params._correlationId as
|
|
300
|
-
| string
|
|
301
|
-
| undefined;
|
|
302
|
-
if (propagatedCorrelationId) {
|
|
303
|
-
connection.correlationId = propagatedCorrelationId;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
296
|
const plainParams: Record<string, unknown> =
|
|
307
297
|
typeof params === "object" && params !== null
|
|
308
298
|
? Object.fromEntries(
|
|
@@ -312,6 +302,27 @@ export class Resque extends Initializer {
|
|
|
312
302
|
)
|
|
313
303
|
: {};
|
|
314
304
|
|
|
305
|
+
const propagatedCorrelationId = plainParams._correlationId as
|
|
306
|
+
| string
|
|
307
|
+
| undefined;
|
|
308
|
+
|
|
309
|
+
const connection = new Connection(
|
|
310
|
+
"task",
|
|
311
|
+
`job:${api.process.name}:${SERVER_JOB_COUNTER++}`,
|
|
312
|
+
);
|
|
313
|
+
if (propagatedCorrelationId) {
|
|
314
|
+
connection.correlationId = propagatedCorrelationId;
|
|
315
|
+
}
|
|
316
|
+
// Synthesize an empty session in-memory — tasks are fresh starts; needed data
|
|
317
|
+
// must come through action params, not session state.
|
|
318
|
+
connection.session = {
|
|
319
|
+
id: `task:${connection.id}`,
|
|
320
|
+
cookieName: config.session.cookieName,
|
|
321
|
+
createdAt: Date.now(),
|
|
322
|
+
data: {},
|
|
323
|
+
};
|
|
324
|
+
connection.sessionLoaded = true;
|
|
325
|
+
|
|
315
326
|
const fanOutId = plainParams._fanOutId as string | undefined;
|
|
316
327
|
|
|
317
328
|
let response: Awaited<ReturnType<(typeof action)["run"]>>;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@ import type { ActionMiddleware } from "keryx/classes/Action.ts";
|
|
|
3
3
|
|
|
4
4
|
export const SessionMiddleware: ActionMiddleware = {
|
|
5
5
|
runBefore: async (_params, connection: Connection<{ userId?: number }>) => {
|
|
6
|
-
if (!connection.session
|
|
6
|
+
if (!connection.session?.data.userId) {
|
|
7
7
|
throw new TypedError({
|
|
8
8
|
message: "Session not found",
|
|
9
9
|
type: ErrorType.CONNECTION_SESSION_NOT_FOUND,
|