@lota-sdk/core 0.1.19 → 0.1.20

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.
@@ -4,10 +4,11 @@ import { fileURLToPath } from 'node:url'
4
4
  import type { Job, Worker } from 'bullmq'
5
5
 
6
6
  import { chatLogger } from '../config/logger'
7
+ import { queueJobService } from '../services/queue-job.service'
7
8
  import { truncateText } from '../utils/string'
8
9
 
9
- export const DEFAULT_JOB_RETENTION = { removeOnComplete: 200, removeOnFail: 200 }
10
- export const LOW_JOB_RETENTION = { removeOnComplete: 50, removeOnFail: 50 }
10
+ export const DEFAULT_JOB_RETENTION = { removeOnComplete: true, removeOnFail: { age: 24 * 60 * 60, count: 200 } }
11
+ export const LOW_JOB_RETENTION = { removeOnComplete: true, removeOnFail: { age: 6 * 60 * 60, count: 50 } }
11
12
  export const LONG_JOB_LOCK_DURATION_MS = 600_000
12
13
 
13
14
  const DEFAULT_SHUTDOWN_TIMEOUT_MS = 10_000
@@ -30,6 +31,8 @@ interface TracedWorkerJobLike {
30
31
  name: string
31
32
  attemptsMade: number | null | undefined
32
33
  data?: unknown
34
+ opts?: unknown
35
+ timestamp?: number
33
36
  }
34
37
 
35
38
  function truncateTraceString(value: string, maxChars = MAX_TRACE_STRING_CHARS): string {
@@ -143,11 +146,36 @@ export const createWorkerShutdown = (worker: Worker, name: string, logger: typeo
143
146
  }
144
147
  }
145
148
 
146
- export function createTracedWorkerProcessor<TJob extends TracedWorkerJobLike>(
147
- _queueName: string,
148
- processor: (job: TJob) => Promise<void>,
149
- ): (job: TJob) => Promise<void> {
150
- return async (job: TJob) => processor(job)
149
+ export function createTracedWorkerProcessor<TJob extends TracedWorkerJobLike, TResult = void>(
150
+ queueName: string,
151
+ processor: (job: TJob) => Promise<TResult>,
152
+ ): (job: TJob) => Promise<TResult> {
153
+ return async (job: TJob) => {
154
+ const trackedJob = {
155
+ queueName,
156
+ id: typeof job.id === 'string' || typeof job.id === 'number' ? job.id : undefined,
157
+ name: job.name,
158
+ attemptsMade: job.attemptsMade,
159
+ data: job.data,
160
+ opts: job.opts,
161
+ timestamp: job.timestamp,
162
+ }
163
+
164
+ await queueJobService.markAttemptStarted(trackedJob)
165
+
166
+ try {
167
+ const result = await processor(job)
168
+ await queueJobService.markAttemptCompleted(trackedJob, result)
169
+ return result
170
+ } catch (error) {
171
+ try {
172
+ await queueJobService.markAttemptFailed(trackedJob, error)
173
+ } catch (persistenceError) {
174
+ chatLogger.error`Failed to persist queue job failure (queue=${queueName}, job=${job.id}): ${persistenceError}`
175
+ }
176
+ throw error
177
+ }
178
+ }
151
179
  }
152
180
 
153
181
  export const registerShutdownSignals = ({