switchman-inst-jobs 4.0.13 → 4.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/db/migrate/20101216224513_create_delayed_jobs.rb +5 -3
- data/db/migrate/20110208031356_add_delayed_jobs_tag.rb +2 -0
- data/db/migrate/20110426161613_add_delayed_jobs_max_attempts.rb +2 -0
- data/db/migrate/20110516225834_add_delayed_jobs_strand.rb +2 -0
- data/db/migrate/20110531144916_cleanup_delayed_jobs_indexes.rb +9 -7
- data/db/migrate/20110610213249_optimize_delayed_jobs.rb +20 -18
- data/db/migrate/20110831210257_add_delayed_jobs_next_in_strand.rb +28 -26
- data/db/migrate/20120510004759_delayed_jobs_delete_trigger_lock_for_update.rb +20 -18
- data/db/migrate/20120531150712_drop_psql_jobs_pop_fn.rb +4 -2
- data/db/migrate/20120607164022_delayed_jobs_use_advisory_locks.rb +61 -59
- data/db/migrate/20120607181141_index_jobs_on_locked_by.rb +3 -1
- data/db/migrate/20120608191051_add_jobs_run_at_index.rb +3 -1
- data/db/migrate/20120927184213_change_delayed_jobs_handler_to_text.rb +2 -0
- data/db/migrate/20140505215131_add_failed_jobs_original_job_id.rb +2 -0
- data/db/migrate/20140505215510_copy_failed_jobs_original_id.rb +2 -0
- data/db/migrate/20140505223637_drop_failed_jobs_original_id.rb +2 -0
- data/db/migrate/20140512213941_add_source_to_jobs.rb +2 -0
- data/db/migrate/20150807133223_add_max_concurrent_to_jobs.rb +51 -49
- data/db/migrate/20151123210429_add_expires_at_to_jobs.rb +2 -0
- data/db/migrate/20151210162949_improve_max_concurrent.rb +37 -35
- data/db/migrate/20161206323555_add_back_default_string_limits_jobs.rb +4 -2
- data/db/migrate/20170308045400_add_shard_id_to_delayed_jobs.rb +2 -0
- data/db/migrate/20170308045401_add_delayed_jobs_shard_id_to_switchman_shards.rb +2 -0
- data/db/migrate/20181217155351_speed_up_max_concurrent_triggers.rb +73 -71
- data/db/migrate/20190726154743_make_critical_columns_not_null.rb +2 -0
- data/db/migrate/20200330230722_add_id_to_get_delayed_jobs_index.rb +14 -10
- data/db/migrate/20200818130101_add_on_hold_to_switchman_shards.rb +2 -0
- data/db/migrate/20200822014259_add_block_stranded_to_switchman_shards.rb +2 -0
- data/db/migrate/20200824222232_speed_up_max_concurrent_delete_trigger.rb +6 -4
- data/db/migrate/20200825011002_add_strand_order_override.rb +10 -7
- data/db/migrate/20210809145804_add_n_strand_index.rb +4 -3
- data/db/migrate/20210812210128_add_singleton_column.rb +12 -12
- data/db/migrate/20210917232626_add_delete_conflicting_singletons_before_unlock_trigger.rb +3 -3
- data/db/migrate/20210928174754_fix_singleton_condition_in_before_insert.rb +2 -2
- data/db/migrate/20210929204903_update_conflicting_singleton_function_to_use_index.rb +2 -2
- data/db/migrate/20211101190934_update_after_delete_trigger_for_singleton_index.rb +2 -2
- data/db/migrate/20211207094200_update_after_delete_trigger_for_singleton_transition_cases.rb +2 -2
- data/db/migrate/20211220112800_fix_singleton_race_condition_insert.rb +2 -2
- data/db/migrate/20211220113000_fix_singleton_race_condition_delete.rb +2 -2
- data/db/migrate/20220127091200_fix_singleton_unique_constraint.rb +6 -6
- data/db/migrate/20220128084800_update_insert_trigger_for_singleton_unique_constraint_change.rb +2 -2
- data/db/migrate/20220128084900_update_delete_trigger_for_singleton_unique_constraint_change.rb +2 -2
- data/db/migrate/20220203063200_remove_old_singleton_index.rb +8 -8
- data/db/migrate/20220328152900_add_failed_jobs_indicies.rb +2 -2
- data/lib/switchman-inst-jobs.rb +3 -1
- data/lib/switchman_inst_jobs/active_record/connection_adapters/postgresql_adapter.rb +3 -1
- data/lib/switchman_inst_jobs/active_record/migration.rb +5 -3
- data/lib/switchman_inst_jobs/delayed/backend/active_record/abstract_job.rb +2 -0
- data/lib/switchman_inst_jobs/delayed/backend/base.rb +10 -2
- data/lib/switchman_inst_jobs/delayed/message_sending.rb +2 -0
- data/lib/switchman_inst_jobs/delayed/pool.rb +2 -0
- data/lib/switchman_inst_jobs/delayed/settings.rb +3 -1
- data/lib/switchman_inst_jobs/delayed/worker/health_check.rb +5 -3
- data/lib/switchman_inst_jobs/delayed/worker.rb +2 -0
- data/lib/switchman_inst_jobs/engine.rb +7 -5
- data/lib/switchman_inst_jobs/guard_rail.rb +2 -0
- data/lib/switchman_inst_jobs/jobs_migrator.rb +58 -55
- data/lib/switchman_inst_jobs/new_relic.rb +2 -0
- data/lib/switchman_inst_jobs/switchman/database_server.rb +3 -1
- data/lib/switchman_inst_jobs/switchman/default_shard.rb +2 -0
- data/lib/switchman_inst_jobs/switchman/shard.rb +27 -23
- data/lib/switchman_inst_jobs/timed_cache.rb +2 -0
- data/lib/switchman_inst_jobs/version.rb +3 -1
- data/lib/switchman_inst_jobs/yaml_extensions.rb +3 -1
- data/lib/switchman_inst_jobs.rb +22 -20
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f1621254a45cdb7658e081093f093e8e494040d9c504d973c7e61418f2101b1
|
4
|
+
data.tar.gz: aef5ccde279a3f6e08ed178e9d6b7debd44657fbd813d51cff32e625783e9b27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 ==
|
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:
|
32
|
-
add_index :delayed_jobs, [:queue], name:
|
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,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
|
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:
|
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:
|
14
|
-
remove_index :delayed_jobs, name:
|
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:
|
19
|
-
add_index :delayed_jobs, %i[priority run_at], name:
|
20
|
-
add_index :delayed_jobs, [:queue], name:
|
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
|
5
|
-
t.integer
|
6
|
-
t.string
|
7
|
-
t.integer
|
8
|
-
t.text
|
9
|
-
t.string
|
10
|
-
t.datetime
|
11
|
-
t.datetime
|
12
|
-
t.datetime
|
13
|
-
t.string
|
14
|
-
t.datetime
|
15
|
-
t.datetime
|
16
|
-
t.string
|
17
|
-
t.integer
|
18
|
-
t.string
|
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:
|
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:
|
25
|
-
add_index :delayed_jobs, %w[strand id], name:
|
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:
|
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(
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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(
|
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(
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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(
|
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(
|
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(
|
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:
|
46
|
-
add_index :delayed_jobs, %w[run_at queue locked_at strand priority], name:
|
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 ==
|
4
|
-
execute(
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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 ==
|
17
|
-
execute(
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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 ==
|
4
|
-
connection.execute(
|
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 ==
|
8
|
-
execute(
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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(
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
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(
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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 ==
|
52
|
-
execute(
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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(
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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(
|
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:
|
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:
|
11
|
+
remove_index :delayed_jobs, name: "index_delayed_jobs_on_run_at_and_tag"
|
10
12
|
end
|
11
13
|
end
|
@@ -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 ==
|
6
|
-
execute(
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
CODE
|
17
|
+
RETURN NEW;
|
18
|
+
END;
|
19
|
+
$$ LANGUAGE plpgsql SET search_path TO #{::Switchman::Shard.current.name};
|
20
|
+
SQL
|
19
21
|
|
20
|
-
execute(
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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 ==
|
43
|
-
execute(
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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(
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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
|