switchman-inst-jobs 4.0.12 → 4.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/db/migrate/20101216224513_create_delayed_jobs.rb +5 -3
  3. data/db/migrate/20110208031356_add_delayed_jobs_tag.rb +2 -0
  4. data/db/migrate/20110426161613_add_delayed_jobs_max_attempts.rb +2 -0
  5. data/db/migrate/20110516225834_add_delayed_jobs_strand.rb +2 -0
  6. data/db/migrate/20110531144916_cleanup_delayed_jobs_indexes.rb +9 -7
  7. data/db/migrate/20110610213249_optimize_delayed_jobs.rb +20 -18
  8. data/db/migrate/20110831210257_add_delayed_jobs_next_in_strand.rb +28 -26
  9. data/db/migrate/20120510004759_delayed_jobs_delete_trigger_lock_for_update.rb +20 -18
  10. data/db/migrate/20120531150712_drop_psql_jobs_pop_fn.rb +4 -2
  11. data/db/migrate/20120607164022_delayed_jobs_use_advisory_locks.rb +61 -59
  12. data/db/migrate/20120607181141_index_jobs_on_locked_by.rb +3 -1
  13. data/db/migrate/20120608191051_add_jobs_run_at_index.rb +3 -1
  14. data/db/migrate/20120927184213_change_delayed_jobs_handler_to_text.rb +2 -0
  15. data/db/migrate/20140505215131_add_failed_jobs_original_job_id.rb +2 -0
  16. data/db/migrate/20140505215510_copy_failed_jobs_original_id.rb +2 -0
  17. data/db/migrate/20140505223637_drop_failed_jobs_original_id.rb +2 -0
  18. data/db/migrate/20140512213941_add_source_to_jobs.rb +2 -0
  19. data/db/migrate/20150807133223_add_max_concurrent_to_jobs.rb +51 -49
  20. data/db/migrate/20151123210429_add_expires_at_to_jobs.rb +2 -0
  21. data/db/migrate/20151210162949_improve_max_concurrent.rb +37 -35
  22. data/db/migrate/20161206323555_add_back_default_string_limits_jobs.rb +4 -2
  23. data/db/migrate/20170308045400_add_shard_id_to_delayed_jobs.rb +2 -0
  24. data/db/migrate/20170308045401_add_delayed_jobs_shard_id_to_switchman_shards.rb +2 -0
  25. data/db/migrate/20181217155351_speed_up_max_concurrent_triggers.rb +73 -71
  26. data/db/migrate/20190726154743_make_critical_columns_not_null.rb +2 -0
  27. data/db/migrate/20200330230722_add_id_to_get_delayed_jobs_index.rb +14 -10
  28. data/db/migrate/20200818130101_add_on_hold_to_switchman_shards.rb +2 -0
  29. data/db/migrate/20200822014259_add_block_stranded_to_switchman_shards.rb +2 -0
  30. data/db/migrate/20200824222232_speed_up_max_concurrent_delete_trigger.rb +6 -4
  31. data/db/migrate/20200825011002_add_strand_order_override.rb +10 -7
  32. data/db/migrate/20210809145804_add_n_strand_index.rb +4 -3
  33. data/db/migrate/20210812210128_add_singleton_column.rb +12 -12
  34. data/db/migrate/20210917232626_add_delete_conflicting_singletons_before_unlock_trigger.rb +3 -3
  35. data/db/migrate/20210928174754_fix_singleton_condition_in_before_insert.rb +2 -2
  36. data/db/migrate/20210929204903_update_conflicting_singleton_function_to_use_index.rb +2 -2
  37. data/db/migrate/20211101190934_update_after_delete_trigger_for_singleton_index.rb +2 -2
  38. data/db/migrate/20211207094200_update_after_delete_trigger_for_singleton_transition_cases.rb +2 -2
  39. data/db/migrate/20211220112800_fix_singleton_race_condition_insert.rb +2 -2
  40. data/db/migrate/20211220113000_fix_singleton_race_condition_delete.rb +2 -2
  41. data/db/migrate/20220127091200_fix_singleton_unique_constraint.rb +6 -6
  42. data/db/migrate/20220128084800_update_insert_trigger_for_singleton_unique_constraint_change.rb +2 -2
  43. data/db/migrate/20220128084900_update_delete_trigger_for_singleton_unique_constraint_change.rb +2 -2
  44. data/db/migrate/20220203063200_remove_old_singleton_index.rb +8 -8
  45. data/db/migrate/20220328152900_add_failed_jobs_indicies.rb +2 -2
  46. data/lib/switchman-inst-jobs.rb +3 -1
  47. data/lib/switchman_inst_jobs/active_record/connection_adapters/postgresql_adapter.rb +3 -1
  48. data/lib/switchman_inst_jobs/active_record/migration.rb +5 -3
  49. data/lib/switchman_inst_jobs/delayed/backend/active_record/abstract_job.rb +2 -0
  50. data/lib/switchman_inst_jobs/delayed/backend/base.rb +10 -2
  51. data/lib/switchman_inst_jobs/delayed/message_sending.rb +2 -0
  52. data/lib/switchman_inst_jobs/delayed/pool.rb +2 -0
  53. data/lib/switchman_inst_jobs/delayed/settings.rb +3 -1
  54. data/lib/switchman_inst_jobs/delayed/worker/health_check.rb +5 -3
  55. data/lib/switchman_inst_jobs/delayed/worker.rb +2 -0
  56. data/lib/switchman_inst_jobs/engine.rb +7 -5
  57. data/lib/switchman_inst_jobs/guard_rail.rb +2 -0
  58. data/lib/switchman_inst_jobs/jobs_migrator.rb +62 -55
  59. data/lib/switchman_inst_jobs/new_relic.rb +2 -0
  60. data/lib/switchman_inst_jobs/switchman/database_server.rb +3 -1
  61. data/lib/switchman_inst_jobs/switchman/default_shard.rb +2 -0
  62. data/lib/switchman_inst_jobs/switchman/shard.rb +27 -23
  63. data/lib/switchman_inst_jobs/timed_cache.rb +2 -0
  64. data/lib/switchman_inst_jobs/version.rb +3 -1
  65. data/lib/switchman_inst_jobs/yaml_extensions.rb +3 -1
  66. data/lib/switchman_inst_jobs.rb +22 -20
  67. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4081fb390625cd34169a6585ffaddb12143403e3545988a1ada3ca90d34dc2d
