inst-jobs 2.3.1 → 2.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +4 -4
  2. data/db/migrate/20101216224513_create_delayed_jobs.rb +9 -7
  3. data/db/migrate/20110531144916_cleanup_delayed_jobs_indexes.rb +8 -13
  4. data/db/migrate/20110610213249_optimize_delayed_jobs.rb +8 -8
  5. data/db/migrate/20110831210257_add_delayed_jobs_next_in_strand.rb +25 -25
  6. data/db/migrate/20120510004759_delayed_jobs_delete_trigger_lock_for_update.rb +4 -8
  7. data/db/migrate/20120531150712_drop_psql_jobs_pop_fn.rb +1 -3
  8. data/db/migrate/20120607164022_delayed_jobs_use_advisory_locks.rb +11 -15
  9. data/db/migrate/20120607181141_index_jobs_on_locked_by.rb +1 -1
  10. data/db/migrate/20120608191051_add_jobs_run_at_index.rb +2 -2
  11. data/db/migrate/20120927184213_change_delayed_jobs_handler_to_text.rb +1 -1
  12. data/db/migrate/20140505215510_copy_failed_jobs_original_id.rb +2 -3
  13. data/db/migrate/20150807133223_add_max_concurrent_to_jobs.rb +9 -13
  14. data/db/migrate/20151210162949_improve_max_concurrent.rb +4 -8
  15. data/db/migrate/20161206323555_add_back_default_string_limits_jobs.rb +3 -2
  16. data/db/migrate/20181217155351_speed_up_max_concurrent_triggers.rb +13 -17
  17. data/db/migrate/20200330230722_add_id_to_get_delayed_jobs_index.rb +8 -8
  18. data/db/migrate/20200824222232_speed_up_max_concurrent_delete_trigger.rb +72 -77
  19. data/db/migrate/20200825011002_add_strand_order_override.rb +93 -97
  20. data/db/migrate/20210809145804_add_n_strand_index.rb +12 -0
  21. data/db/migrate/20210812210128_add_singleton_column.rb +203 -0
  22. data/exe/inst_jobs +3 -2
  23. data/lib/delayed/backend/active_record.rb +182 -148
  24. data/lib/delayed/backend/base.rb +80 -69
  25. data/lib/delayed/batch.rb +11 -9
  26. data/lib/delayed/cli.rb +98 -84
  27. data/lib/delayed/core_ext/kernel.rb +4 -2
  28. data/lib/delayed/daemon.rb +70 -74
  29. data/lib/delayed/job_tracking.rb +26 -25
  30. data/lib/delayed/lifecycle.rb +27 -24
  31. data/lib/delayed/log_tailer.rb +17 -17
  32. data/lib/delayed/logging.rb +13 -16
  33. data/lib/delayed/message_sending.rb +42 -51
  34. data/lib/delayed/performable_method.rb +5 -7
  35. data/lib/delayed/periodic.rb +66 -65
  36. data/lib/delayed/plugin.rb +2 -4
  37. data/lib/delayed/pool.rb +198 -192
  38. data/lib/delayed/server/helpers.rb +6 -6
  39. data/lib/delayed/server.rb +51 -54
  40. data/lib/delayed/settings.rb +93 -81
  41. data/lib/delayed/testing.rb +21 -22
  42. data/lib/delayed/version.rb +1 -1
  43. data/lib/delayed/work_queue/in_process.rb +21 -17
  44. data/lib/delayed/work_queue/parent_process/client.rb +55 -53
  45. data/lib/delayed/work_queue/parent_process/server.rb +215 -209
  46. data/lib/delayed/work_queue/parent_process.rb +52 -53
  47. data/lib/delayed/worker/consul_health_check.rb +21 -19
  48. data/lib/delayed/worker/health_check.rb +21 -12
  49. data/lib/delayed/worker/null_health_check.rb +3 -1
  50. data/lib/delayed/worker/process_helper.rb +8 -9
  51. data/lib/delayed/worker.rb +271 -261
  52. data/lib/delayed/yaml_extensions.rb +12 -10
  53. data/lib/delayed_job.rb +37 -38
  54. data/lib/inst-jobs.rb +1 -1
  55. data/spec/active_record_job_spec.rb +128 -135
  56. data/spec/delayed/cli_spec.rb +7 -7
  57. data/spec/delayed/daemon_spec.rb +8 -8
  58. data/spec/delayed/message_sending_spec.rb +8 -9
  59. data/spec/delayed/periodic_spec.rb +13 -12
  60. data/spec/delayed/server_spec.rb +38 -38
  61. data/spec/delayed/settings_spec.rb +26 -25
  62. data/spec/delayed/work_queue/in_process_spec.rb +7 -7
  63. data/spec/delayed/work_queue/parent_process/client_spec.rb +16 -12
  64. data/spec/delayed/work_queue/parent_process/server_spec.rb +43 -40
  65. data/spec/delayed/work_queue/parent_process_spec.rb +21 -21
  66. data/spec/delayed/worker/consul_health_check_spec.rb +22 -22
  67. data/spec/delayed/worker/health_check_spec.rb +51 -49
  68. data/spec/delayed/worker_spec.rb +28 -25
  69. data/spec/gemfiles/52.gemfile +5 -3
  70. data/spec/gemfiles/52.gemfile.lock +240 -0
  71. data/spec/gemfiles/60.gemfile +5 -3
  72. data/spec/gemfiles/60.gemfile.lock +246 -0
  73. data/spec/gemfiles/61.gemfile +5 -3
  74. data/spec/sample_jobs.rb +45 -15
  75. data/spec/shared/delayed_batch.rb +74 -67
  76. data/spec/shared/delayed_method.rb +143 -102
  77. data/spec/shared/performable_method.rb +39 -38
  78. data/spec/shared/shared_backend.rb +537 -437
  79. data/spec/shared/testing.rb +14 -14
  80. data/spec/shared/worker.rb +155 -147
  81. data/spec/shared_jobs_specs.rb +13 -13
  82. data/spec/spec_helper.rb +43 -40
  83. metadata +73 -52
  84. data/lib/delayed/backend/redis/bulk_update.lua +0 -50
  85. data/lib/delayed/backend/redis/destroy_job.lua +0 -2
  86. data/lib/delayed/backend/redis/enqueue.lua +0 -29
  87. data/lib/delayed/backend/redis/fail_job.lua +0 -5
  88. data/lib/delayed/backend/redis/find_available.lua +0 -3
  89. data/lib/delayed/backend/redis/functions.rb +0 -59
  90. data/lib/delayed/backend/redis/get_and_lock_next_available.lua +0 -17
  91. data/lib/delayed/backend/redis/includes/jobs_common.lua +0 -203
  92. data/lib/delayed/backend/redis/job.rb +0 -528
  93. data/lib/delayed/backend/redis/set_running.lua +0 -5
  94. data/lib/delayed/backend/redis/tickle_strand.lua +0 -2
  95. data/spec/redis_job_spec.rb +0 -148
