switchman-inst-jobs 1.5.2 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/db/migrate/20101216224513_create_delayed_jobs.rb +42 -0
  3. data/db/migrate/20110208031356_add_delayed_jobs_tag.rb +14 -0
  4. data/db/migrate/20110426161613_add_delayed_jobs_max_attempts.rb +13 -0
  5. data/db/migrate/20110516225834_add_delayed_jobs_strand.rb +14 -0
  6. data/db/migrate/20110531144916_cleanup_delayed_jobs_indexes.rb +26 -0
  7. data/db/migrate/20110610213249_optimize_delayed_jobs.rb +40 -0
  8. data/db/migrate/20110831210257_add_delayed_jobs_next_in_strand.rb +52 -0
  9. data/db/migrate/20120510004759_delayed_jobs_delete_trigger_lock_for_update.rb +31 -0
  10. data/db/migrate/20120531150712_drop_psql_jobs_pop_fn.rb +15 -0
  11. data/db/migrate/20120607164022_delayed_jobs_use_advisory_locks.rb +80 -0
  12. data/db/migrate/20120607181141_index_jobs_on_locked_by.rb +15 -0
  13. data/db/migrate/20120608191051_add_jobs_run_at_index.rb +15 -0
  14. data/db/migrate/20120927184213_change_delayed_jobs_handler_to_text.rb +13 -0
  15. data/db/migrate/20140505215131_add_failed_jobs_original_job_id.rb +13 -0
  16. data/db/migrate/20140505215510_copy_failed_jobs_original_id.rb +12 -0
  17. data/db/migrate/20140505223637_drop_failed_jobs_original_id.rb +13 -0
  18. data/db/migrate/20140512213941_add_source_to_jobs.rb +15 -0
  19. data/db/migrate/20150807133223_add_max_concurrent_to_jobs.rb +70 -0
  20. data/db/migrate/20151123210429_add_expires_at_to_jobs.rb +15 -0
  21. data/db/migrate/20151210162949_improve_max_concurrent.rb +50 -0
  22. data/db/migrate/20161206323555_add_back_default_string_limits_jobs.rb +39 -0
  23. data/db/migrate/20181217155351_speed_up_max_concurrent_triggers.rb +95 -0
  24. data/db/migrate/20190726154743_make_critical_columns_not_null.rb +15 -0
  25. data/db/migrate/20200330230722_add_id_to_get_delayed_jobs_index.rb +25 -0
  26. data/db/migrate/20200824222232_speed_up_max_concurrent_delete_trigger.rb +94 -0
  27. data/db/migrate/20200825011002_add_strand_order_override.rb +126 -0
  28. data/lib/switchman_inst_jobs/active_record/migration.rb +10 -0
  29. data/lib/switchman_inst_jobs/jobs_migrator.rb +8 -6
  30. data/lib/switchman_inst_jobs/version.rb +1 -1
  31. metadata +36 -11
  32. data/db/migrate/20180628153808_set_search_paths_on_functions.rb +0 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99fc38ce3e38711c3ebb2d35f6e6eccb62f5d9388e6fbec9b9ba40830b3b4df7
4
- data.tar.gz: 12501153af5a11a2c4d958cbe60d2185d684d92794081b1bbffe6f3394a6b257
3
+ metadata.gz: 8e4c1c0421945fc0c823b2b1e452801565ac1c3e5ad9aae77d52fd8a36d83d0d
4
+ data.tar.gz: 87d5a191c8dd7c1918d1bae1d8b05ffdfee1146e462e9adab0869f2b2210f681
5
5
  SHA512:
6
- metadata.gz: 236773fe9bc45914fc1e78fe45ba8e16793259a93c2e58fe3a6603d04f03d4a748c8fd51171bbcb1440dd39e56a5b016bf9864668f540b8322f2b7f354ae68fa
7
- data.tar.gz: 42d222e8d2660833010b731295cf76984e5e8cff9097c911260ab4ea0491771088685f73c0a9e05d335a38af79efdf34ad71296f7017297cdd9d382f8163f2e9
6
+ metadata.gz: a6c0abc940a6f4a7dc73923e9e020b853f23d29551bb19efa67338ae1e0da0af9065cde8f6a9f60e212f9514dd95c73b242512f1e21e53147601ff7f8cde30c1
7
+ data.tar.gz: 4caad338666dcfbb83967c007a28443a45f86aa813b8fa4f8d81f6354beb9e9f3b68ae7b4118afa5d40c6e2da77e34f7982c8e28a6c1de2e9cdcc99942acd2c4
@@ -0,0 +1,42 @@
1
+ class CreateDelayedJobs < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ unless connection.adapter_name == 'PostgreSQL'
8
+ raise("#{connection.adapter_name} is not supported for delayed jobs queue")
9
+ end
10
+
11
+ create_table :delayed_jobs do |table|
12
+ # Allows some jobs to jump to the front of the queue
13
+ table.integer :priority, default: 0
14
+ # Provides for retries, but still fail eventually.
15
+ table.integer :attempts, default: 0
16
+ # YAML-encoded string of the object that will do work
17
+ table.text :handler, limit: (500 * 1024)
18
+ # reason for last failure (See Note below)
19
+ table.text :last_error
20
+ # The queue that this job is in
21
+ table.string :queue, default: nil
22
+ # When to run.
23
+ # Could be Time.zone.now for immediately, or sometime in the future.
24
+ table.datetime :run_at
25
+ # Set when a client is working on this object
26
+ table.datetime :locked_at
27
+ # Set when all retries have failed
28
+ table.datetime :failed_at
29
+ # Who is working on this object (if locked)
30
+ table.string :locked_by
31
+
32
+ table.timestamps
33
+ end
34
+
35
+ add_index :delayed_jobs, %i[priority run_at], name: 'delayed_jobs_priority'
36
+ add_index :delayed_jobs, [:queue], name: 'delayed_jobs_queue'
37
+ end
38
+
39
+ def down
40
+ drop_table :delayed_jobs
41
+ end
42
+ end
@@ -0,0 +1,14 @@
1
+ class AddDelayedJobsTag < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ add_column :delayed_jobs, :tag, :string
8
+ add_index :delayed_jobs, [:tag]
9
+ end
10
+
11
+ def down
12
+ remove_column :delayed_jobs, :tag
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ class AddDelayedJobsMaxAttempts < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ add_column :delayed_jobs, :max_attempts, :integer
8
+ end
9
+
10
+ def down
11
+ remove_column :delayed_jobs, :max_attempts
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ class AddDelayedJobsStrand < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ add_column :delayed_jobs, :strand, :string
8
+ add_index :delayed_jobs, :strand
9
+ end
10
+
11
+ def down
12
+ remove_column :delayed_jobs, :strand
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ class CleanupDelayedJobsIndexes < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ case connection.adapter_name
8
+ when 'PostgreSQL'
9
+ # "nulls first" syntax is postgresql specific, and allows for more
10
+ # efficient querying for the next job
11
+ connection.execute("CREATE INDEX get_delayed_jobs_index ON #{::Delayed::Job.quoted_table_name} (priority, run_at, failed_at nulls first, locked_at nulls first, queue)")
12
+ else
13
+ add_index :delayed_jobs, %w[priority run_at locked_at failed_at queue], name: 'get_delayed_jobs_index'
14
+ end
15
+
16
+ # unused indexes
17
+ remove_index :delayed_jobs, name: 'delayed_jobs_queue'
18
+ remove_index :delayed_jobs, name: 'delayed_jobs_priority'
19
+ end
20
+
21
+ def down
22
+ remove_index :delayed_jobs, name: 'get_delayed_jobs_index'
23
+ add_index :delayed_jobs, %i[priority run_at], name: 'delayed_jobs_priority'
24
+ add_index :delayed_jobs, [:queue], name: 'delayed_jobs_queue'
25
+ end
26
+ end
@@ -0,0 +1,40 @@
1
+ class OptimizeDelayedJobs < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ create_table :failed_jobs do |t|
8
+ t.integer 'priority', default: 0
9
+ t.integer 'attempts', default: 0
10
+ t.string 'handler', limit: 512_000
11
+ t.integer 'original_id', limit: 8
12
+ t.text 'last_error'
13
+ t.string 'queue'
14
+ t.datetime 'run_at'
15
+ t.datetime 'locked_at'
16
+ t.datetime 'failed_at'
17
+ t.string 'locked_by'
18
+ t.datetime 'created_at'
19
+ t.datetime 'updated_at'
20
+ t.string 'tag'
21
+ t.integer 'max_attempts'
22
+ t.string 'strand'
23
+ end
24
+
25
+ remove_index :delayed_jobs, name: 'get_delayed_jobs_index'
26
+ remove_index :delayed_jobs, [:strand]
27
+
28
+ add_index :delayed_jobs, %w[run_at queue locked_at strand priority], name: 'index_delayed_jobs_for_get_next'
29
+ add_index :delayed_jobs, %w[strand id], name: 'index_delayed_jobs_on_strand'
30
+
31
+ # move all failed jobs to the new failed table
32
+ Delayed::Backend::ActiveRecord::Job.where('failed_at IS NOT NULL').find_each do |job|
33
+ job.fail! unless job.on_hold?
34
+ end
35
+ end
36
+
37
+ def down
38
+ raise ActiveRecord::IrreversibleMigration
39
+ end
40
+ end
@@ -0,0 +1,52 @@
1
+ class AddDelayedJobsNextInStrand < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ remove_index :delayed_jobs, name: 'index_delayed_jobs_for_get_next'
8
+
9
+ add_column :delayed_jobs, :next_in_strand, :boolean, default: true, null: false
10
+
11
+ # create the new index
12
+ connection.execute("CREATE INDEX get_delayed_jobs_index ON #{::Delayed::Job.quoted_table_name} (priority, run_at, queue) WHERE locked_at IS NULL AND next_in_strand = 't'")
13
+
14
+ # create the insert trigger
15
+ execute(<<-CODE)
16
+ CREATE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
17
+ BEGIN
18
+ LOCK delayed_jobs IN SHARE ROW EXCLUSIVE MODE;
19
+ IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
20
+ NEW.next_in_strand := 'f';
21
+ END IF;
22
+ RETURN NEW;
23
+ END;
24
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
25
+ CODE
26
+ execute("CREATE TRIGGER delayed_jobs_before_insert_row_tr BEFORE INSERT ON #{::Delayed::Job.quoted_table_name} FOR EACH ROW WHEN (NEW.strand IS NOT NULL) EXECUTE PROCEDURE #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')}()")
27
+
28
+ # create the delete trigger
29
+ execute(<<-CODE)
30
+ CREATE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
31
+ BEGIN
32
+ 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);
33
+ RETURN OLD;
34
+ END;
35
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
36
+ CODE
37
+ execute("CREATE TRIGGER delayed_jobs_after_delete_row_tr AFTER DELETE ON #{::Delayed::Job.quoted_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')} ()")
38
+
39
+ execute(%{UPDATE #{::Delayed::Job.quoted_table_name} SET next_in_strand = 'f' WHERE strand IS NOT NULL AND id <> (SELECT id FROM #{::Delayed::Job.quoted_table_name} j2 WHERE j2.strand = delayed_jobs.strand ORDER BY j2.strand, j2.id ASC LIMIT 1)})
40
+ end
41
+
42
+ def down
43
+ execute %(DROP TRIGGER delayed_jobs_before_insert_row_tr ON #{::Delayed::Job.quoted_table_name})
44
+ execute %{DROP FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} ()}
45
+ execute %(DROP TRIGGER delayed_jobs_after_delete_row_tr ON #{::Delayed::Job.quoted_table_name})
46
+ execute %{DROP FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} ()}
47
+
48
+ remove_column :delayed_jobs, :next_in_strand
49
+ remove_index :delayed_jobs, name: 'get_delayed_jobs_index'
50
+ add_index :delayed_jobs, %w[run_at queue locked_at strand priority], name: 'index_delayed_jobs_for_get_next'
51
+ end
52
+ end
@@ -0,0 +1,31 @@
1
+ class DelayedJobsDeleteTriggerLockForUpdate < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ if connection.adapter_name == 'PostgreSQL'
8
+ execute(<<-CODE)
9
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
10
+ BEGIN
11
+ 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);
12
+ RETURN OLD;
13
+ END;
14
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
15
+ CODE
16
+ end
17
+ end
18
+
19
+ def down
20
+ if connection.adapter_name == 'PostgreSQL'
21
+ execute(<<-CODE)
22
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
23
+ BEGIN
24
+ 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);
25
+ RETURN OLD;
26
+ END;
27
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
28
+ CODE
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ class DropPsqlJobsPopFn < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ if connection.adapter_name == 'PostgreSQL'
8
+ connection.execute('DROP FUNCTION IF EXISTS pop_from_delayed_jobs(varchar, varchar, integer, integer, timestamp without time zone)')
9
+ end
10
+ end
11
+
12
+ def down
13
+ raise ActiveRecord::IrreversibleMigration
14
+ end
15
+ end
@@ -0,0 +1,80 @@
1
+ class DelayedJobsUseAdvisoryLocks < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ # use an advisory lock based on the name of the strand, instead of locking the whole table
8
+ # note that we're using half of the md5, so collisions are possible, but we don't really
9
+ # care because that would just be the old behavior, whereas for the most part locking will
10
+ # be much smaller
11
+ if connection.adapter_name == 'PostgreSQL'
12
+ execute(<<-CODE)
13
+ CREATE FUNCTION #{connection.quote_table_name('half_md5_as_bigint')}(strand varchar) RETURNS bigint AS $$
14
+ DECLARE
15
+ strand_md5 bytea;
16
+ BEGIN
17
+ strand_md5 := decode(md5(strand), 'hex');
18
+ RETURN (CAST(get_byte(strand_md5, 0) AS bigint) << 56) +
19
+ (CAST(get_byte(strand_md5, 1) AS bigint) << 48) +
20
+ (CAST(get_byte(strand_md5, 2) AS bigint) << 40) +
21
+ (CAST(get_byte(strand_md5, 3) AS bigint) << 32) +
22
+ (CAST(get_byte(strand_md5, 4) AS bigint) << 24) +
23
+ (get_byte(strand_md5, 5) << 16) +
24
+ (get_byte(strand_md5, 6) << 8) +
25
+ get_byte(strand_md5, 7);
26
+ END;
27
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
28
+ CODE
29
+
30
+ execute(<<-CODE)
31
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
32
+ BEGIN
33
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(NEW.strand));
34
+ IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
35
+ NEW.next_in_strand := 'f';
36
+ END IF;
37
+ RETURN NEW;
38
+ END;
39
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
40
+ CODE
41
+
42
+ execute(<<-CODE)
43
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
44
+ BEGIN
45
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
46
+ 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);
47
+ RETURN OLD;
48
+ END;
49
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
50
+ CODE
51
+ end
52
+ end
53
+
54
+ def down
55
+ if connection.adapter_name == 'PostgreSQL'
56
+ execute(<<-CODE)
57
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
58
+ BEGIN
59
+ LOCK delayed_jobs IN SHARE ROW EXCLUSIVE MODE;
60
+ IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
61
+ NEW.next_in_strand := 'f';
62
+ END IF;
63
+ RETURN NEW;
64
+ END;
65
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
66
+ CODE
67
+
68
+ execute(<<-CODE)
69
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
70
+ BEGIN
71
+ 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);
72
+ RETURN OLD;
73
+ END;
74
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
75
+ CODE
76
+
77
+ execute("DROP FUNCTION #{connection.quote_table_name('half_md5_as_bigint')}(varchar)")
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,15 @@
1
+ class IndexJobsOnLockedBy < ActiveRecord::Migration[4.2]
2
+ disable_ddl_transaction! if respond_to?(:disable_ddl_transaction!)
3
+
4
+ def connection
5
+ Delayed::Backend::ActiveRecord::Job.connection
6
+ end
7
+
8
+ def up
9
+ add_index :delayed_jobs, :locked_by, algorithm: :concurrently, where: 'locked_by IS NOT NULL'
10
+ end
11
+
12
+ def down
13
+ remove_index :delayed_jobs, :locked_by
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ class AddJobsRunAtIndex < ActiveRecord::Migration[4.2]
2
+ disable_ddl_transaction! if respond_to?(:disable_ddl_transaction!)
3
+
4
+ def connection
5
+ Delayed::Backend::ActiveRecord::Job.connection
6
+ end
7
+
8
+ def up
9
+ add_index :delayed_jobs, %w[run_at tag], algorithm: :concurrently
10
+ end
11
+
12
+ def down
13
+ remove_index :delayed_jobs, name: 'index_delayed_jobs_on_run_at_and_tag'
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ class ChangeDelayedJobsHandlerToText < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Job.connection
4
+ end
5
+
6
+ def up
7
+ change_column :delayed_jobs, :handler, :text
8
+ end
9
+
10
+ def down
11
+ change_column :delayed_jobs, :handler, :string, limit: 500.kilobytes
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class AddFailedJobsOriginalJobId < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ add_column :failed_jobs, :original_job_id, :integer, limit: 8
8
+ end
9
+
10
+ def down
11
+ remove_column :failed_jobs, :original_job_id
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ class CopyFailedJobsOriginalId < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ # this is a smaller, less frequently accessed table, so we just update all at once
8
+ Delayed::Backend::ActiveRecord::Job::Failed.where('original_job_id is null').update_all('original_job_id = original_id')
9
+ end
10
+
11
+ def down; end
12
+ end
@@ -0,0 +1,13 @@
1
+ class DropFailedJobsOriginalId < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def up
7
+ remove_column :failed_jobs, :original_id
8
+ end
9
+
10
+ def down
11
+ add_column :failed_jobs, :original_id, :integer, limit: 8
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ class AddSourceToJobs < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Job.connection
4
+ end
5
+
6
+ def up
7
+ add_column :delayed_jobs, :source, :string
8
+ add_column :failed_jobs, :source, :string
9
+ end
10
+
11
+ def down
12
+ remove_column :delayed_jobs, :source
13
+ remove_column :failed_jobs, :source
14
+ end
15
+ end
@@ -0,0 +1,70 @@
1
+ class AddMaxConcurrentToJobs < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Job.connection
4
+ end
5
+
6
+ def up
7
+ add_column :delayed_jobs, :max_concurrent, :integer, default: 1, null: false
8
+
9
+ if connection.adapter_name == 'PostgreSQL'
10
+ execute(<<-CODE)
11
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
12
+ BEGIN
13
+ IF NEW.strand IS NOT NULL THEN
14
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(NEW.strand));
15
+ IF (SELECT COUNT(*) FROM delayed_jobs WHERE strand = NEW.strand) >= NEW.max_concurrent THEN
16
+ NEW.next_in_strand := 'f';
17
+ END IF;
18
+ END IF;
19
+ RETURN NEW;
20
+ END;
21
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
22
+ CODE
23
+
24
+ execute(<<-CODE)
25
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
26
+ BEGIN
27
+ IF OLD.strand IS NOT NULL THEN
28
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
29
+ IF (SELECT COUNT(*) FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't') < OLD.max_concurrent THEN
30
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (
31
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
32
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE
33
+ );
34
+ END IF;
35
+ END IF;
36
+ RETURN OLD;
37
+ END;
38
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
39
+ CODE
40
+ end
41
+ end
42
+
43
+ def down
44
+ remove_column :delayed_jobs, :max_concurrent
45
+
46
+ if connection.adapter_name == 'PostgreSQL'
47
+ execute(<<-CODE)
48
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
49
+ BEGIN
50
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(NEW.strand));
51
+ IF (SELECT 1 FROM delayed_jobs WHERE strand = NEW.strand LIMIT 1) = 1 THEN
52
+ NEW.next_in_strand := 'f';
53
+ END IF;
54
+ RETURN NEW;
55
+ END;
56
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
57
+ CODE
58
+
59
+ execute(<<-CODE)
60
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
61
+ BEGIN
62
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
63
+ 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);
64
+ RETURN OLD;
65
+ END;
66
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
67
+ CODE
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,15 @@
1
+ class AddExpiresAtToJobs < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Job.connection
4
+ end
5
+
6
+ def up
7
+ add_column :delayed_jobs, :expires_at, :datetime
8
+ add_column :failed_jobs, :expires_at, :datetime
9
+ end
10
+
11
+ def down
12
+ remove_column :delayed_jobs, :expires_at
13
+ remove_column :failed_jobs, :expires_at
14
+ end
15
+ end
@@ -0,0 +1,50 @@
1
+ class ImproveMaxConcurrent < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Job.connection
4
+ end
5
+
6
+ def up
7
+ if connection.adapter_name == 'PostgreSQL'
8
+ execute(<<-CODE)
9
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
10
+ DECLARE
11
+ running_count integer;
12
+ BEGIN
13
+ IF OLD.strand IS NOT NULL THEN
14
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
15
+ running_count := (SELECT COUNT(*) FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't');
16
+ IF running_count < OLD.max_concurrent THEN
17
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
18
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
19
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
20
+ );
21
+ END IF;
22
+ END IF;
23
+ RETURN OLD;
24
+ END;
25
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
26
+ CODE
27
+ end
28
+ end
29
+
30
+ def down
31
+ if connection.adapter_name == 'PostgreSQL'
32
+ execute(<<-CODE)
33
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
34
+ BEGIN
35
+ IF OLD.strand IS NOT NULL THEN
36
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
37
+ IF (SELECT COUNT(*) FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't') < OLD.max_concurrent THEN
38
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id = (
39
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
40
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE
41
+ );
42
+ END IF;
43
+ END IF;
44
+ RETURN OLD;
45
+ END;
46
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
47
+ CODE
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,39 @@
1
+ class AddBackDefaultStringLimitsJobs < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Job.connection
4
+ end
5
+
6
+ def up
7
+ drop_triggers
8
+
9
+ add_string_limit_if_missing :delayed_jobs, :queue
10
+ add_string_limit_if_missing :delayed_jobs, :locked_by
11
+ add_string_limit_if_missing :delayed_jobs, :tag
12
+ add_string_limit_if_missing :delayed_jobs, :strand
13
+ add_string_limit_if_missing :delayed_jobs, :source
14
+
15
+ add_string_limit_if_missing :failed_jobs, :queue
16
+ add_string_limit_if_missing :failed_jobs, :locked_by
17
+ add_string_limit_if_missing :failed_jobs, :tag
18
+ add_string_limit_if_missing :failed_jobs, :strand
19
+ add_string_limit_if_missing :failed_jobs, :source
20
+
21
+ readd_triggers
22
+ end
23
+
24
+ def drop_triggers
25
+ execute %(DROP TRIGGER delayed_jobs_before_insert_row_tr ON #{::Delayed::Job.quoted_table_name})
26
+ execute %(DROP TRIGGER delayed_jobs_after_delete_row_tr ON #{::Delayed::Job.quoted_table_name})
27
+ end
28
+
29
+ def readd_triggers
30
+ execute("CREATE TRIGGER delayed_jobs_before_insert_row_tr BEFORE INSERT ON #{::Delayed::Job.quoted_table_name} FOR EACH ROW WHEN (NEW.strand IS NOT NULL) EXECUTE PROCEDURE #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')}()")
31
+ execute("CREATE TRIGGER delayed_jobs_after_delete_row_tr AFTER DELETE ON #{::Delayed::Job.quoted_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')}()")
32
+ end
33
+
34
+ def add_string_limit_if_missing(table, column)
35
+ return if column_exists?(table, column, :string, limit: 255)
36
+
37
+ change_column table, column, :string, limit: 255
38
+ end
39
+ end
@@ -0,0 +1,95 @@
1
+ class SpeedUpMaxConcurrentTriggers < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Job.connection
4
+ end
5
+
6
+ def up
7
+ if connection.adapter_name == 'PostgreSQL'
8
+ # tl;dr sacrifice some responsiveness to max_concurrent changes for faster performance
9
+ # don't get the count every single time - it's usually safe to just set the next one in line
10
+ # since the max_concurrent doesn't change all that often for a strand
11
+ execute(<<-CODE)
12
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
13
+ DECLARE
14
+ running_count integer;
15
+ BEGIN
16
+ IF OLD.strand IS NOT NULL THEN
17
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
18
+ IF OLD.id % 20 = 0 THEN
19
+ running_count := (SELECT COUNT(*) FROM (
20
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
21
+ ) subquery_for_count);
22
+ IF running_count < OLD.max_concurrent THEN
23
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
24
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
25
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
26
+ );
27
+ END IF;
28
+ ELSE
29
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
30
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
31
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE);
32
+ END IF;
33
+ END IF;
34
+ RETURN OLD;
35
+ END;
36
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
37
+ CODE
38
+
39
+ # don't need the full count on insert
40
+ execute(<<-CODE)
41
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
42
+ BEGIN
43
+ IF NEW.strand IS NOT NULL THEN
44
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(NEW.strand));
45
+ IF (SELECT COUNT(*) FROM (
46
+ SELECT 1 AS one FROM delayed_jobs WHERE strand = NEW.strand LIMIT NEW.max_concurrent
47
+ ) subquery_for_count) = NEW.max_concurrent THEN
48
+ NEW.next_in_strand := 'f';
49
+ END IF;
50
+ END IF;
51
+ RETURN NEW;
52
+ END;
53
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
54
+ CODE
55
+ end
56
+ end
57
+
58
+ def down
59
+ if connection.adapter_name == 'PostgreSQL'
60
+ execute(<<-CODE)
61
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
62
+ DECLARE
63
+ running_count integer;
64
+ BEGIN
65
+ IF OLD.strand IS NOT NULL THEN
66
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
67
+ running_count := (SELECT COUNT(*) FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't');
68
+ IF running_count < OLD.max_concurrent THEN
69
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
70
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
71
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
72
+ );
73
+ END IF;
74
+ END IF;
75
+ RETURN OLD;
76
+ END;
77
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
78
+ CODE
79
+
80
+ execute(<<-CODE)
81
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_before_insert_row_tr_fn')} () RETURNS trigger AS $$
82
+ BEGIN
83
+ IF NEW.strand IS NOT NULL THEN
84
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(NEW.strand));
85
+ IF (SELECT COUNT(*) FROM delayed_jobs WHERE strand = NEW.strand) >= NEW.max_concurrent THEN
86
+ NEW.next_in_strand := 'f';
87
+ END IF;
88
+ END IF;
89
+ RETURN NEW;
90
+ END;
91
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
92
+ CODE
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,15 @@
1
+ class MakeCriticalColumnsNotNull < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Job.connection
4
+ end
5
+
6
+ def up
7
+ change_column_null :delayed_jobs, :run_at, false
8
+ change_column_null :delayed_jobs, :queue, false
9
+ end
10
+
11
+ def down
12
+ change_column_null :delayed_jobs, :run_at, true
13
+ change_column_null :delayed_jobs, :queue, true
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ class AddIdToGetDelayedJobsIndex < ActiveRecord::Migration[4.2]
2
+ disable_ddl_transaction! if respond_to?(:disable_ddl_transaction!)
3
+
4
+ def connection
5
+ Delayed::Job.connection
6
+ end
7
+
8
+ def up
9
+ rename_index :delayed_jobs, 'get_delayed_jobs_index', 'get_delayed_jobs_index_old'
10
+ add_index :delayed_jobs, %i[queue priority run_at id],
11
+ algorithm: :concurrently,
12
+ where: 'locked_at IS NULL AND next_in_strand',
13
+ name: 'get_delayed_jobs_index'
14
+ remove_index :delayed_jobs, name: 'get_delayed_jobs_index_old'
15
+ end
16
+
17
+ def down
18
+ rename_index :delayed_jobs, 'get_delayed_jobs_index', 'get_delayed_jobs_index_old'
19
+ add_index :delayed_jobs, %i[priority run_at queue],
20
+ algorithm: :concurrently,
21
+ where: 'locked_at IS NULL AND next_in_strand',
22
+ name: 'get_delayed_jobs_index'
23
+ remove_index :delayed_jobs, name: 'get_delayed_jobs_index_old'
24
+ end
25
+ end
@@ -0,0 +1,94 @@
1
+ class SpeedUpMaxConcurrentDeleteTrigger < ActiveRecord::Migration[4.2]
2
+ def connection
3
+ Delayed::Job.connection
4
+ end
5
+
6
+ def up
7
+ if connection.adapter_name == 'PostgreSQL'
8
+ # tl;dr sacrifice some responsiveness to max_concurrent changes for faster performance
9
+ # don't get the count every single time - it's usually safe to just set the next one in line
10
+ # since the max_concurrent doesn't change all that often for a strand
11
+ execute(<<-SQL)
12
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
13
+ DECLARE
14
+ running_count integer;
15
+ should_lock boolean;
16
+ should_be_precise boolean;
17
+ BEGIN
18
+ IF OLD.strand IS NOT NULL THEN
19
+ should_lock := true;
20
+ should_be_precise := OLD.id % (OLD.max_concurrent * 4) = 0;
21
+
22
+ IF NOT should_be_precise AND OLD.max_concurrent > 16 THEN
23
+ running_count := (SELECT COUNT(*) FROM (
24
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
25
+ ) subquery_for_count);
26
+ should_lock := running_count < OLD.max_concurrent;
27
+ END IF;
28
+
29
+ IF should_lock THEN
30
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
31
+ END IF;
32
+
33
+ IF should_be_precise THEN
34
+ running_count := (SELECT COUNT(*) FROM (
35
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
36
+ ) subquery_for_count);
37
+ IF running_count < OLD.max_concurrent THEN
38
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
39
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
40
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
41
+ );
42
+ END IF;
43
+ ELSE
44
+ -- n-strands don't require precise ordering; we can make this query more performant
45
+ IF OLD.max_concurrent > 1 THEN
46
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
47
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
48
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE SKIP LOCKED);
49
+ ELSE
50
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
51
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
52
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE);
53
+ END IF;
54
+ END IF;
55
+ END IF;
56
+ RETURN OLD;
57
+ END;
58
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
59
+ SQL
60
+ end
61
+ end
62
+
63
+ def down
64
+ if connection.adapter_name == 'PostgreSQL'
65
+ execute(<<-SQL)
66
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
67
+ DECLARE
68
+ running_count integer;
69
+ BEGIN
70
+ IF OLD.strand IS NOT NULL THEN
71
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
72
+ IF OLD.id % 20 = 0 THEN
73
+ running_count := (SELECT COUNT(*) FROM (
74
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
75
+ ) subquery_for_count);
76
+ IF running_count < OLD.max_concurrent THEN
77
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
78
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
79
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
80
+ );
81
+ END IF;
82
+ ELSE
83
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
84
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
85
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE);
86
+ END IF;
87
+ END IF;
88
+ RETURN OLD;
89
+ END;
90
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
91
+ SQL
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,126 @@
1
+ class AddStrandOrderOverride < ActiveRecord::Migration[4.2]
2
+ disable_ddl_transaction! if respond_to?(:disable_ddl_transaction!)
3
+
4
+ def connection
5
+ Delayed::Job.connection
6
+ end
7
+
8
+ def up
9
+ add_column :delayed_jobs, :strand_order_override, :integer, default: 0, null: false
10
+ add_column :failed_jobs, :strand_order_override, :integer, default: 0, null: false
11
+ add_index :delayed_jobs, %i[strand strand_order_override id],
12
+ algorithm: :concurrently,
13
+ where: 'strand IS NOT NULL',
14
+ name: 'next_in_strand_index'
15
+
16
+ if connection.adapter_name == 'PostgreSQL'
17
+ # Use the strand_order_override as the primary sorting mechanism (useful when moving between jobs queues without preserving ID ordering)
18
+ execute(<<-SQL)
19
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
20
+ DECLARE
21
+ running_count integer;
22
+ should_lock boolean;
23
+ should_be_precise boolean;
24
+ BEGIN
25
+ IF OLD.strand IS NOT NULL THEN
26
+ should_lock := true;
27
+ should_be_precise := OLD.id % (OLD.max_concurrent * 4) = 0;
28
+
29
+ IF NOT should_be_precise AND OLD.max_concurrent > 16 THEN
30
+ running_count := (SELECT COUNT(*) FROM (
31
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
32
+ ) subquery_for_count);
33
+ should_lock := running_count < OLD.max_concurrent;
34
+ END IF;
35
+
36
+ IF should_lock THEN
37
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
38
+ END IF;
39
+
40
+ IF should_be_precise THEN
41
+ running_count := (SELECT COUNT(*) FROM (
42
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
43
+ ) subquery_for_count);
44
+ IF running_count < OLD.max_concurrent THEN
45
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
46
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
47
+ j2.strand = OLD.strand ORDER BY j2.strand_order_override ASC, j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
48
+ );
49
+ END IF;
50
+ ELSE
51
+ -- n-strands don't require precise ordering; we can make this query more performant
52
+ IF OLD.max_concurrent > 1 THEN
53
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
54
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
55
+ j2.strand = OLD.strand ORDER BY j2.strand_order_override ASC, j2.id ASC LIMIT 1 FOR UPDATE SKIP LOCKED);
56
+ ELSE
57
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
58
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
59
+ j2.strand = OLD.strand ORDER BY j2.strand_order_override ASC, j2.id ASC LIMIT 1 FOR UPDATE);
60
+ END IF;
61
+ END IF;
62
+ END IF;
63
+ RETURN OLD;
64
+ END;
65
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
66
+ SQL
67
+ end
68
+ end
69
+
70
+ def down
71
+ remove_column :delayed_jobs, :strand_order_override, :integer
72
+ remove_column :failed_jobs, :strand_order_override, :integer
73
+
74
+ if connection.adapter_name == 'PostgreSQL'
75
+ execute(<<-SQL)
76
+ CREATE OR REPLACE FUNCTION #{connection.quote_table_name('delayed_jobs_after_delete_row_tr_fn')} () RETURNS trigger AS $$
77
+ DECLARE
78
+ running_count integer;
79
+ should_lock boolean;
80
+ should_be_precise boolean;
81
+ BEGIN
82
+ IF OLD.strand IS NOT NULL THEN
83
+ should_lock := true;
84
+ should_be_precise := OLD.id % (OLD.max_concurrent * 4) = 0;
85
+
86
+ IF NOT should_be_precise AND OLD.max_concurrent > 16 THEN
87
+ running_count := (SELECT COUNT(*) FROM (
88
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
89
+ ) subquery_for_count);
90
+ should_lock := running_count < OLD.max_concurrent;
91
+ END IF;
92
+
93
+ IF should_lock THEN
94
+ PERFORM pg_advisory_xact_lock(half_md5_as_bigint(OLD.strand));
95
+ END IF;
96
+
97
+ IF should_be_precise THEN
98
+ running_count := (SELECT COUNT(*) FROM (
99
+ SELECT 1 as one FROM delayed_jobs WHERE strand = OLD.strand AND next_in_strand = 't' LIMIT OLD.max_concurrent
100
+ ) subquery_for_count);
101
+ IF running_count < OLD.max_concurrent THEN
102
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id IN (
103
+ SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
104
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT (OLD.max_concurrent - running_count) FOR UPDATE
105
+ );
106
+ END IF;
107
+ ELSE
108
+ -- n-strands don't require precise ordering; we can make this query more performant
109
+ IF OLD.max_concurrent > 1 THEN
110
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
111
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
112
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE SKIP LOCKED);
113
+ ELSE
114
+ UPDATE delayed_jobs SET next_in_strand = 't' WHERE id =
115
+ (SELECT id FROM delayed_jobs j2 WHERE next_in_strand = 'f' AND
116
+ j2.strand = OLD.strand ORDER BY j2.id ASC LIMIT 1 FOR UPDATE);
117
+ END IF;
118
+ END IF;
119
+ END IF;
120
+ RETURN OLD;
121
+ END;
122
+ $$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
123
+ SQL
124
+ end
125
+ end
126
+ end
@@ -17,6 +17,16 @@ module SwitchmanInstJobs
17
17
  ensure