4
- data.tar.gz: 2f60cfa5de1529a206115bd2d68758f41f71763afbdaa980095de67dda1da544
3
+ metadata.gz: 0f1621254a45cdb7658e081093f093e8e494040d9c504d973c7e61418f2101b1
4
+ data.tar.gz: aef5ccde279a3f6e08ed178e9d6b7debd44657fbd813d51cff32e625783e9b27
5
5
  SHA512:
6
- metadata.gz: 86bb9901b00dd38410092f17e15e1ff9b70b4907426ad32b7f2779a65844d7442e01e8df056d2293c90354eb814e880068854713e9169099e7d772294fc0a1d9
7
- data.tar.gz: 9376187ebecfaab79e07696fd44ed762694e5ed06e9b539183f93f0f49e9706fa9eef59d6e9ef5456bccae2be780360d021da600e854eb8f2afcace71ca25bb5
6
+ metadata.gz: 33ed1c97b1730e8c7b6fc274c806ad045bdfd0602945c1273f567e22d73df8c50a0a908345bd069bec895f9c73ffe4df3d42622e2afa12bc5858ee4133599457
7
+ data.tar.gz: 19923c2ad0d827db1beb7c6a700282794e5208347922d4ebbe1191cd196dbb18a4db2e4e39f4b88b577609093858a55f37a9c0858410d404e07223cacfeae819
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CreateDelayedJobs < ActiveRecord::Migration[4.2]
2
4
  def up