@@ -24,8 +24,8 @@ class AddBackDefaultStringLimitsJobs < ActiveRecord::Migration[4.2]
24
24
  end
25
25
 
26
26
  def drop_triggers
27
- execute %{DROP TRIGGER delayed_jobs_before_insert_row_tr ON delayed_jobs}
28
- execute %{DROP TRIGGER delayed_jobs_after_delete_row_tr ON delayed_jobs}
27
+ execute %(DROP TRIGGER delayed_jobs_before_insert_row_tr ON delayed_jobs)
28
+ execute %(DROP TRIGGER delayed_jobs_after_delete_row_tr ON delayed_jobs)
29
29
  end
30
30
 
31
31
  def readd_triggers
@@ -35,6 +35,7 @@ class AddBackDefaultStringLimitsJobs < ActiveRecord::Migration[4.2]
35
35
 
36
36
  def add_string_limit_if_missing(table, column)
37
37
  return if column_exists?(table, column, :string, limit: 255)
38
+
38
39
  change_column table, column, :string, limit: 255
39
40
  end
40
41
  end
@@ -1,16 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class SpeedUpMaxConcurrentTriggers < ActiveRecord::Migration[4.2]
3
+ class SpeedUpMaxConcurrentTriggers < ActiveRecord::Migration[4.2]
4
4
  def connection
