@@ -124,6 +124,7 @@ public final class PostgresJobQueue: JobQueueDriver, CancellableJobQueue, Resuma
124
124
case failed = 2
125
125
case cancelled = 3
126
126
case paused = 4
127
+ case completed = 5
127
128
}
128
129
129
130
/// Queue configuration
@@ -143,10 +144,10 @@ public final class PostgresJobQueue: JobQueueDriver, CancellableJobQueue, Resuma
143
144
pollTime: Duration = . milliseconds( 100 ) ,
144
145
queueName: String = " default " ,
145
146
retentionPolicy: RetentionPolicy = . init(
146
- canceled : . init( duration: " 7D " ) ,
147
- completed: . init( duration: " 7D " ) ,
148
- failed: . init( duration: " 7D " )
149
- ) //.keepAll()
147
+ cancelled : . init( duration: 7 * 24 * 60 ) ,
148
+ completed: . init( duration: 7 * 24 * 60 ) ,
149
+ failed: . init( duration: 7 * 24 * 60 )
150
+ )
150
151
) {
151
152
self . pollTime = pollTime
152
153
self . queueName = queueName
@@ -192,8 +193,9 @@ public final class PostgresJobQueue: JobQueueDriver, CancellableJobQueue, Resuma
192
193
public func cancel( jobID: JobID ) async throws {
193
194
try await self . client. withTransaction ( logger: logger) { connection in
194
195
try await deleteFromQueue ( jobID: jobID, connection: connection)
195
- try await delete ( jobID: jobID)
196
+ try await setStatus ( jobID: jobID, status : . cancelled , connection : connection )
196
197
}
198
+ try await processDataRetentionPolicy ( )
197
199
}
198
200
199
201
/// Pause job
@@ -298,15 +300,15 @@ public final class PostgresJobQueue: JobQueueDriver, CancellableJobQueue, Resuma
298
300
299
301
/// Retry an existing Job
300
302
/// - Parameters
301
- /// - id : Job instance ID
303
+ /// - jobID : Job instance ID
302
304
/// - jobRequest: Job Request
303
305
/// - options: Job retry options
304
- public func retry< Parameters: JobParameters > ( _ id : JobID , jobRequest: JobRequest < Parameters > , options: JobRetryOptions ) async throws {
306
+ public func retry< Parameters: JobParameters > ( _ jobID : JobID , jobRequest: JobRequest < Parameters > , options: JobRetryOptions ) async throws {
305
307
let buffer = try self . jobRegistry. encode ( jobRequest: jobRequest)
306
308
try await self . client. withTransaction ( logger: self . logger) { connection in
307
- try await self . updateJob ( id : id , buffer: buffer, connection: connection)
309
+ try await self . updateJob ( jobID : jobID , buffer: buffer, connection: connection)
308
310
try await self . addToQueue (
309
- jobID: id ,
311
+ jobID: jobID ,
310
312
queueName: configuration. queueName,
311
313
options: . init( delayUntil: options. delayUntil) ,
312
314
connection: connection
@@ -316,7 +318,9 @@ public final class PostgresJobQueue: JobQueueDriver, CancellableJobQueue, Resuma
316
318
317
319
/// This is called to say job has finished processing and it can be deleted
318
320
public func finished( jobID: JobID ) async throws {
319
- try await self . delete ( jobID: jobID)
321
+ //try await self.delete(jobID: jobID)
322
+ try await self . setStatus ( jobID: jobID, status: . completed)
323
+ try await processDataRetentionPolicy ( )
320
324
}
321
325
322
326
/// This is called to say job has failed to run and should be put aside
@@ -459,15 +463,59 @@ public final class PostgresJobQueue: JobQueueDriver, CancellableJobQueue, Resuma
459
463
logger: self . logger
460
464
)
461
465
}
462
- // TODO: maybe add a new column colum for attempt so far after PR https://github.com/hummingbird-project/swift-jobs/pull/63 is merged?
463
- func updateJob( id: JobID , buffer: ByteBuffer , connection: PostgresConnection ) async throws {
466
+
467
+ func processDataRetentionPolicy( ) async throws {
468
+ try await withThrowingTaskGroup ( of: Void . self) { group in
469
+ try await self . client. withTransaction ( logger: logger) { tx in
470
+ let now = Date . now. timeIntervalSince1970
471
+ let retentionPolicy = configuration. retentionPolicy
472
+ /// process // cancelled events
473
+ group. addTask {
474
+ try await tx. query (
475
+ """
476
+ DELETE FROM swift_jobs.jobs
477
+ WHERE status = \( Status . cancelled)
478
+ AND extract(epoch FROM last_modified)::int + \( retentionPolicy. cancelled. duration) < \( now)
479
+ """ ,
480
+ logger: self . logger
481
+ )
482
+ }
483
+
484
+ /// process failed events clean up
485
+ group. addTask {
486
+ try await tx. query (
487
+ """
488
+ DELETE FROM swift_jobs.jobs
489
+ WHERE status = \( Status . failed)
490
+ AND extract(epoch FROM last_modified)::int + \( retentionPolicy. failed. duration) < \( now)
491
+ """ ,
492
+ logger: self . logger
493
+ )
494
+ }
495
+
496
+ /// process completed events
497
+ group. addTask {
498
+ try await tx. query (
499
+ """
500
+ DELETE FROM swift_jobs.jobs
501
+ WHERE status = \( Status . completed)
502
+ AND extract(epoch FROM last_modified)::int + \( retentionPolicy. completed. duration) < \( now)
503
+ """ ,
504
+ logger: self . logger
505
+ )
506
+ }
507
+ }
508
+ }
509
+ }
510
+
511
+ func updateJob( jobID: JobID , buffer: ByteBuffer , connection: PostgresConnection ) async throws {
464
512
try await connection. query (
465
513
"""
466
514
UPDATE swift_jobs.jobs
467
515
SET job = \( buffer) ,
468
516
last_modified = \( Date . now) ,
469
517
status = \( Status . failed)
470
- WHERE id = \( id ) AND queue_name = \( configuration. queueName)
518
+ WHERE id = \( jobID ) AND queue_name = \( configuration. queueName)
471
519
""" ,
472
520
logger: self . logger
473
521
)
0 commit comments