3
- unless connection.adapter_name == 'PostgreSQL'
5
+ unless connection.adapter_name == "PostgreSQL"
4
6
  raise("#{connection.adapter_name} is not supported for delayed jobs queue")
5
7
  end
6
8
 
@@ -28,8 +30,8 @@ class CreateDelayedJobs < ActiveRecord::Migration[4.2]
28
30
  table.timestamps
29
31
  end
30
32
 
31
- add_index :delayed_jobs, %i[priority run_at], name: 'delayed_jobs_priority'
32
- add_index :delayed_jobs, [:queue], name: 'delayed_jobs_queue'
33
+ add_index :delayed_jobs, %i[priority run_at], name: "delayed_jobs_priority"
34
+ add_index :delayed_jobs, [:queue], name: "delayed_jobs_queue"
33
35
  end
34
36
 
35
37
  def down
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddDelayedJobsTag < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  add_column :delayed_jobs, :tag, :string
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddDelayedJobsMaxAttempts < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  add_column :delayed_jobs, :max_attempts, :integer
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddDelayedJobsStrand < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  add_column :delayed_jobs, :strand, :string
@@ -1,22 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CleanupDelayedJobsIndexes < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  case connection.adapter_name
4
- when 'PostgreSQL'
6
+ when "PostgreSQL"
5
7
  # "nulls first" syntax is postgresql specific, and allows for more
6
8
  # efficient querying for the next job
7
9
  connection.execute("CREATE INDEX get_delayed_jobs_index ON #{connection.quote_table_name(::Delayed::Job.table_name)} (priority, run_at, failed_at nulls first, locked_at nulls first, queue)")
8
10
  else
9
- add_index :delayed_jobs, %w[priority run_at locked_at failed_at queue], name: 'get_delayed_jobs_index'
11
+ add_index :delayed_jobs, %w[priority run_at locked_at failed_at queue], name: "get_delayed_jobs_index"
10
12
  end
11
13
 
12
14
  # unused indexes
13
- remove_index :delayed_jobs, name: 'delayed_jobs_queue'
14
- remove_index :delayed_jobs, name: 'delayed_jobs_priority'
15
+ remove_index :delayed_jobs, name: "delayed_jobs_queue"
16
+ remove_index :delayed_jobs, name: "delayed_jobs_priority"
15
17
  end
16
18
 
17
19
  def down
18
- remove_index :delayed_jobs, name: 'get_delayed_jobs_index'
19
- add_index :delayed_jobs, %i[priority run_at], name: 'delayed_jobs_priority'
20
- add_index :delayed_jobs, [:queue], name: 'delayed_jobs_queue'
20
+ remove_index :delayed_jobs, name: "get_delayed_jobs_index"
21
+ add_index :delayed_jobs, %i[priority run_at], name: "delayed_jobs_priority"
22
+ add_index :delayed_jobs, [:queue], name: "delayed_jobs_queue"
21
23
  end
22
24
  end
@@ -1,28 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class OptimizeDelayedJobs < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  create_table :failed_jobs do |t|
4
- t.integer 'priority', default: 0
5
- t.integer 'attempts', default: 0
6
- t.string 'handler', limit: 512_000
7
- t.integer 'original_id', limit: 8
8
- t.text 'last_error'
9
- t.string 'queue'
10
- t.datetime 'run_at'
11
- t.datetime 'locked_at'
12
- t.datetime 'failed_at'
13
- t.string 'locked_by'
14
- t.datetime 'created_at'
15
- t.datetime 'updated_at'
16
- t.string 'tag'
17
- t.integer 'max_attempts'
18
- t.string 'strand'
6
+ t.integer "priority", default: 0
7
+ t.integer "attempts", default: 0
8
+ t.string "handler", limit: 512_000
9
+ t.integer "original_id", limit: 8
10
+ t.text "last_error"
11
+ t.string "queue"
12
+ t.datetime "run_at"
13
+ t.datetime "locked_at"
14
+ t.datetime "failed_at"
15
+ t.string "locked_by"
16
+ t.datetime "created_at"
17
+ t.datetime "updated_at"
18
+ t.string "tag"
19
+ t.integer "max_attempts"
20
+ t.string "strand"
19
21
  end