5
5
  Delayed::Job.connection
6
6
  end
7
7
 
8
8
  def up
9
- if connection.adapter_name == 'PostgreSQL'
10
- # tl;dr sacrifice some responsiveness to max_concurrent changes for faster performance
11
- # don't get the count every single time - it's usually safe to just set the next one in line
12
- # since the max_concurrent doesn't change all that often for a strand
13
- execute(<<-CODE)
9
+ # tl;dr sacrifice some responsiveness to max_concurrent changes for faster performance
10
+ # don't get the count every single time - it's usually safe to just set the next one in line
11
+ # since the max_concurrent doesn't change all that often for a strand
12
+ execute(<<~SQL)
14
13
  CREATE OR REPLACE FUNCTION delayed_jobs_after_delete_row_tr_fn () RETURNS trigger AS $$
15
14
  DECLARE
16
15
  running_count integer;
@@ -36,10 +35,10 @@ class SpeedUpMaxConcurrentTriggers < ActiveRecord::Migration[4.2]
36
35
  RETURN OLD;
37
36
  END;
38
37
  $$ LANGUAGE plpgsql;
39
- CODE
38
+ SQL
40
39
 
41
- # don't need the full count on insert
42
- execute(<<-CODE)
40
+ # don't need the full count on insert
41
+ execute(<<~SQL)
43
42
  CREATE OR REPLACE FUNCTION delayed_jobs_before_insert_row_tr_fn () RETURNS trigger AS $$
44
43
  BEGIN
45
44
  IF NEW.strand IS NOT NULL THEN
@@ -53,13 +52,11 @@ class SpeedUpMaxConcurrentTriggers < ActiveRecord::Migration[4.2]
53
52
  RETURN NEW;
54
53
  END;
55
54
  $$ LANGUAGE plpgsql;
56
- CODE
57
- end
55
+ SQL
58
56
  end
59
57
 
60
58
  def down
61
- if connection.adapter_name == 'PostgreSQL'
62
- execute(<<-CODE)
59
+ execute(<<~SQL)
63
60
  CREATE OR REPLACE FUNCTION delayed_jobs_after_delete_row_tr_fn () RETURNS trigger AS $$
64
61
  DECLARE
65
62
  running_count integer;
@@ -77,9 +74,9 @@ class SpeedUpMaxConcurrentTriggers < ActiveRecord::Migration[4.2]
77
74
  RETURN OLD;
78
75
  END;
79
76
  $$ LANGUAGE plpgsql;
80
- CODE
77
+ SQL
81
78
 
82
- execute(<<-CODE)
79
+ execute(<<~SQL)
83
80
  CREATE OR REPLACE FUNCTION delayed_jobs_before_insert_row_tr_fn () RETURNS trigger AS $$
84
81
  BEGIN
85
82
  IF NEW.strand IS NOT NULL THEN
@@ -91,7 +88,6 @@ class SpeedUpMaxConcurrentTriggers < ActiveRecord::Migration[4.2]
91
88
  RETURN NEW;
92
89
  END;
93
90
  $$ LANGUAGE plpgsql;
94
- CODE
95
- end
91
+ SQL
96
92
  end
97
93
  end
@@ -9,19 +9,19 @@ class AddIdToGetDelayedJobsIndex < ActiveRecord::Migration[4.2]
9
9
 
