delayed_job_ironmq 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
File without changes
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ This is [IronMQ](http://www.iron.io/products/mq) backend for [delayed_job](http://github.com/collectiveidea/delayed_job)
2
+
3
+ # Getting Started
4
+
5
+ ## Get credentials
6
+
7
+ To start using delayed_job_ironmq, you need to sign up and get an oauth token.
8
+
9
+ 1. Go to http://iron.io/ and sign up.
10
+ 2. Get an Oauth Token at http://hud.iron.io/tokens
11
+
12
+ ## Installation
13
+
14
+ Add the gems to your `Gemfile:`
15
+
16
+ ```ruby
17
+ gem 'delayed_job'
18
+ gem 'delayed_job_ironmq'
19
+ ```
20
+
21
+ And add an initializer (`config/initializers/delayed_job_config.rb`):
22
+
23
+ ```ruby
24
+ Delayed::Worker.configure do |config|
25
+ config.token = 'XXXXXXXXXXXXXXXX'
26
+ config.project_id = 'XXXXXXXXXXXXXXXX'
27
+
28
+ # optional params:
29
+ config.available_priorities = [-1,0,1,2] # Default is [0]. Please note, each new priority will speed down a bit picking job from queue
30
+ config.queue_name = 'default' # Specify an alternative queue name
31
+ config.delay = 0 # Time to wait before message will be available on the queue
32
+ config.timeout = 5.minutes # The time in seconds to wait after message is taken off the queue, before it is put back on. Delete before :timeout to ensure it does not go back on the queue.
33
+ config.expires_in = 7.days # After this time, message will be automatically removed from the queue.
34
+ end
35
+ end
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ That's it. Use [delayed_job as normal](http://github.com/collectiveidea/delayed_job).
41
+
42
+ Example:
43
+
44
+ ```ruby
45
+ class TestJob < Struct.new(:a, :b)
46
+ def perform
47
+ puts a/b
48
+ end
49
+ end
50
+ Delayed::Job.enqueue TestJob.new(10, 2)
51
+
52
+ ```
53
+
54
+ # Documentation
55
+
56
+ You can find more documentation here:
57
+
58
+ * http://iron.io
59
+ * http://dev.iron.io
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,75 @@
1
+ module Delayed
2
+ module Backend
3
+ module Ironmq
4
+ module Actions
5
+ def field(name, options = {})
6
+ #type = options[:type] || String
7
+ default = options[:default] || nil
8
+ define_method name do
9
+ @attributes ||= {}
10
+ @attributes[name.to_sym] || default
11
+ end
12
+ define_method "#{name}=" do |value|
13
+ @attributes ||= {}
14
+ @attributes[name.to_sym] = value
15
+ end
16
+ end
17
+
18
+ def before_fork
19
+ end
20
+
21
+ def after_fork
22
+ end
23
+
24
+ def db_time_now
25
+ Time.now.utc
26
+ end
27
+
28
+ #def self.queue_name
29
+ # Delayed::Worker.queue_name
30
+ #end
31
+
32
+ def find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
33
+ Delayed::Worker.available_priorities.each do |priority|
34
+ messages = ironmq.messages.get(queue_name: queue_name(priority), n: limit)
35
+ jobs = messages.map do |message|
36
+ Delayed::Backend::Ironmq::Job.new(message)
37
+ end
38
+ return jobs if jobs.present?
39
+ end
40
+ []
41
+ end
42
+
43
+ def delete_all
44
+ deleted = 0
45
+ Delayed::Worker.available_priorities.each do |priority|
46
+ loop do
47
+ msgs = ironmq.messages.get(n: 1000, queue_name: queue_name(priority))
48
+ break if msgs.blank?
49
+ msgs.each do |msg|
50
+ ironmq.messages.delete(msg.id, queue_name: queue_name(priority))
51
+ deleted += 1
52
+ end
53
+ end
54
+ end
55
+ puts "Messages removed: #{deleted}"
56
+ end
57
+
58
+ # No need to check locks
59
+ def clear_locks!(*args)
60
+ true
61
+ end
62
+
63
+ private
64
+
65
+ def ironmq
66
+ ::Delayed::Worker.ironmq
67
+ end
68
+
69
+ def queue_name(priority)
70
+ "#{Delayed::Worker.queue_name}_#{priority || 0}"
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ class IronMqConfig
2
+ attr_accessor :token, :project_id,
3
+ :queue_name, :delay, :timeout,
4
+ :expires_in, :available_priorities
5
+ end
@@ -0,0 +1,123 @@
1
+
2
+
3
+ module Delayed
4
+ module Backend
5
+ module Ironmq
6
+ class Job
7
+ include ::DelayedJobIronmq::Document
8
+ include Delayed::Backend::Base
9
+ extend Delayed::Backend::Ironmq::Actions
10
+
11
+ field :priority, :type => Integer, :default => 0
12
+ field :attempts, :type => Integer, :default => 0
13
+ field :handler, :type => String
14
+ field :run_at, :type => Time
15
+ field :locked_at, :type => Time
16
+ field :locked_by, :type => String
17
+ field :failed_at, :type => Time
18
+ field :last_error, :type => String
19
+ field :queue, :type => String
20
+
21
+ def initialize(data = {})
22
+ puts "[init] Delayed::Backend::Ironmq"
23
+ @id = nil
24
+ if data.is_a?(IronMQ::Message)
25
+ @id = data.id
26
+ data = JSON.load(data.body)
27
+ end
28
+
29
+ data.symbolize_keys!
30
+ payload_obj = data.delete(:payload_object) || data.delete(:handler)
31
+
32
+ @queue_name = data[:queue_name] || Delayed::Worker.queue_name
33
+ @delay = data[:delay] || Delayed::Worker.delay
34
+ @timeout = data[:timeout] || Delayed::Worker.timeout
35
+ @expires_in = data[:expires_in] || Delayed::Worker.expires_in
36
+ @attributes = data
37
+ self.payload_object = payload_obj
38
+ end
39
+
40
+ def payload_object
41
+ @payload_object ||= YAML.load(self.handler)
42
+ rescue TypeError, LoadError, NameError, ArgumentError => e
43
+ raise DeserializationError,
44
+ "Job failed to load: #{e.message}. Handler: #{handler.inspect}"
45
+ end
46
+
47
+ def payload_object=(object)
48
+ if object.is_a? String
49
+ @payload_object = YAML.load(object)
50
+ self.handler = object
51
+ else
52
+ @payload_object = object
53
+ self.handler = object.to_yaml
54
+ end
55
+ end
56
+
57
+ def save
58
+ puts "[SAVE] #{@attributes.inspect}"
59
+
60
+ if @attributes[:handler].blank?
61
+ raise "Handler missing!"
62
+ end
63
+ payload = JSON.dump(@attributes)
64
+
65
+ ironmq.messages.delete(@id, queue_name: queue_name) if @id.present?
66
+ ironmq.messages.post(payload,
67
+ timeout: @timeout,
68
+ queue_name: queue_name,
69
+ delay: @delay,
70
+ expires_in: @expires_in)
71
+ true
72
+ end
73
+
74
+ def save!
75
+ save
76
+ end
77
+
78
+ def destroy
79
+ if @id
80
+ puts "job destroyed! #{@id.inspect}"
81
+ ironmq.messages.delete(@id, queue_name: queue_name) if @id.present?
82
+ end
83
+ end
84
+
85
+ def fail!
86
+ destroy
87
+ # v2: move to separate queue
88
+ end
89
+
90
+ def update_attributes(attributes)
91
+ attributes.symbolize_keys!
92
+ @attributes.merge attributes
93
+ save
94
+ end
95
+
96
+ # No need to check locks
97
+ def lock_exclusively!(*args)
98
+ true
99
+ end
100
+
101
+ # No need to check locks
102
+ def unlock(*args)
103
+ true
104
+ end
105
+
106
+ def reload(*args)
107
+ # reset
108
+ super
109
+ end
110
+
111
+ private
112
+
113
+ def queue_name
114
+ "#{@queue_name}_#{@attributes[:priority] || 0}"
115
+ end
116
+
117
+ def ironmq
118
+ ::Delayed::Worker.ironmq
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'iron_mq_config'
2
+
3
+ module Delayed
4
+ class Worker
5
+ class << self
6
+ attr_accessor :config, :ironmq,
7
+ :queue_name, :delay, :timeout, :expires_in, :available_priorities
8
+
9
+ def configure
10
+ yield(config)
11
+ if config && config.token && config.project_id
12
+ Delayed::Worker.ironmq = IronMQ::Client.new(
13
+ 'token' => config.token,
14
+ 'project_id' => config.project_id
15
+ )
16
+ self.queue_name = config.queue_name || 'default'
17
+ self.delay = config.delay || 0
18
+ self.timeout = config.timeout || 5.minutes
19
+ self.expires_in = config.expires_in || 7.days
20
+
21
+ priorities = config.available_priorities || [0]
22
+ if priorities.include?(0) && priorities.all?{|p|p.is_a?(Integer)}
23
+ self.available_priorities = priorities.sort
24
+ else
25
+ raise ArgumentError, "available_priorities option has wrong format. Please provide array of Integer values, includes zero. Default is [0]."
26
+ end
27
+ else
28
+ raise ArgumentError, "Required option missing. Please provide both 'token' and 'project_id'"
29
+ end
30
+ end
31
+
32
+ def config
33
+ @config ||= IronMqConfig.new
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+
3
+ module DelayedJobIronmq
4
+ module Document
5
+ end
6
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+ require 'iron_mq'
3
+ require 'delayed_job'
4
+
5
+ require_relative 'delayed/serialization/ironmq'
6
+ require_relative 'delayed/backend/actions'
7
+ require_relative 'delayed/backend/iron_mq_config'
8
+ require_relative 'delayed/backend/worker'
9
+ require_relative 'delayed/backend/ironmq'
10
+
11
+ Delayed::Worker.backend = :ironmq
data/spec/.gitkeep ADDED
File without changes
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed_job_ironmq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexander Shapiotko
9
+ - Iron.io, Inc
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-03-06 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: iron_mq
17
+ requirement: &36672804 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 1.4.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *36672804
26
+ - !ruby/object:Gem::Dependency
27
+ name: delayed_job
28
+ requirement: &36672300 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *36672300
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &36672000 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.0.0
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *36672000
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: &36671688 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '2.0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *36671688
59
+ - !ruby/object:Gem::Dependency
60
+ name: jeweler
61
+ requirement: &36671388 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 1.8.3
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *36671388
70
+ description: IronMQ backend for delayed_job
71
+ email: info@iron.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - LICENSE
76
+ - README.md
77
+ files:
78
+ - LICENSE
79
+ - README.md
80
+ - VERSION
81
+ - lib/delayed/backend/actions.rb
82
+ - lib/delayed/backend/iron_mq_config.rb
83
+ - lib/delayed/backend/ironmq.rb
84
+ - lib/delayed/backend/worker.rb
85
+ - lib/delayed/serialization/ironmq.rb
86
+ - lib/delayed_job_ironmq.rb
87
+ - spec/.gitkeep
88
+ homepage: http://www.iron.io
89
+ licenses: []
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ segments:
101
+ - 0
102
+ hash: -931014419
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.11
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: IronMQ backend for delayed_job
115
+ test_files: []