20
22
 
21
- remove_index :delayed_jobs, name: 'get_delayed_jobs_index'
23
+ remove_index :delayed_jobs, name: "get_delayed_jobs_index"
22
24
  remove_index :delayed_jobs, [:strand]
23
25
 
24
- add_index :delayed_jobs, %w[run_at queue locked_at strand priority], name: 'index_delayed_jobs_for_get_next'
25
- add_index :delayed_jobs, %w[strand id], name: 'index_delayed_jobs_on_strand'
26
+ add_index :delayed_jobs, %w[run_at queue locked_at strand priority], name: "index_delayed_jobs_for_get_next"
27
+ add_index :delayed_jobs, %w[strand id], name: "index_delayed_jobs_on_strand"
26
28
  end
27
29
 
28
30
  def down
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddDelayedJobsNextInStrand < ActiveRecord::Migration[4.2]
2
4
  def up
3
- remove_index :delayed_jobs, name: 'index_delayed_jobs_for_get_next'
5
+ remove_index :delayed_jobs, name: "index_delayed_jobs_for_get_next"
4
6
 
5
7
  add_column :delayed_jobs, :next_in_strand, :boolean, default: true, null: false
6
8
 
@@ -8,41 +10,41 @@ class AddDelayedJobsNextInStrand < ActiveRecord::Migration[4.2]
8
10
  connection.execute("CREATE INDEX get_delayed_jobs_index ON #{connection.quote_table_name(::Delayed::Job.table_name)} (priority, run_at, queue) WHERE locked_at IS NULL AND next_in_strand = 't'")
9
11
 
10
12
  # create the insert trigger
11
- execute(<<-CODE)
12
- CREATE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
13
- BEGIN
14
- LOCK delayed_jobs IN SHARE ROW EXCLUSIVE MODE;
15
- IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
16
- NEW.next_in_strand := 'f';
17
- END IF;
18
- RETURN NEW;
19
- END;
20
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
21
- CODE
22
- execute("CREATE TRIGGER delayed_jobs_before_insert_row_tr BEFORE INSERT ON #{connection.quote_table_name(::Delayed::Job.table_name)} FOR EACH ROW WHEN (NEW.strand IS NOT NULL) EXECUTE PROCEDURE #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')}()")
13
+ execute(<<~SQL)
14
+ CREATE FUNCTION #{connection.quote_table_name("delayed_jobs_before_insert_row_tr_fn")} () RETURNS trigger AS $$
15
+ BEGIN
16
+ LOCK delayed_jobs IN SHARE ROW EXCLUSIVE MODE;
17
+ IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
18
+ NEW.next_in_strand := 'f';
19
+ END IF;
20
+ RETURN NEW;
21
+ END;
22
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
23
+ SQL
24
+ execute("CREATE TRIGGER delayed_jobs_before_insert_row_tr BEFORE INSERT ON #{connection.quote_table_name(::Delayed::Job.table_name)} FOR EACH ROW WHEN (NEW.strand IS NOT NULL) EXECUTE PROCEDURE #{connection.quote_table_name("delayed_jobs_before_insert_row_tr_fn")}()")
23
25
 
24
26
  # create the delete trigger