10
10
  def up
11
11
  rename_index :delayed_jobs, "get_delayed_jobs_index", "get_delayed_jobs_index_old"
12
- add_index :delayed_jobs, [:queue, :priority, :run_at, :id],
13
- algorithm: :concurrently,
14
- where: "locked_at IS NULL AND next_in_strand",
15
- name: "get_delayed_jobs_index"
12
+ add_index :delayed_jobs, %i[queue priority run_at id],
13
+ algorithm: :concurrently,
14
+ where: "locked_at IS NULL AND next_in_strand",
15
+ name: "get_delayed_jobs_index"
16
16
  remove_index :delayed_jobs, name: "get_delayed_jobs_index_old"
17
17
  end
18
18
 
19
19
  def down
20
20
  rename_index :delayed_jobs, "get_delayed_jobs_index", "get_delayed_jobs_index_old"
21
- add_index :delayed_jobs, [:priority, :run_at, :queue],
22
- algorithm: :concurrently,
23
- where: "locked_at IS NULL AND next_in_strand",
24
- name: "get_delayed_jobs_index"
21
+ add_index :delayed_jobs, %i[priority run_at queue],
22
+ algorithm: :concurrently,
23
+ where: "locked_at IS NULL AND next_in_strand",
24
+ name: "get_delayed_jobs_index"
25
25
  remove_index :delayed_jobs, name: "get_delayed_jobs_index_old"
26
26
  end
27
27
  end
@@ -6,92 +6,87 @@ class SpeedUpMaxConcurrentDeleteTrigger < ActiveRecord::Migration[4.2]
6
6
  end
7
7
 
8
8
  def up
9
- if connection.adapter_name == 'PostgreSQL'
10
- # tl;dr sacrifice some responsiveness to max_concurrent changes for faster performance
11
- # don't get the count every single time - it's usually safe to just set the next one in line
12
- # since the max_concurrent doesn't change all that often for a strand
13
- execute(<<-SQL)
14
- CREATE OR REPLACE FUNCTION delayed_jobs_after_delete_row_tr_fn () RETURNS trigger AS $$
15
- DECLARE
16
- running_count integer;
17
- should_lock boolean;
18
- should_be_precise boolean;
19
- BEGIN
20
- IF OLD.strand IS NOT NULL THEN
21
- should_lock := true;
22
- should_be_precise := OLD.id % (OLD.max_concurrent * 4) = 0;
23
-
24
- IF NOT should_be_precise AND OLD.max_concurrent > 16 THEN
25
- running_count := (SELECT COUNT(*) FROM (
26
- SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
27
- ) subquery_for_count);
28
- should_lock := running_count < OLD.max_concurrent;
29
- END IF;
30
-
31
- IF should_lock THEN
32
- PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
9
+ # tl;dr sacrifice some responsiveness to max_concurrent changes for faster performance
10
+ # don't get the count every single time - it's usually safe to just set the next one in line
11
+ # since the max_concurrent doesn't change all that often for a strand
12
+ execute(<<-SQL)
13
+ CREATE OR REPLACE FUNCTION delayed_jobs_after_delete_row_tr_fn () RETURNS trigger AS $$
14
+ DECLARE
15
+ running_count integer;
16
+ should_lock boolean;
17
+ should_be_precise boolean;
18
+ BEGIN
19
+ IF OLD.strand IS NOT NULL THEN
20
+ should_lock := true;
21
+ should_be_precise := OLD.id % (OLD.max_concurrent * 4) = 0;
22
+
23
+ IF NOT should_be_precise AND OLD.max_concurrent > 16 THEN
24
+ running_count := (SELECT COUNT(*) FROM (
25
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
26
+ ) subquery_for_count);
27
+ should_lock := running_count < OLD.max_concurrent;
28
+ END IF;
29
+
30
+ IF should_lock THEN
31
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
32
+ END IF;
33
+
34
+ IF should_be_precise THEN
35
+ running_count := (SELECT COUNT(*) FROM (
36
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
37
+ ) subquery_for_count);
38
+ IF running_count < OLD.max_concurrent THEN
39
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
40
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
41
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
42
+ );
33
43
  END IF;
