canvas-jobs 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +7 -0
  2. data/db/migrate/20101216224513_create_delayed_jobs.rb +40 -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 +13 -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/lib/canvas-jobs.rb +1 -0
  20. data/lib/delayed/backend/active_record.rb +297 -0
  21. data/lib/delayed/backend/base.rb +317 -0
  22. data/lib/delayed/backend/redis/bulk_update.lua +40 -0
  23. data/lib/delayed/backend/redis/destroy_job.lua +2 -0
  24. data/lib/delayed/backend/redis/enqueue.lua +29 -0
  25. data/lib/delayed/backend/redis/fail_job.lua +5 -0
  26. data/lib/delayed/backend/redis/find_available.lua +3 -0
  27. data/lib/delayed/backend/redis/functions.rb +57 -0
  28. data/lib/delayed/backend/redis/get_and_lock_next_available.lua +17 -0
  29. data/lib/delayed/backend/redis/includes/jobs_common.lua +203 -0
  30. data/lib/delayed/backend/redis/job.rb +481 -0
  31. data/lib/delayed/backend/redis/set_running.lua +5 -0
  32. data/lib/delayed/backend/redis/tickle_strand.lua +2 -0
  33. data/lib/delayed/batch.rb +56 -0
  34. data/lib/delayed/engine.rb +4 -0
  35. data/lib/delayed/job_tracking.rb +31 -0
  36. data/lib/delayed/lifecycle.rb +83 -0
  37. data/lib/delayed/message_sending.rb +130 -0
  38. data/lib/delayed/performable_method.rb +42 -0
  39. data/lib/delayed/periodic.rb +81 -0
  40. data/lib/delayed/pool.rb +335 -0
  41. data/lib/delayed/settings.rb +32 -0
  42. data/lib/delayed/version.rb +3 -0
  43. data/lib/delayed/worker.rb +213 -0
  44. data/lib/delayed/yaml_extensions.rb +63 -0
  45. data/lib/delayed_job.rb +40 -0
  46. data/spec/active_record_job_spec.rb +61 -0
  47. data/spec/gemfiles/32.gemfile +6 -0
  48. data/spec/gemfiles/40.gemfile +6 -0
  49. data/spec/gemfiles/41.gemfile +6 -0
  50. data/spec/gemfiles/42.gemfile +6 -0
  51. data/spec/migrate/20140924140513_add_story_table.rb +7 -0
  52. data/spec/redis_job_spec.rb +77 -0
  53. data/spec/sample_jobs.rb +26 -0
  54. data/spec/shared/delayed_batch.rb +85 -0
  55. data/spec/shared/delayed_method.rb +419 -0
  56. data/spec/shared/performable_method.rb +52 -0
  57. data/spec/shared/shared_backend.rb +836 -0
  58. data/spec/shared/worker.rb +291 -0
  59. data/spec/shared_jobs_specs.rb +13 -0
  60. data/spec/spec_helper.rb +91 -0
  61. metadata +329 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 727a0655ca96cd696cca764d7cc6abbf4386fbb9