25
- execute(<<-CODE)
26
- CREATE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
27
- BEGIN
28
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1);
29
- RETURN OLD;
30
- END;
31
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
32
- CODE
33
- execute("CREATE TRIGGER delayed_jobs_after_delete_row_tr AFTER DELETE ON #{connection.quote_table_name(::Delayed::Job.table_name)} FOR EACH ROW WHEN (OLD.strand IS NOT NULL AND OLD.next_in_strand = 't') EXECUTE PROCEDURE #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} ()")
27
+ execute(<<~SQL)
28
+ CREATE FUNCTION #{connection.quote_table_name("delayed_jobs_after_delete_row_tr_fn")} () RETURNS trigger AS $$
29
+ BEGIN
30
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1);
31
+ RETURN OLD;
32
+ END;
33
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
34
+ SQL
35
+ execute("CREATE TRIGGER delayed_jobs_after_delete_row_tr AFTER DELETE ON #{connection.quote_table_name(::Delayed::Job.table_name)} FOR EACH ROW WHEN (OLD.strand IS NOT NULL AND OLD.next_in_strand = 't') EXECUTE PROCEDURE #{connection.quote_table_name("delayed_jobs_after_delete_row_tr_fn")} ()")
34
36
 
35
37
  execute(%{UPDATE #{connection.quote_table_name(::Delayed::Job.table_name)} SET next_in_strand = 'f' WHERE strand IS NOT NULL AND id <> (SELECT id FROM #{connection.quote_table_name(::Delayed::Job.table_name)} j2 WHERE j2.strand = delayed_jobs.strand ORDER BY j2.strand, j2.id ASC LIMIT 1)})
36
38
  end
37
39
 
38
40
  def down
39
41
  execute %(DROP TRIGGER delayed_jobs_before_insert_row_tr ON #{connection.quote_table_name(::Delayed::Job.table_name)})
40
- execute %{DROP FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} ()}
42
+ execute %{DROP FUNCTION #{connection.quote_table_name("delayed_jobs_before_insert_row_tr_fn")} ()}
41
43
  execute %(DROP TRIGGER delayed_jobs_after_delete_row_tr ON #{connection.quote_table_name(::Delayed::Job.table_name)})
42
- execute %{DROP FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} ()}
44
+ execute %{DROP FUNCTION #{connection.quote_table_name("delayed_jobs_after_delete_row_tr_fn")} ()}
43
45
 
44
46
  remove_column :delayed_jobs, :next_in_strand
45
- remove_index :delayed_jobs, name: 'get_delayed_jobs_index'
46
- add_index :delayed_jobs, %w[run_at queue locked_at strand priority], name: 'index_delayed_jobs_for_get_next'
47
+ remove_index :delayed_jobs, name: "get_delayed_jobs_index"
48
+ add_index :delayed_jobs, %w[run_at queue locked_at strand priority], name: "index_delayed_jobs_for_get_next"
47
49
  end
48
50
  end
@@ -1,27 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class DelayedJobsDeleteTriggerLockForUpdate < ActiveRecord::Migration[4.2]
2
4
  def up
3
- if connection.adapter_name == 'PostgreSQL'
4
- execute(<<-CODE)
5
- CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
6
- BEGIN
7
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1 FOR UPDATE);
8
- RETURN OLD;
9
- END;
10
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
11
- CODE
5
+ if connection.adapter_name == "PostgreSQL"
6
+ execute(<<~SQL)
7
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name("delayed_jobs_after_delete_row_tr_fn")} () RETURNS trigger AS $$
8
+ BEGIN
9
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1 FOR UPDATE);
10
+ RETURN OLD;
11
+ END;
12
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
13
+ SQL
12
14
  end
13
15
  end
14
16
 
15
17
  def down
16
- if connection.adapter_name == 'PostgreSQL'
17
- execute(<<-CODE)
18
- CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
19
- BEGIN
20
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1);
21
- RETURN OLD;
22
- END;
23
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
24
- CODE
18
+ if connection.adapter_name == "PostgreSQL"
19
+ execute(<<~SQL)
20
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name("delayed_jobs_after_delete_row_tr_fn")} () RETURNS trigger AS $$
21
+ BEGIN
22
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1);
23
+ RETURN OLD;
24
+ END;
25
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
26
+ SQL
25
27
  end