34
-
35
- IF should_be_precise THEN
36
- running_count := (SELECT COUNT(*) FROM (
37
- SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
38
- ) subquery_for_count);
39
- IF running_count < OLD.max_concurrent THEN
40
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
41
- SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
42
- j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
43
- );
44
- END IF;
44
+ ELSE
45
+ -- n-strands don't require precise ordering; we can make this query more performant
46
+ IF OLD.max_concurrent > 1 THEN
47
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
48
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
49
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE SKIP LOCKED);
45
50
  ELSE
46
- -- n-strands don't require precise ordering; we can make this query more performant
47
- IF OLD.max_concurrent > 1 THEN
48
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
51
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
49
52
  (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
50
- j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE SKIP LOCKED);
51
- ELSE
52
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
53
- (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
54
- j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE);
55
- END IF;
53
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE);
56
54
  END IF;
57
55
  END IF;
58
- RETURN OLD;
59
- END;
60
- $$ LANGUAGE plpgsql;
61
- SQL
62
- end
56
+ END IF;
57
+ RETURN OLD;
58
+ END;
59
+ $$ LANGUAGE plpgsql;
60
+ SQL
63
61
  end
64
62
 
65
63
  def down
66
- if connection.adapter_name == 'PostgreSQL'
67
- execute(<<-SQL)
68
- CREATE OR REPLACE FUNCTION delayed_jobs_after_delete_row_tr_fn () RETURNS trigger AS $$
69
- DECLARE
70
- running_count integer;
71
- BEGIN
72
- IF OLD.strand IS NOT NULL THEN
73
- PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
74
- IF OLD.id % 20 = 0 THEN
75
- running_count := (SELECT COUNT(*) FROM (
76
- SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
77
- ) subquery_for_count);
78
- IF running_count < OLD.max_concurrent THEN
79
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
80
- SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
81
- j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
82
- );
83
- END IF;
84
- ELSE
85
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
86
- (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
87
- j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE);
64
+ execute(<<-SQL)
65
+ CREATE OR REPLACE FUNCTION delayed_jobs_after_delete_row_tr_fn () RETURNS trigger AS $$
66
+ DECLARE
67
+ running_count integer;
68
+ BEGIN
69
+ IF OLD.strand IS NOT NULL THEN
70
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
71
+ IF OLD.id % 20 = 0 THEN
72
+ running_count := (SELECT COUNT(*) FROM (
73
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
74
+ ) subquery_for_count);
75
+ IF running_count < OLD.max_concurrent THEN
76
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
77
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
78
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
79
+ );
88
80
  END IF;
81
+ ELSE
82
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
83
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
84
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE);
89
85
  END IF;
90
- RETURN OLD;
91
- END;
92
- $$ LANGUAGE plpgsql;
93
- SQL
94
- end
86
+ END IF;
87
+ RETURN OLD;
88
+ END;
89
+ $$ LANGUAGE plpgsql;
90
+ SQL
95
91
  end
96
92
  end
97
-
@@ -10,119 +10,115 @@ class AddStrandOrderOverride < ActiveRecord::Migration[4.2]
10
10
  def up
11
11
  add_column :delayed_jobs, :strand_order_override, :integer, default: 0, null: false
12
12
  add_column :failed_jobs, :strand_order_override, :integer, default: 0, null: false
13
- add_index :delayed_jobs, [:strand, :strand_order_override, :id],
14
- algorithm: :concurrently,
15
- where: "strand IS NOT NULL",
16
- name: "next_in_strand_index"
13
+ add_index :delayed_jobs, %i[strand strand_order_override id],
14
+ algorithm: :concurrently,
15
+ where: "strand IS NOT NULL",
16
+ name: "next_in_strand_index"
17
17
 