4
+ data.tar.gz: f85f4112dd1f605ff176b250d8cc92c330fe721b
5
+ SHA512:
6
+ metadata.gz: bcd41802f4198d324e7f3ca295f21bcb0a1fc9f1f7bff25490b0c8f156feedd50b7eac05d510bc778831584ac235d99c72b357381d42be7a04a56968a583f2ea
7
+ data.tar.gz: a4254c78e7dc10866675584f954aa08a3f729951e4f20d425cfea3a0e3e205323fb58d8cd1ac36c9d7a3f70665410941b6a3b157091d2fe12c57f9ee093ff913
@@ -0,0 +1,40 @@
1
+ class CreateDelayedJobs < ActiveRecord::Migration
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.up
7
+ raise("#{connection.adapter_name} is not supported for delayed jobs queue") unless connection.adapter_name == 'PostgreSQL'
8
+
9
+ create_table :delayed_jobs do |table|
10
+ # Allows some jobs to jump to the front of the queue
11
+ table.integer :priority, :default => 0
12
+ # Provides for retries, but still fail eventually.
13
+ table.integer :attempts, :default => 0
14
+ # YAML-encoded string of the object that will do work
15
+ table.text :handler, :limit => (500 * 1024)
16
+ # reason for last failure (See Note below)
17
+ table.text :last_error
18
+ # The queue that this job is in
19
+ table.string :queue, :default => nil
20
+ # When to run.
21
+ # Could be Time.zone.now for immediately, or sometime in the future.
22
+ table.datetime :run_at
23
+ # Set when a client is working on this object
24
+ table.datetime :locked_at
25
+ # Set when all retries have failed
26
+ table.datetime :failed_at
27
+ # Who is working on this object (if locked)
28
+ table.string :locked_by
29
+
30
+ table.timestamps
31
+ end
32
+
33
+ add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
34
+ add_index :delayed_jobs, [:queue], :name => 'delayed_jobs_queue'
35
+ end
36
+
37
+ def self.down
38
+ drop_table :delayed_jobs
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ class AddDelayedJobsTag < ActiveRecord::Migration
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.up
7
+ add_column :delayed_jobs, :tag, :string
8
+ add_index :delayed_jobs, [:tag]
9
+ end
10
+
11
+ def self.down
12
+ remove_column :delayed_jobs, :tag
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ class AddDelayedJobsMaxAttempts < ActiveRecord::Migration
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.up
7
+ add_column :delayed_jobs, :max_attempts, :integer
8
+ end
9
+
10
+ def self.down
11
+ remove_column :delayed_jobs, :max_attempts
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ class AddDelayedJobsStrand < ActiveRecord::Migration
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.up
7
+ add_column :delayed_jobs, :strand, :string
8
+ add_index :delayed_jobs, :strand
9
+ end
10
+
11
+ def self.down
12
+ remove_column :delayed_jobs, :strand
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ class CleanupDelayedJobsIndexes < ActiveRecord::Migration
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.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_jobs (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 self.down
22
+ remove_index :delayed_jobs, :name => 'get_delayed_jobs_index'
23
+ add_index :delayed_jobs, [: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
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.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 => 512000
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 self.down
38
+ raise ActiveRecord::IrreversibleMigration
39
+ end
40
+ end
@@ -0,0 +1,52 @@
1
+ class AddDelayedJobsNextInStrand < ActiveRecord::Migration
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.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_jobs (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 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;
25
+ CODE
26
+ execute("CREATE TRIGGER delayed_jobs_before_insert_row_tr BEFORE INSERT ON delayed_jobs FOR EACH ROW WHEN (NEW.strand IS NOT NULL) EXECUTE PROCEDURE delayed_jobs_before_insert_row_tr_fn()")
27
+
28
+ # create the delete trigger
29
+ execute(<<-CODE)
30
+ CREATE FUNCTION 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;
36
+ CODE
37
+ execute("CREATE TRIGGER delayed_jobs_after_delete_row_tr AFTER DELETE ON delayed_jobs FOR EACH ROW WHEN (OLD.strand IS NOT NULL AND OLD.next_in_strand = 't') EXECUTE PROCEDURE delayed_jobs_after_delete_row_tr_fn()")
38
+
39
+ execute(%{UPDATE delayed_jobs SET next_in_strand = 'f' WHERE strand IS NOT NULL AND id <> (SELECT id FROM delayed_jobs j2 WHERE j2.strand = delayed_jobs.strand ORDER BY j2.strand, j2.id ASC LIMIT 1)})
40
+ end
41
+
42
+ def self.down
43
+ execute %{DROP TRIGGER delayed_jobs_before_insert_row_tr ON delayed_jobs}
44
+ execute %{DROP FUNCTION delayed_jobs_before_insert_row_tr_fn()}
45
+ execute %{DROP TRIGGER delayed_jobs_after_delete_row_tr ON delayed_jobs}
46
+ execute %{DROP FUNCTION 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
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.up
7
+ if connection.adapter_name == 'PostgreSQL'
8
+ execute(<<-CODE)
9
+ CREATE OR REPLACE FUNCTION 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;
15
+ CODE
16
+ end
17
+ end
18
+
19
+ def self.down
20
+ if connection.adapter_name == 'PostgreSQL'
21
+ execute(<<-CODE)
22
+ CREATE OR REPLACE FUNCTION 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;
28
+ CODE
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ class DropPsqlJobsPopFn < ActiveRecord::Migration
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.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 self.down
13
+ raise ActiveRecord::IrreversibleMigration
14
+ end
15
+ end
@@ -0,0 +1,80 @@
1
+ class DelayedJobsUseAdvisoryLocks < ActiveRecord::Migration
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.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 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;
28
+ CODE
29
+
30
+ execute(<<-CODE)
31
+ CREATE OR REPLACE FUNCTION 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;
40
+ CODE
41
+
42
+ execute(<<-CODE)
43
+ CREATE OR REPLACE FUNCTION 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;
50
+ CODE
51
+ end
52
+ end
53
+
54
+ def self.down
55
+ if connection.adapter_name == 'PostgreSQL'
56
+ execute(<<-CODE)
57
+ CREATE OR REPLACE FUNCTION 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;
66
+ CODE
67
+
68
+ execute(<<-CODE)
69
+ CREATE OR REPLACE FUNCTION 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;
75
+ CODE
76
+
77
+ execute('DROP FUNCTION half_md5_as_bigint(varchar)')
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,15 @@
1
+ class IndexJobsOnLockedBy < ActiveRecord::Migration
2
+ disable_ddl_transaction! if respond_to?(:disable_ddl_transaction!)
3
+
4
+ def self.connection
5
+ Delayed::Backend::ActiveRecord::Job.connection
6
+ end
7
+
8
+ def self.up
9
+ add_index :delayed_jobs, :locked_by, :algorithm => :concurrently, :where => "locked_by IS NOT NULL"
10
+ end
11
+
12
+ def self.down
13
+ remove_index :delayed_jobs, :locked_by
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ class AddJobsRunAtIndex < ActiveRecord::Migration
2
+ disable_ddl_transaction! if respond_to?(:disable_ddl_transaction!)
3
+
4
+ def self.connection
5
+ Delayed::Backend::ActiveRecord::Job.connection
6
+ end
7
+
8
+ def self.up
9
+ add_index :delayed_jobs, %w[run_at tag], :algorithm => :concurrently
10
+ end
11
+
12
+ def self.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
2
+ def self.connection
3
+ Delayed::Job.connection
4
+ end
5
+
6
+ def self.up
7
+ change_column :delayed_jobs, :handler, :text
8
+ end
9
+
10
+ def self.down
11
+ change_column :delayed_jobs, :handler, :string, :limit => 500.kilobytes
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class AddFailedJobsOriginalJobId < ActiveRecord::Migration
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.up
7
+ add_column :failed_jobs, :original_job_id, :integer, limit: 8
8
+ end
9
+
10
+ def self.down
11
+ remove_column :failed_jobs, :original_job_id
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class CopyFailedJobsOriginalId < ActiveRecord::Migration
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.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 self.down
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ class DropFailedJobsOriginalId < ActiveRecord::Migration
2
+ def self.connection
3
+ Delayed::Backend::ActiveRecord::Job.connection
4
+ end
5
+
6
+ def self.up
7
+ remove_column :failed_jobs, :original_id
8
+ end
9
+
10
+ def self.down
11
+ add_column :failed_jobs, :original_id, :integer, limit: 8
12
+ end
13
+ end