Skip to content

Commit c33e932

Browse files
committed
Simplify delayed_until check and set default to NOW() if no value is passed
1 parent b7f30e6 commit c33e932

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// UpdateJobDelay.swift
3+
// swift-jobs-postgres
4+
//
5+
// Created by Stevenson Michel on 2/17/25.
6+
//
7+
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This source file is part of the Hummingbird server framework project
11+
//
12+
// Copyright (c) 2024 the Hummingbird authors
13+
// Licensed under Apache License v2.0
14+
//
15+
// See LICENSE.txt for license information
16+
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
17+
//
18+
// SPDX-License-Identifier: Apache-2.0
19+
//
20+
//===----------------------------------------------------------------------===//
21+
22+
import Logging
23+
import PostgresMigrations
24+
import PostgresNIO
25+
26+
struct UpdateJobDelay: DatabaseMigration {
27+
func apply(connection: PostgresConnection, logger: Logger) async throws {
28+
try await connection.query(
29+
"""
30+
ALTER TABLE _hb_pg_job_queue ALTER COLUMN delayed_until TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
31+
""",
32+
logger: logger
33+
)
34+
}
35+
36+
func revert(connection: PostgresConnection, logger: Logger) async throws {
37+
try await connection.query(
38+
"ALTER TABLE _hb_pg_job_queue ALTER COLUMN delayed_until DROP NOT NULL",
39+
logger: logger
40+
)
41+
}
42+
43+
var name: String { "_Update_JobQueueDelay_Table_" }
44+
var group: DatabaseMigrationGroup { .jobQueue }
45+
}

Sources/JobsPostgres/PostgresJobsQueue.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public final class PostgresJobQueue: JobQueueDriver {
112112
await migrations.add(CreateJobQueue())
113113
await migrations.add(CreateJobQueueMetadata())
114114
await migrations.add(CreateJobDelay())
115+
await migrations.add(UpdateJobDelay())
115116
}
116117

117118
/// Run on initialization of the job queue
@@ -203,7 +204,7 @@ public final class PostgresJobQueue: JobQueueDriver {
203204
SELECT
204205
job_id
205206
FROM _hb_pg_job_queue
206-
WHERE (delayed_until IS NULL OR delayed_until <= NOW())
207+
WHERE delayed_until <= NOW()
207208
ORDER BY createdAt, delayed_until ASC
208209
FOR UPDATE SKIP LOCKED
209210
LIMIT 1

0 commit comments

Comments
 (0)