@@ -131,6 +131,8 @@ public final class PostgresJobQueue: JobQueueDriver {
131
131
case pending = 0
132
132
case processing = 1
133
133
case failed = 2
134
+ case paused = 3
135
+ case cancelled = 4
134
136
}
135
137
136
138
/// Queue configuration
@@ -247,22 +249,51 @@ public final class PostgresJobQueue: JobQueueDriver {
247
249
248
250
/// Retry an existing Job
249
251
/// - Parameters
250
- /// - id : Job instance ID
252
+ /// - jobID : Job instance ID
251
253
/// - jobRequest: Job Request
252
254
/// - options: Job retry options
253
- public func retry< Parameters: JobParameters > ( _ id : JobID , jobRequest: JobRequest < Parameters > , options: JobRetryOptions ) async throws {
255
+ public func retry< Parameters: JobParameters > ( _ jobID : JobID , jobRequest: JobRequest < Parameters > , options: JobRetryOptions ) async throws {
254
256
let buffer = try self . jobRegistry. encode ( jobRequest: jobRequest)
255
257
try await self . client. withTransaction ( logger: self . logger) { connection in
256
- try await self . updateJob ( id : id , buffer: buffer, connection: connection)
258
+ try await self . updateJob ( jobID : jobID , buffer: buffer, connection: connection)
257
259
try await self . addToQueue (
258
- jobID: id ,
260
+ jobID: jobID ,
259
261
queueName: configuration. queueName,
260
262
options: . init( delayUntil: options. delayUntil) ,
261
263
connection: connection
262
264
)
263
265
}
264
266
}
265
267
268
+ /// Perform actions on job
269
+ /// - Parameters
270
+ /// - jobID: Job instance ID
271
+ /// - action Job Action
272
+ public func performAction( jobID: JobID , action: JobAction ) async throws {
273
+ switch action {
274
+ case . cancel ( ) :
275
+ try await self . performJobAction ( jobID: jobID, status: . cancelled)
276
+ case . pause ( ) :
277
+ try await self . performJobAction ( jobID: jobID, status: . paused)
278
+ case . resume ( ) :
279
+ try await self . client. withTransaction ( logger: logger) { connection in
280
+ try await self . setStatus ( jobID: jobID, status: . pending, connection: connection)
281
+ try await self . addToQueue (
282
+ jobID: jobID,
283
+ queueName: configuration. queueName,
284
+ options: . init( ) ,
285
+ connection: connection
286
+ )
287
+ }
288
+ default:
289
+ break
290
+ }
291
+ }
292
+
293
+ public func isEmpty( ) async throws -> Bool {
294
+ true
295
+ }
296
+
266
297
/// This is called to say job has finished processing and it can be deleted
267
298
public func finished( jobID: JobID) async throws {
268
299
try await self . delete ( jobID: jobID)
@@ -408,15 +439,15 @@ public final class PostgresJobQueue: JobQueueDriver {
408
439
logger: self . logger
409
440
)
410
441
}
411
- // TODO: maybe add a new column colum for attempt so far after PR https://github.com/hummingbird-project/swift-jobs/pull/63 is merged?
412
- func updateJob( id : JobID , buffer: ByteBuffer , connection: PostgresConnection ) async throws {
442
+
443
+ func updateJob( jobID : JobID , buffer: ByteBuffer , connection: PostgresConnection ) async throws {
413
444
try await connection. query (
414
445
"""
415
446
UPDATE swift_jobs.jobs
416
447
SET job = \( buffer) ,
417
448
last_modified = \( Date . now) ,
418
449
status = \( Status . failed)
419
- WHERE id = \( id ) AND queue_name = \( configuration. queueName)
450
+ WHERE id = \( jobID ) AND queue_name = \( configuration. queueName)
420
451
""" ,
421
452
logger: self . logger
422
453
)
@@ -485,6 +516,20 @@ public final class PostgresJobQueue: JobQueueDriver {
485
516
return jobs
486
517
}
487
518
519
+ func performJobAction( jobID: JobID , status: Status ) async throws {
520
+ try await self . client. withTransaction ( logger: logger) { connection in
521
+
522
+ try await connection. query (
523
+ """
524
+ DELETE FROM swift_jobs.queues
525
+ WHERE job_id = \( jobID) AND queue_name = \( configuration. queueName)
526
+ """ ,
527
+ logger: self . logger
528
+ )
529
+ try await self . setStatus ( jobID: jobID, status: status, connection: connection)
530
+ }
531
+ }
532
+
488
533
func updateJobsOnInit( withStatus status: Status , onInit: JobCleanup , connection: PostgresConnection ) async throws {
489
534
switch onInit {
490
535
case . remove:
0 commit comments