26
28
  end
27
29
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class DropPsqlJobsPopFn < ActiveRecord::Migration[4.2]
2
4
  def up
3
- if connection.adapter_name == 'PostgreSQL'
4
- connection.execute('DROP FUNCTION IF EXISTS pop_from_delayed_jobs(varchar, varchar, integer, integer, timestamp without time zone)')
5
+ if connection.adapter_name == "PostgreSQL"
6
+ connection.execute("DROP FUNCTION IF EXISTS pop_from_delayed_jobs(varchar, varchar, integer, integer, timestamp without time zone)")
5
7
  end
6
8
  end
7
9
 
@@ -1,76 +1,78 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class DelayedJobsUseAdvisoryLocks < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  # use an advisory lock based on the name of the strand, instead of locking the whole table
4
6
  # note that we're using half of the md5, so collisions are possible, but we don't really
5
7
  # care because that would just be the old behavior, whereas for the most part locking will
6
8
  # be much smaller
7
- if connection.adapter_name == 'PostgreSQL'
8
- execute(<<-CODE)
9
- CREATE FUNCTION #{connection.quote_table_name('half_md5_as_bigint')}(strand varchar) RETURNS bigint AS $$
10
- DECLARE
11
- strand_md5 bytea;
12
- BEGIN
13
- strand_md5 := decode(md5(strand), 'hex');
14
- RETURN (CAST(get_byte(strand_md5, 0) AS bigint) << 56) +
15
- (CAST(get_byte(strand_md5, 1) AS bigint) << 48) +
16
- (CAST(get_byte(strand_md5, 2) AS bigint) << 40) +
17
- (CAST(get_byte(strand_md5, 3) AS bigint) << 32) +
18
- (CAST(get_byte(strand_md5, 4) AS bigint) << 24) +
19
- (get_byte(strand_md5, 5) << 16) +
20
- (get_byte(strand_md5, 6) << 8) +
21
- get_byte(strand_md5, 7);
22
- END;
23
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
24
- CODE
9
+ if connection.adapter_name == "PostgreSQL"
10
+ execute(<<~SQL)
11
+ CREATE FUNCTION #{connection.quote_table_name("half_md5_as_bigint")}(strand varchar) RETURNS bigint AS $$
12
+ DECLARE
13
+ strand_md5 bytea;
14
+ BEGIN
15
+ strand_md5 := decode(md5(strand), 'hex');
16
+ RETURN (CAST(get_byte(strand_md5, 0) AS bigint) << 56) +
17
+ (CAST(get_byte(strand_md5, 1) AS bigint) << 48) +
18
+ (CAST(get_byte(strand_md5, 2) AS bigint) << 40) +
19
+ (CAST(get_byte(strand_md5, 3) AS bigint) << 32) +
20
+ (CAST(get_byte(strand_md5, 4) AS bigint) << 24) +
21
+ (get_byte(strand_md5, 5) << 16) +
22
+ (get_byte(strand_md5, 6) << 8) +
23
+ get_byte(strand_md5, 7);
24
+ END;
25
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
26
+ SQL
25
27
 
26
- execute(<<-CODE)
27
- CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
28
- BEGIN
29
- PERFORM pg_advisory_xact_lock(half_md5_as_bigint(NEW.strand));
30
- IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
31
- NEW.next_in_strand := 'f';
32
- END IF;
33
- RETURN NEW;
34
- END;
35
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
36
- CODE
28
+ execute(<<~SQL)
29
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name("delayed_jobs_before_insert_row_tr_fn")} () RETURNS trigger AS $$
30
+ BEGIN
31
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(NEW.strand));
32
+ IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
33
+ NEW.next_in_strand := 'f';
34
+ END IF;
35
+ RETURN NEW;
36
+ END;
37
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
38
+ SQL
37
39
 
