Skip to content

Commit

Permalink
Remove hardcoding from the queue, check for others. Except for setup … (
Browse files Browse the repository at this point in the history
#350)

Remove hardcoding from the queue, check for others. Except for setup (and upgrade), the table name only comes from config now. Fixes #346
  • Loading branch information
ukd1 authored Mar 21, 2024
1 parent ac45544 commit ef9cbca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/queue_classic/queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,19 @@ def lock
s = <<~SQL
WITH selected_job AS (
SELECT id
FROM queue_classic_jobs
WHERE
locked_at IS NULL AND
q_name = $1 AND
scheduled_at <= now()
FROM #{QC.table_name}
WHERE locked_at IS NULL
AND q_name = $1
AND scheduled_at <= now()
LIMIT 1
FOR NO KEY UPDATE SKIP LOCKED
)
UPDATE queue_classic_jobs
UPDATE #{QC.table_name}
SET
locked_at = now(),
locked_by = pg_backend_pid()
FROM selected_job
WHERE queue_classic_jobs.id = selected_job.id
WHERE #{QC.table_name}.id = selected_job.id
RETURNING *
SQL

Expand Down
18 changes: 18 additions & 0 deletions test/hard_coding_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require_relative 'helper'

class HardCodingTest < Minitest::Test
def test_for_hard_coded_table_names
# This is a simple way to do this, but prolly there could be a better way.
#
# TLDR: do not hard code the table name! It should (at the moment) only appear twice. Once for setup (all the upgrade SQL is currently hardcoded...),
# and once for the config. If you change this test to add more hard coded table names, please reconsider.
#
# Ideally, you should use the config throughout the codebase; more context @ https://github.com/QueueClassic/queue_classic/issues/346
#
#
#
assert_equal `grep queue_classic_jobs lib -R`.split("\n").sort, ['lib/queue_classic/config.rb: @table_name ||= "queue_classic_jobs"', 'lib/queue_classic/setup.rb: conn.execute("DROP TABLE IF EXISTS queue_classic_jobs CASCADE")'].sort
end
end

0 comments on commit ef9cbca

Please sign in to comment.