18
18
  ::ActiveRecord::Migration.open_migrations -= 1
19
19
  end
20
+
21
+ def copy(destination, sources, options = {})
22
+ if sources.delete('delayed_engine')
23
+ # rubocop:disable Rails/Output
24
+ puts 'NOTE: Not installing delayed_engine migrations in an application using switchman-inst-jobs'
25
+ puts '(use rake switchman_inst_jobs:install:migrations instead)'
26
+ # rubocop:enable Rails/Output
27
+ end
28
+ super
29
+ end
20
30
  end
21
31
  end
22
32
  end
@@ -183,12 +183,14 @@ module SwitchmanInstJobs
183
183
  # Adapted from get_and_lock_next_available in delayed/backend/active_record.rb
184
184
  target_jobs = scope.limit(1000).lock('FOR UPDATE SKIP LOCKED')
185
185
 
186
- query = "WITH limited_jobs AS (#{target_jobs.to_sql}) " \
187
- "UPDATE #{::Delayed::Job.quoted_table_name} " \
188
- "SET locked_by = #{::Delayed::Job.connection.quote(::Delayed::Backend::Base::ON_HOLD_LOCKED_BY)}, " \
189
- "locked_at = #{::Delayed::Job.connection.quote(::Delayed::Job.db_time_now)} "\
190
- "FROM limited_jobs WHERE limited_jobs.id=#{::Delayed::Job.quoted_table_name}.id " \
191
- "RETURNING #{::Delayed::Job.quoted_table_name}.*"
186
+ query = source_shard.activate(:delayed_jobs) do
187
+ "WITH limited_jobs AS (#{target_jobs.to_sql}) " \
188
+ "UPDATE #{::Delayed::Job.quoted_table_name} " \
189
+ "SET locked_by = #{::Delayed::Job.connection.quote(::Delayed::Backend::Base::ON_HOLD_LOCKED_BY)}, " \
190
+ "locked_at = #{::Delayed::Job.connection.quote(::Delayed::Job.db_time_now)} "\
191
+ "FROM limited_jobs WHERE limited_jobs.id=#{::Delayed::Job.quoted_table_name}.id " \
192
+ "RETURNING #{::Delayed::Job.quoted_table_name}.*"
193
+ end
192
194
 