38
- execute(<<-CODE)
39
- CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
40
- BEGIN
41
- PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
42
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1 FOR UPDATE);
43
- RETURN OLD;
44
- END;
45
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
46
- CODE
40
+ execute(<<~SQL)
41
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name("delayed_jobs_after_delete_row_tr_fn")} () RETURNS trigger AS $$
42
+ BEGIN
43
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
44
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1 FOR UPDATE);
45
+ RETURN OLD;
46
+ END;
47
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
48
+ SQL
47
49
  end
48
50
  end
49
51
 
50
52
  def down
51
- if connection.adapter_name == 'PostgreSQL'
52
- execute(<<-CODE)
53
- CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
54
- BEGIN
55
- LOCK delayed_jobs IN SHARE ROW EXCLUSIVE MODE;
56
- IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
57
- NEW.next_in_strand := 'f';
58
- END IF;
59
- RETURN NEW;
60
- END;
61
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
62
- CODE
53
+ if connection.adapter_name == "PostgreSQL"
54
+ execute(<<~SQL)
55
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name("delayed_jobs_before_insert_row_tr_fn")} () RETURNS trigger AS $$
56
+ BEGIN
57
+ LOCK delayed_jobs IN SHARE ROW EXCLUSIVE MODE;
58
+ IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
59
+ NEW.next_in_strand := 'f';
60
+ END IF;
61
+ RETURN NEW;
62
+ END;
63
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
64
+ SQL
63
65
 
64
- execute(<<-CODE)
65
- CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
66
- BEGIN
67
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1 FOR UPDATE);
68
- RETURN OLD;
69
- END;
70
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
71
- CODE
66
+ execute(<<~SQL)
67
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name("delayed_jobs_after_delete_row_tr_fn")} () RETURNS trigger AS $$
68
+ BEGIN
69
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1 FOR UPDATE);
70
+ RETURN OLD;
71
+ END;
72
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
73
+ SQL
72
74
 
73
- execute("DROP FUNCTION #{connection.quote_table_name('half_md5_as_bigint')}(varchar)")
75
+ execute("DROP FUNCTION #{connection.quote_table_name("half_md5_as_bigint")}(varchar)")
74
76
  end
75
77
  end
76
78
  end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class IndexJobsOnLockedBy < ActiveRecord::Migration[4.2]
2
4
  disable_ddl_transaction!
3
5
 
4
6
  def up
5
- add_index :delayed_jobs, :locked_by, algorithm: :concurrently, where: 'locked_by IS NOT NULL'
7
+ add_index :delayed_jobs, :locked_by, algorithm: :concurrently, where: "locked_by IS NOT NULL"
6
8
  end
7
9
 
8
10
  def down
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddJobsRunAtIndex < ActiveRecord::Migration[4.2]
2
4
  disable_ddl_transaction!
3
5
 
@@ -6,6 +8,6 @@ class AddJobsRunAtIndex < ActiveRecord::Migration[4.2]
6
8
  end
7
9
 
8
10
  def down
9
- remove_index :delayed_jobs, name: 'index_delayed_jobs_on_run_at_and_tag'
11
+ remove_index :delayed_jobs, name: "index_delayed_jobs_on_run_at_and_tag"
10
12
  end
11
13
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ChangeDelayedJobsHandlerToText < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  change_column :delayed_jobs, :handler, :text
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddFailedJobsOriginalJobId < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  add_column :failed_jobs, :original_job_id, :integer, limit: 8
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CopyFailedJobsOriginalId < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  # Noop since we don't want to modify the shard using a different connection than the one we are using to build it and
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class DropFailedJobsOriginalId < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  remove_column :failed_jobs, :original_id
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddSourceToJobs < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  add_column :delayed_jobs, :source, :string
@@ -1,66 +1,68 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddMaxConcurrentToJobs < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  add_column :delayed_jobs, :max_concurrent, :integer, default: 1, null: false
4
6
 