18
- if connection.adapter_name == 'PostgreSQL'
19
- # Use the strand_order_override as the primary sorting mechanism (useful when moving between jobs queues without preserving ID ordering)
20
- execute(<<-SQL)
21
- CREATE OR REPLACE FUNCTION delayed_jobs_after_delete_row_tr_fn () RETURNS trigger AS $$
22
- DECLARE
23
- running_count integer;
24
- should_lock boolean;
25
- should_be_precise boolean;
26
- BEGIN
27
- IF OLD.strand IS NOT NULL THEN
28
- should_lock := true;
29
- should_be_precise := OLD.id % (OLD.max_concurrent * 4) = 0;
30
-
31
- IF NOT should_be_precise AND OLD.max_concurrent > 16 THEN
32
- running_count := (SELECT COUNT(*) FROM (
33
- SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
34
- ) subquery_for_count);
35
- should_lock := running_count < OLD.max_concurrent;
36
- END IF;
37
-
38
- IF should_lock THEN
39
- PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
18
+ # Use the strand_order_override as the primary sorting mechanism (useful when moving between jobs queues without preserving ID ordering)
19
+ execute(<<-SQL)
20
+ CREATE OR REPLACE FUNCTION delayed_jobs_after_delete_row_tr_fn () RETURNS trigger AS $$
21
+ DECLARE
22
+ running_count integer;
23
+ should_lock boolean;
24
+ should_be_precise boolean;
25
+ BEGIN
26
+ IF OLD.strand IS NOT NULL THEN
27
+ should_lock := true;
28
+ should_be_precise := OLD.id % (OLD.max_concurrent * 4) = 0;
29
+
30
+ IF NOT should_be_precise AND OLD.max_concurrent > 16 THEN
31
+ running_count := (SELECT COUNT(*) FROM (
32
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
33
+ ) subquery_for_count);
34
+ should_lock := running_count < OLD.max_concurrent;
35
+ END IF;
36
+
37
+ IF should_lock THEN
38
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
39
+ END IF;
40
+
41
+ IF should_be_precise THEN
42
+ running_count := (SELECT COUNT(*) FROM (
43
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
44
+ ) subquery_for_count);
45
+ IF running_count < OLD.max_concurrent THEN
46
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
47
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
48
+ j2.strand = OLD.strand ORDER BY j2.strand_order_override ASC, j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
49
+ );
40
50
  END IF;
41
-
42
- IF should_be_precise THEN
43
- running_count := (SELECT COUNT(*) FROM (
44
- SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
45
- ) subquery_for_count);
46
- IF running_count < OLD.max_concurrent THEN
47
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
48
- SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
49
- j2.strand = OLD.strand ORDER BY j2.strand_order_override ASC, j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
50
- );
51
- END IF;
51
+ ELSE
52
+ -- n-strands don't require precise ordering; we can make this query more performant
53
+ IF OLD.max_concurrent > 1 THEN
54
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
55
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
56
+ j2.strand = OLD.strand ORDER BY j2.strand_order_override ASC, j2.id ASC LIMIT 1 FOR UPDATE SKIP LOCKED);
52
57
  ELSE
53
- -- n-strands don't require precise ordering; we can make this query more performant
54
- IF OLD.max_concurrent > 1 THEN
55
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
58
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
56
59
  (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
57
- j2.strand = OLD.strand ORDER BY j2.strand_order_override ASC, j2.id ASC LIMIT 1 FOR UPDATE SKIP LOCKED);
58
- ELSE
59
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
60
- (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
61
- j2.strand = OLD.strand ORDER BY j2.strand_order_override ASC, j2.id ASC LIMIT 1 FOR UPDATE);
62
- END IF;
60
+ j2.strand = OLD.strand ORDER BY j2.strand_order_override ASC, j2.id ASC LIMIT 1 FOR UPDATE);
63
61
  END IF;