193
195
  jobs = source_shard.activate(:delayed_jobs) { ::Delayed::Job.find_by_sql(query) }
194
196
  new_jobs = jobs.map do |job|
@@ -1,3 +1,3 @@
1
1
  module SwitchmanInstJobs
2
- VERSION = '1.5.2'.freeze
2
+ VERSION = '1.6.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: switchman-inst-jobs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Petty
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-27 00:00:00.000000000 Z
11
+ date: 2020-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inst-jobs
@@ -70,20 +70,20 @@ dependencies:
70
70
  requirements:
71
71
  - - ">="
72
72
  - !ruby/object:Gem::Version
73
- version: '1.14'
73
+ version: '1.16'
74
74
  - - "<"
75
75
  - !ruby/object:Gem::Version
76
- version: '1.16'
76
+ version: '1.17'
77
77
  type: :runtime
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - ">="
82
82
  - !ruby/object:Gem::Version
83
- version: '1.14'
83
+ version: '1.16'
84
84
  - - "<"
85
85
  - !ruby/object:Gem::Version
86
- version: '1.16'
86
+ version: '1.17'
87
87
  - !ruby/object:Gem::Dependency
88
88
  name: bundler
89
89
  requirement: !ruby/object:Gem::Requirement
@@ -266,17 +266,42 @@ dependencies:
266
266
  - - "~>"
