@zintrust/zedgi 3.0.1 → 3.0.3

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 CHANGED
@@ -31,6 +31,10 @@ ZEDGI_REDIS_HEADER=
31
31
  ZEDGI_QUEUE_HEADER=
32
32
  ZEDGI_MYSQL_HEADER=
33
33
  ZEDGI_POSTGRES_HEADER=
34
+
35
+ # Optional Redis credential profile for queues. Falls back to ZEDGI_REDIS_PROFILE.
36
+ ZEDGI_QUEUE_PROFILE=
37
+ ZEDGI_REDIS_PROFILE=
34
38
  ```
35
39
 
36
40
  ## Drivers
@@ -68,6 +72,11 @@ can be used for producers, monitoring, and consumers. When `QUEUE_CONNECTION` an
68
72
  to `queue-zedgi` to keep workers, monitors, and producers on the same Zedgi-backed
69
73
  Redis target.
70
74
 
75
+ When `ZEDGI_QUEUE_PROFILE` is set, the queue driver uses that named Redis
76
+ credential profile. ZinTrust registers the profile with the configured Redis DB
77
+ (`REDIS_QUEUE_DB` or `WORKERS_REDIS_QUEUE_DB`) so BullMQ keys are read from the
78
+ intended logical database.
79
+
71
80
  ## Registration
72
81
 
73
82
  The package registers:
@@ -4,6 +4,7 @@ type RedisConfig = {
4
4
  db?: number;
5
5
  database?: number;
6
6
  header?: Record<string, unknown>;
7
+ profile?: string;
7
8
  };
8
9
  declare const createZedgiMonitorDriver: (config: RedisConfig) => QueueDriver;
9
10
  export { createZedgiMonitorDriver };
@@ -1,4 +1,5 @@
1
1
  import { Logger } from '@zintrust/core/logger';
2
+ import { isWaitingJobState } from '@zintrust/queue-monitor/driver';
2
3
  import { ZedgiRuntime } from './ZedgiRuntime.js';
3
4
  const createZedgiEnqueue = (getQueueConfig) => async (name, payload, options) => {
4
5
  try {
@@ -67,6 +68,35 @@ const createZedgiRetryJob = (getQueueConfig) => async (queueName, jobId) => {
67
68
  throw error;
68
69
  }
69
70
  };
71
+ const waitingStateRejection = (state) => ({
72
+ ok: false,
73
+ status: 'not_waiting',
74
+ reason: `Job is ${typeof state === 'string' && state.trim() !== '' ? state : 'unknown'}, not waiting`,
75
+ });
76
+ const createZedgiDeleteWaitingJob = (getQueueConfig) => async (queueName, jobId) => {
77
+ const queue = ZedgiRuntime.queue(queueName, getQueueConfig());
78
+ const job = (await queue.getJob(jobId));
79
+ if (!job)
80
+ return { ok: false, status: 'missing' };
81
+ if (!isWaitingJobState(job.state))
82
+ return waitingStateRejection(job.state);
83
+ await queue.removeJob(jobId);
84
+ return { ok: true, status: 'removed' };
85
+ };
86
+ const createZedgiRequeueWaitingJob = (getQueueConfig) => async (queueName, jobId) => {
87
+ const queue = ZedgiRuntime.queue(queueName, getQueueConfig());
88
+ const job = (await queue.getJob(jobId));
89
+ if (!job)
90
+ return { ok: false, status: 'missing' };
91
+ if (!isWaitingJobState(job.state))
92
+ return waitingStateRejection(job.state);
93
+ const opts = { ...job.opts };
94
+ delete opts['jobId'];
95
+ await queue.removeJob(jobId);
96
+ const added = await queue.add(job.name ?? `${queueName}-job`, job.data ?? {}, opts);
97
+ const newJobId = added?.id !== undefined && added.id !== null ? String(added.id) : undefined;
98
+ return { ok: true, status: 'requeued', newJobId };
99
+ };
70
100
  const createZedgiRecoverActiveJob = (getQueueConfig) => async (queueName, jobId) => {
71
101
  try {
72
102
  const queue = ZedgiRuntime.queue(queueName, getQueueConfig());
@@ -111,6 +141,7 @@ const createZedgiMonitorDriver = (config) => {
111
141
  password: config.password,
112
142
  database: config.db ?? config.database,
113
143
  header: config.header,
144
+ profile: config.profile,
114
145
  });
115
146
  return {
116
147
  enqueue: createZedgiEnqueue(getQueueConfig),
@@ -120,6 +151,8 @@ const createZedgiMonitorDriver = (config) => {
120
151
  getRecentJobs: createZedgiGetRecentJobs(getQueueConfig),
121
152
  retryJob: createZedgiRetryJob(getQueueConfig),
122
153
  recoverActiveJob: createZedgiRecoverActiveJob(getQueueConfig),
154
+ deleteWaitingJob: createZedgiDeleteWaitingJob(getQueueConfig),
155
+ requeueWaitingJob: createZedgiRequeueWaitingJob(getQueueConfig),
123
156
  getQueues: createZedgiGetQueues(getQueueConfig),
124
157
  close: createZedgiClose(),
125
158
  };
@@ -1,4 +1,5 @@
1
1
  import { Env } from '@zintrust/core/config';
2
+ import { ErrorFactory } from '@zintrust/core/errors';
2
3
  import { generateUuid } from '@zintrust/core/utils';
3
4
  import { ZedgiRuntime } from './ZedgiRuntime.js';
4
5
  const resolveRequestedJobId = (payload) => {
@@ -40,7 +41,7 @@ const callQueueRpc = async (config, queueName, method, payload) => {
40
41
  ...payload,
41
42
  });
42
43
  }
43
- throw new Error('Zedgi Redis RPC hook support is required for queue consumption');
44
+ throw ErrorFactory.createConfigError('Zedgi Redis RPC hook support is required for queue consumption');
44
45
  };
45
46
  const createDriver = (config) => {
46
47
  const queue = (name) => ZedgiRuntime.queue(name, config);
@@ -7,6 +7,9 @@ export declare const ZedgiRuntime: Readonly<{
7
7
  redis(config?: Partial<ZedgiRedisCacheConfig | ZedgiQueueConfig>): import("@zedgi/zedgi-client").RedisClient;
8
8
  queue(name: string, config?: Partial<ZedgiQueueConfig | ZedgiRedisCacheConfig>): Readonly<{
9
9
  add: (jobName: string, data?: unknown, opts?: Record<string, unknown>) => Promise<import("@zedgi/zedgi-client").QueueJob | null>;
10
+ dequeue: (visibilityTimeoutMs?: number) => Promise<import("@zedgi/zedgi-client").QueueJob | null>;
11
+ ack: (id: string, returnValue?: unknown) => Promise<boolean>;
12
+ fail: (id: string, reason?: string) => Promise<boolean>;
10
13
  getJob: (id: string) => Promise<import("@zedgi/zedgi-client").QueueJob | null>;
11
14
  getJobs: (states?: string[], start?: number, end?: number, asc?: boolean) => Promise<(import("@zedgi/zedgi-client").QueueJob | null)[]>;
12
15
  getJobCounts: (...types: string[]) => Promise<Record<string, number>>;
@@ -112,6 +112,15 @@ const redisCredentialFromConfig = (config, fallbackHeaderKey) => {
112
112
  credential['header'] = header;
113
113
  return credential;
114
114
  };
115
+ const resolveCredentialProfile = (config) => {
116
+ if (config === null || typeof config !== 'object')
117
+ return undefined;
118
+ const profile = config.profile;
119
+ if (typeof profile !== 'string')
120
+ return undefined;
121
+ const normalized = profile.trim();
122
+ return normalized === '' ? undefined : normalized;
123
+ };
115
124
  const sqlCredentialFromConfig = (config, fallbackHeaderKey) => {
116
125
  const credential = {};
117
126
  if (hasText(config.username))
@@ -144,10 +153,22 @@ export const ZedgiRuntime = Object.freeze({
144
153
  client = undefined;
145
154
  },
146
155
  redis(config = {}) {
156
+ const explicitProfile = resolveCredentialProfile(config);
157
+ if (explicitProfile !== undefined) {
158
+ const credential = redisCredentialFromConfig(config, 'ZEDGI_REDIS_HEADER');
159
+ registerProfile('redis', explicitProfile, credential);
160
+ return ensureClient().redis(explicitProfile);
161
+ }
147
162
  const credential = memoizeCredential('redis', 'redis', redisCredentialFromConfig(config, 'ZEDGI_REDIS_HEADER'), redisCredentials);
148
163
  return ensureClient().redis(getProfileName(credential));
149
164
  },
150
165
  queue(name, config = {}) {
166
+ const explicitProfile = resolveCredentialProfile(config);
167
+ if (explicitProfile !== undefined) {
168
+ const credential = redisCredentialFromConfig(config, 'ZEDGI_QUEUE_HEADER');
169
+ registerProfile('redis', explicitProfile, credential);
170
+ return ensureClient().queue(name, explicitProfile);
171
+ }
151
172
  const credential = memoizeCredential('redis', 'redis', redisCredentialFromConfig(config, 'ZEDGI_QUEUE_HEADER'), redisCredentials);
152
173
  return ensureClient().queue(name, getProfileName(credential));
153
174
  },
package/dist/types.d.ts CHANGED
@@ -18,6 +18,7 @@ export type ZedgiQueueConfig = {
18
18
  password?: string;
19
19
  database?: number;
20
20
  header?: Record<string, unknown>;
21
+ profile?: string;
21
22
  };
22
23
  export type CacheDriver = {
23
24
  get<T>(key: string): Promise<T | null>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zintrust/zedgi",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
4
4
  "description": "Zedgi HTTPS transport drivers for ZinTrust cache, database, and queue.",
5
5
  "private": false,
6
6
  "type": "module",
@@ -38,7 +38,7 @@
38
38
  "adapter"
39
39
  ],
40
40
  "dependencies": {
41
- "@zedgi/zedgi-client": "^1.0.3"
41
+ "@zedgi/zedgi-client": "^1.1.1"
42
42
  },
43
43
  "scripts": {
44
44
  "build": "tsc -p tsconfig.json",