64
62
  END IF;
65
- RETURN OLD;
66
- END;
67
- $$ LANGUAGE plpgsql;
68
- SQL
69
- end
63
+ END IF;
64
+ RETURN OLD;
65
+ END;
66
+ $$ LANGUAGE plpgsql;
67
+ SQL
70
68
  end
71
69
 
72
70
  def down
73
71
  remove_column :delayed_jobs, :strand_order_override, :integer
74
72
  remove_column :failed_jobs, :strand_order_override, :integer
75
73
 
76
- if connection.adapter_name == 'PostgreSQL'
77
- execute(<<-SQL)
78
- CREATE OR REPLACE FUNCTION delayed_jobs_after_delete_row_tr_fn () RETURNS trigger AS $$
79
- DECLARE
80
- running_count integer;
81
- should_lock boolean;
82
- should_be_precise boolean;
83
- BEGIN
84
- IF OLD.strand IS NOT NULL THEN
85
- should_lock := true;
86
- should_be_precise := OLD.id % (OLD.max_concurrent * 4) = 0;
87
-
88
- IF NOT should_be_precise AND OLD.max_concurrent > 16 THEN
89
- running_count := (SELECT COUNT(*) FROM (
90
- SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
91
- ) subquery_for_count);
92
- should_lock := running_count < OLD.max_concurrent;
93
- END IF;
94
-
95
- IF should_lock THEN
96
- PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
74
+ execute(<<-SQL)
75
+ CREATE OR REPLACE FUNCTION delayed_jobs_after_delete_row_tr_fn () RETURNS trigger AS $$
76
+ DECLARE
77
+ running_count integer;
78
+ should_lock boolean;
79
+ should_be_precise boolean;
80
+ BEGIN
81
+ IF OLD.strand IS NOT NULL THEN
82
+ should_lock := true;
83
+ should_be_precise := OLD.id % (OLD.max_concurrent * 4) = 0;
84
+
85
+ IF NOT should_be_precise AND OLD.max_concurrent > 16 THEN
86
+ running_count := (SELECT COUNT(*) FROM (
87
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
88
+ ) subquery_for_count);
89
+ should_lock := running_count < OLD.max_concurrent;
90
+ END IF;
91
+
92
+ IF should_lock THEN
93
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
94
+ END IF;
95
+
96
+ IF should_be_precise THEN
97
+ running_count := (SELECT COUNT(*) FROM (
98
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
99
+ ) subquery_for_count);
100
+ IF running_count < OLD.max_concurrent THEN
101
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
102
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
103
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
104
+ );
97
105
  END IF;
98
-
99
- IF should_be_precise THEN
100
- running_count := (SELECT COUNT(*) FROM (
101
- SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
102
- ) subquery_for_count);
103
- IF running_count < OLD.max_concurrent THEN
104
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
105
- SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
106
- j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
107
- );
108
- END IF;
106
+ ELSE
107
+ -- n-strands don't require precise ordering; we can make this query more performant
108
+ IF OLD.max_concurrent > 1 THEN
109
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
110
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
111
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE SKIP LOCKED);
109
112
  ELSE
110
- -- n-strands don't require precise ordering; we can make this query more performant
111
- IF OLD.max_concurrent > 1 THEN
112
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
113
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
113
114
  (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
114
- j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE SKIP LOCKED);
115
- ELSE
116
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
117
- (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
118
- j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE);
119
- END IF;
115
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE);
120
116
  END IF;
121
117
  END IF;
122
- RETURN OLD;
123
- END;
124
- $$ LANGUAGE plpgsql;
125
- SQL
126
- end
118
+ END IF;
119
+ RETURN OLD;
120
+ END;
121
+ $$ LANGUAGE plpgsql;
122
+ SQL
127
123
  end
128
124
  end