267
267
  - !ruby/object:Gem::Version
268
268
  version: '1.4'
269
- description:
269
+ description:
270
270
  email:
271
271
  - bpetty@instructure.com
272
272
  executables: []
273
273
  extensions: []
274
274
  extra_rdoc_files: []
275
275
  files:
276
+ - db/migrate/20101216224513_create_delayed_jobs.rb
277
+ - db/migrate/20110208031356_add_delayed_jobs_tag.rb
278
+ - db/migrate/20110426161613_add_delayed_jobs_max_attempts.rb
279
+ - db/migrate/20110516225834_add_delayed_jobs_strand.rb
280
+ - db/migrate/20110531144916_cleanup_delayed_jobs_indexes.rb
281
+ - db/migrate/20110610213249_optimize_delayed_jobs.rb
282
+ - db/migrate/20110831210257_add_delayed_jobs_next_in_strand.rb
283
+ - db/migrate/20120510004759_delayed_jobs_delete_trigger_lock_for_update.rb
284
+ - db/migrate/20120531150712_drop_psql_jobs_pop_fn.rb
285
+ - db/migrate/20120607164022_delayed_jobs_use_advisory_locks.rb
286
+ - db/migrate/20120607181141_index_jobs_on_locked_by.rb
287
+ - db/migrate/20120608191051_add_jobs_run_at_index.rb
288
+ - db/migrate/20120927184213_change_delayed_jobs_handler_to_text.rb
289
+ - db/migrate/20140505215131_add_failed_jobs_original_job_id.rb
290
+ - db/migrate/20140505215510_copy_failed_jobs_original_id.rb
291
+ - db/migrate/20140505223637_drop_failed_jobs_original_id.rb
292
+ - db/migrate/20140512213941_add_source_to_jobs.rb
293
+ - db/migrate/20150807133223_add_max_concurrent_to_jobs.rb
294
+ - db/migrate/20151123210429_add_expires_at_to_jobs.rb
295
+ - db/migrate/20151210162949_improve_max_concurrent.rb
296
+ - db/migrate/20161206323555_add_back_default_string_limits_jobs.rb
276
297
  - db/migrate/20170308045400_add_shard_id_to_delayed_jobs.rb