5
- if connection.adapter_name == 'PostgreSQL'
6
- execute(<<-CODE)
7
- CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
8
- BEGIN
9
- IF NEW.strand IS NOT NULL THEN
10
- PERFORM pg_advisory_xact_lock(half_md5_as_bigint(NEW.strand));
11
- IF (SELECT COUNT(*) FROM delayed_jobs WHERE strand = NEW.strand) >= NEW.max_concurrent THEN
12
- NEW.next_in_strand := 'f';
7
+ if connection.adapter_name == "PostgreSQL"
8
+ execute(<<~SQL)
9
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name("delayed_jobs_before_insert_row_tr_fn")} () RETURNS trigger AS $$
10
+ BEGIN
11
+ IF NEW.strand IS NOT NULL THEN
12
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(NEW.strand));
13
+ IF (SELECT COUNT(*) FROM delayed_jobs WHERE strand = NEW.strand) >= NEW.max_concurrent THEN
14
+ NEW.next_in_strand := 'f';
15
+ END IF;
13
16
  END IF;
14
- END IF;
15
- RETURN NEW;
16
- END;
17
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
18
- CODE
17
+ RETURN NEW;
18
+ END;
19
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
20
+ SQL
19
21
 
20
- execute(<<-CODE)
21
- CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
22
- BEGIN
23
- IF OLD.strand IS NOT NULL THEN
24
- PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
25
- IF (SELECT COUNT(*) FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't') < OLD.max_concurrent THEN
26
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (
27
- SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
28
- j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE
29
- );
22
+ execute(<<~SQL)
23
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name("delayed_jobs_after_delete_row_tr_fn")} () RETURNS trigger AS $$
24
+ BEGIN
25
+ IF OLD.strand IS NOT NULL THEN
26
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
27
+ IF (SELECT COUNT(*) FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't') < OLD.max_concurrent THEN
28
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (
29
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
30
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE
31
+ );
32
+ END IF;
30
33
  END IF;
31
- END IF;
32
- RETURN OLD;
33
- END;
34
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
35
- CODE
34
+ RETURN OLD;
35
+ END;
36
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
37
+ SQL
36
38
  end
37
39
  end
38
40
 
39
41
  def down
40
42
  remove_column :delayed_jobs, :max_concurrent
41
43
 
42
- if connection.adapter_name == 'PostgreSQL'
43
- execute(<<-CODE)
44
- CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
45
- BEGIN
46
- PERFORM pg_advisory_xact_lock(half_md5_as_bigint(NEW.strand));
47
- IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
48
- NEW.next_in_strand := 'f';
49
- END IF;
50
- RETURN NEW;
51
- END;
52
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
53
- CODE
44
+ if connection.adapter_name == "PostgreSQL"
45
+ execute(<<~SQL)
46
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name("delayed_jobs_before_insert_row_tr_fn")} () RETURNS trigger AS $$
47
+ BEGIN
48
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(NEW.strand));
49
+ IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
50
+ NEW.next_in_strand := 'f';
51
+ END IF;
52
+ RETURN NEW;
53
+ END;
54
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
55
+ SQL
54
56
 
55
- execute(<<-CODE)
56
- CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
57
- BEGIN
58
- PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
59
- UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1 FOR UPDATE);
60
- RETURN OLD;
61
- END;
62
- $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
63
- CODE
57
+ execute(<<~SQL)
58
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name("delayed_jobs_after_delete_row_tr_fn")} () RETURNS trigger AS $$
59
+ BEGIN
60
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
61
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (SELECT id FROM delayed_jobs j2 WHERE j2.strand = OLD.strand ORDER BY j2.strand, j2.id ASC LIMIT 1 FOR UPDATE);
62
+ RETURN OLD;
63
+ END;
64
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
65
+ SQL
64
66
  end
65
67
  end
66
68
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddExpiresAtToJobs < ActiveRecord::Migration[4.2]
2
4
  def up
3
5
  add_column :delayed_jobs, :expires_at, :datetime