277
- - db/migrate/20180628153808_set_search_paths_on_functions.rb
298
+ - db/migrate/20181217155351_speed_up_max_concurrent_triggers.rb
299
+ - db/migrate/20190726154743_make_critical_columns_not_null.rb
300
+ - db/migrate/20200330230722_add_id_to_get_delayed_jobs_index.rb
278
301
  - db/migrate/20200818130101_add_on_hold_to_switchman_shards.rb
279
302
  - db/migrate/20200822014259_add_block_stranded_to_switchman_shards.rb
303
+ - db/migrate/20200824222232_speed_up_max_concurrent_delete_trigger.rb
304
+ - db/migrate/20200825011002_add_strand_order_override.rb
280
305
  - lib/switchman-inst-jobs.rb
281
306
  - lib/switchman_inst_jobs.rb
282
307
  - lib/switchman_inst_jobs/active_record/connection_adapters/postgresql_adapter.rb
@@ -300,7 +325,7 @@ homepage: https://github.com/instructure/switchman-inst-jobs
300
325
  licenses:
301
326
  - MIT
302
327
  metadata: {}
303
- post_install_message:
328
+ post_install_message:
304
329
  rdoc_options: []
305
330
  require_paths:
306
331
  - lib
@@ -316,7 +341,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
316
341
  version: '0'
317
342
  requirements: []
318
343
  rubygems_version: 3.0.3
319
- signing_key:
344
+ signing_key:
320
345
  specification_version: 4
321
346
  summary: Switchman and Instructure Jobs compatibility gem.
322
347
  test_files: []
@@ -1,15 +0,0 @@
1
- class SetSearchPathsOnFunctions < ActiveRecord::Migration[4.2]
2
- disable_ddl_transaction!
3
-
4
- def up
5
- set_search_path('delayed_jobs_after_delete_row_tr_fn', '()')
6
- set_search_path('delayed_jobs_before_insert_row_tr_fn', '()')
7
- set_search_path('half_md5_as_bigint', '(varchar)')
8
- end
9
-
10
- def down
11
- set_search_path('delayed_jobs_after_delete_row_tr_fn', '()', 'DEFAULT')
12
- set_search_path('delayed_jobs_before_insert_row_tr_fn', '()', 'DEFAULT')
13
- set_search_path('half_md5_as_bigint', '(varchar)', 'DEFAULT')
14
- end
15
- end