governor_background 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Gemfile.lock +2 -1
- data/README.rdoc +40 -30
- data/VERSION +1 -1
- data/governor_background.gemspec +8 -2
- data/lib/governor_background.rb +40 -11
- data/lib/governor_background/controllers/methods.rb +1 -1
- data/lib/governor_background/delayed/job.rb +20 -6
- data/lib/governor_background/delayed/performer.rb +2 -10
- data/lib/governor_background/handler.rb +10 -7
- data/lib/governor_background/job_manager.rb +8 -0
- data/lib/governor_background/resque/job.rb +20 -8
- data/lib/governor_background/resque/performer.rb +6 -7
- data/lib/governor_background/resque/performer_with_state.rb +5 -9
- data/lib/governor_background/resque/resource.rb +26 -0
- data/spec/governor_background/delayed/job_spec.rb +2 -2
- data/spec/governor_background/handler_spec.rb +7 -13
- data/spec/governor_background/resque/resource_spec.rb +16 -0
- data/spec/governor_background_spec.rb +9 -3
- data/spec/rails_app/Gemfile +0 -3
- data/spec/rails_app/Gemfile.lock +1 -1
- metadata +37 -20
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: ./
|
3
3
|
specs:
|
4
|
-
governor_background (0.
|
4
|
+
governor_background (0.3.0)
|
5
5
|
governor
|
6
6
|
|
7
7
|
GEM
|
@@ -154,6 +154,7 @@ DEPENDENCIES
|
|
154
154
|
governor
|
155
155
|
governor_background!
|
156
156
|
jeweler (~> 1.5.2)
|
157
|
+
json
|
157
158
|
mocha
|
158
159
|
resque
|
159
160
|
resque-status
|
data/README.rdoc
CHANGED
@@ -5,14 +5,15 @@ Blagojevich) is the pluggable blogging platform for Rails, built for people
|
|
5
5
|
who want to build their blog into their website, not build their website into
|
6
6
|
their blog.
|
7
7
|
|
8
|
-
*governor_background*
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
*governor_background* plays nicely with Governor, allowing you to tie in
|
9
|
+
additional services that might want to perform blog-related services in the
|
10
|
+
background. For example, you may want to tweet a link to your article when
|
11
|
+
publishing.
|
12
12
|
|
13
13
|
== Dependencies
|
14
14
|
|
15
|
-
* Governor[http://carpeliam.github.com/governor/]
|
15
|
+
* Governor[http://carpeliam.github.com/governor/], although this dependency
|
16
|
+
might be removed. GovernorBackground is pretty independent.
|
16
17
|
* Either Delayed_Job[https://github.com/collectiveidea/delayed_job] or
|
17
18
|
Resque[https://github.com/defunkt/resque]. If you use Resque, it's highly
|
18
19
|
recommended that you use
|
@@ -47,31 +48,40 @@ class, and within that div will be one or more divs with either a 'completed',
|
|
47
48
|
|
48
49
|
== Usage
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
this
|
61
|
-
2.
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
51
|
+
Background jobs happen in two stages in governor_background: first, they get
|
52
|
+
*registered*, then they get *run*.
|
53
|
+
|
54
|
+
1. *register*: To register a job, you give it a name and call
|
55
|
+
<code>GovernorBackground.register</code>:
|
56
|
+
|
57
|
+
GovernorBackground.register('twitter_post') do |content|
|
58
|
+
Twitter.update(content)
|
59
|
+
end
|
60
|
+
|
61
|
+
You'll want to do this once when the application is first started.
|
62
|
+
2. *run*: When you're ready to run your background task, call
|
63
|
+
<code>GovernorBackground.run</code>:
|
64
|
+
|
65
|
+
GovernorBackground.run('twitter_post', 'I am so awesome')
|
66
|
+
|
67
|
+
GovernorBackground will look for a task registered as +twitter_post+ and
|
68
|
+
run its associated block, including any arguments you passed to +run+.
|
69
|
+
|
70
|
+
Job names have to be _globally unique_; a good practice can be to preface your
|
71
|
+
job name with the name of your plugin and an underscore.
|
72
|
+
|
73
|
+
In your I18n settings, add an entry for your job name for each finished status
|
74
|
+
(completed, failed, and killed). You'll have access in your entry to any
|
75
|
+
message passed from the background service. For the above example, you might
|
76
|
+
have the following in your <code>config/locales/en.yml</code> file:
|
77
|
+
|
78
|
+
en:
|
79
|
+
governor_background:
|
80
|
+
twitter_post_completed: 'Your article was successfully posted to Twitter.'
|
81
|
+
twitter_post_failed: 'Unable to tweet your article: %{message}.'
|
82
|
+
twitter_post_killed: 'Your tweet was interrupted: %{message}.'
|
83
|
+
|
84
|
+
Please see governor_twitter[https://github.com/carpeliam/governor_twitter] as
|
75
85
|
an example usage of *governor_background*.
|
76
86
|
|
77
87
|
== Contributing to governor_background
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/governor_background.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{governor_background}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Liam Morley"]
|
12
|
-
s.date = %q{2011-05-
|
12
|
+
s.date = %q{2011-05-15}
|
13
13
|
s.description = %q{A middle-tier plugin for the Rails 3-based Governor blogging system, allowing you to tie in additional services that might want to perform blog-related services in the background.}
|
14
14
|
s.email = %q{liam@carpeliam.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -37,11 +37,13 @@ Gem::Specification.new do |s|
|
|
37
37
|
"lib/governor_background/resque/job.rb",
|
38
38
|
"lib/governor_background/resque/performer.rb",
|
39
39
|
"lib/governor_background/resque/performer_with_state.rb",
|
40
|
+
"lib/governor_background/resque/resource.rb",
|
40
41
|
"spec/governor_background/delayed/job_spec.rb",
|
41
42
|
"spec/governor_background/handler_spec.rb",
|
42
43
|
"spec/governor_background/job_manager_spec.rb",
|
43
44
|
"spec/governor_background/resque/performer_spec.rb",
|
44
45
|
"spec/governor_background/resque/performer_with_state_spec.rb",
|
46
|
+
"spec/governor_background/resque/resource_spec.rb",
|
45
47
|
"spec/governor_background_spec.rb",
|
46
48
|
"spec/rails_app/.gitignore",
|
47
49
|
"spec/rails_app/Gemfile",
|
@@ -114,6 +116,7 @@ Gem::Specification.new do |s|
|
|
114
116
|
"spec/governor_background/job_manager_spec.rb",
|
115
117
|
"spec/governor_background/resque/performer_spec.rb",
|
116
118
|
"spec/governor_background/resque/performer_with_state_spec.rb",
|
119
|
+
"spec/governor_background/resque/resource_spec.rb",
|
117
120
|
"spec/governor_background_spec.rb",
|
118
121
|
"spec/rails_app/app/controllers/application_controller.rb",
|
119
122
|
"spec/rails_app/app/controllers/home_controller.rb",
|
@@ -158,6 +161,7 @@ Gem::Specification.new do |s|
|
|
158
161
|
s.add_development_dependency(%q<factory_girl>, ["~> 2.0.0.beta"])
|
159
162
|
s.add_development_dependency(%q<factory_girl_rails>, ["~> 1.1.beta"])
|
160
163
|
s.add_development_dependency(%q<activerecord-nulldb-adapter>, [">= 0"])
|
164
|
+
s.add_development_dependency(%q<json>, [">= 0"])
|
161
165
|
s.add_development_dependency(%q<will_paginate>, ["~> 3.0.beta"])
|
162
166
|
s.add_development_dependency(%q<devise>, [">= 0"])
|
163
167
|
s.add_development_dependency(%q<governor_background>, [">= 0"])
|
@@ -175,6 +179,7 @@ Gem::Specification.new do |s|
|
|
175
179
|
s.add_dependency(%q<factory_girl>, ["~> 2.0.0.beta"])
|
176
180
|
s.add_dependency(%q<factory_girl_rails>, ["~> 1.1.beta"])
|
177
181
|
s.add_dependency(%q<activerecord-nulldb-adapter>, [">= 0"])
|
182
|
+
s.add_dependency(%q<json>, [">= 0"])
|
178
183
|
s.add_dependency(%q<will_paginate>, ["~> 3.0.beta"])
|
179
184
|
s.add_dependency(%q<devise>, [">= 0"])
|
180
185
|
s.add_dependency(%q<governor_background>, [">= 0"])
|
@@ -193,6 +198,7 @@ Gem::Specification.new do |s|
|
|
193
198
|
s.add_dependency(%q<factory_girl>, ["~> 2.0.0.beta"])
|
194
199
|
s.add_dependency(%q<factory_girl_rails>, ["~> 1.1.beta"])
|
195
200
|
s.add_dependency(%q<activerecord-nulldb-adapter>, [">= 0"])
|
201
|
+
s.add_dependency(%q<json>, [">= 0"])
|
196
202
|
s.add_dependency(%q<will_paginate>, ["~> 3.0.beta"])
|
197
203
|
s.add_dependency(%q<devise>, [">= 0"])
|
198
204
|
s.add_dependency(%q<governor_background>, [">= 0"])
|
data/lib/governor_background.rb
CHANGED
@@ -5,18 +5,47 @@ require 'governor_background/delayed/job'
|
|
5
5
|
require 'governor_background/delayed/performer'
|
6
6
|
require 'governor_background/resque/job'
|
7
7
|
require 'governor_background/resque/performer'
|
8
|
+
require 'governor_background/resque/resource'
|
8
9
|
require 'governor_background/controllers/methods'
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
private
|
15
|
-
def run_in_background(method, *arguments)
|
16
|
-
GovernorBackground::Handler.run_in_background self, method, *arguments
|
17
|
-
end
|
18
|
-
end
|
19
|
-
base.send :include, InstanceMethods
|
11
|
+
begin
|
12
|
+
require 'governor_background/resque/performer_with_state'
|
13
|
+
rescue LoadError
|
14
|
+
$stderr.puts 'resque-status gem not installed, GovernorBackground resque jobs unable to report status'
|
20
15
|
end
|
21
16
|
|
22
|
-
|
17
|
+
module GovernorBackground
|
18
|
+
@@blocks = {}
|
19
|
+
# Registers this job to be run later. Must be called upon application
|
20
|
+
# initialization.
|
21
|
+
#
|
22
|
+
# Example:
|
23
|
+
#
|
24
|
+
# GovernorBackground.register('twitter_post') do |content|
|
25
|
+
# Twitter.update(content)
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# Job names must be globally unique. It's recommended that you preface the
|
29
|
+
# job name with the name of the containing plugin.
|
30
|
+
def self.register(job_name, &block)
|
31
|
+
@@blocks[job_name.to_s] = block
|
32
|
+
end
|
33
|
+
|
34
|
+
# Runs the job with the supplied arguments.
|
35
|
+
#
|
36
|
+
# Example:
|
37
|
+
#
|
38
|
+
# GovernorBackground.run('twitter_post', 'I am so awesome')
|
39
|
+
#
|
40
|
+
# +job_name+ refers to the identifier for a previously registered job.
|
41
|
+
def self.run(job_name, *arguments)
|
42
|
+
GovernorBackground::Handler.run_in_background job_name, *arguments
|
43
|
+
end
|
44
|
+
|
45
|
+
# Retrieves the block for a +job_name+, which must have been previously
|
46
|
+
# registered. This will return a Proc object if the job is found, or +nil+
|
47
|
+
# if no job was registered under this name.
|
48
|
+
def self.retrieve(job_name)
|
49
|
+
@@blocks[job_name.to_s]
|
50
|
+
end
|
51
|
+
end
|
@@ -9,7 +9,7 @@ module GovernorBackground
|
|
9
9
|
finished_jobs = GovernorBackground::JobManager.finished_jobs
|
10
10
|
unless finished_jobs.blank?
|
11
11
|
flash[:governor_background] = finished_jobs.map do |job|
|
12
|
-
[job.status, t("#{job.
|
12
|
+
[job.status, t("#{job.name}_#{job.status}", :message => job.message, :default => [job.status, job.status.to_sym], :scope => :governor_background)]
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
@@ -1,44 +1,58 @@
|
|
1
1
|
module GovernorBackground
|
2
2
|
module Delayed
|
3
|
+
# A wrapper around ::Delayed::Job.
|
3
4
|
class Job
|
4
|
-
attr_reader :
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
attr_reader :name, :created_at
|
6
|
+
# Initializes the job with a job name (passed from
|
7
|
+
# <code>GovernorBackground.run</code>) and the original delayed_job job.
|
8
|
+
def initialize(name, job)
|
9
|
+
@name = name
|
8
10
|
@id = job.try(:id)
|
9
11
|
@created_at = Time.now
|
10
12
|
end
|
11
13
|
|
14
|
+
# Returns true if this job is currently waiting in the queue, false
|
15
|
+
# otherwise.
|
12
16
|
def queued?
|
13
17
|
job_queued?
|
14
18
|
end
|
15
19
|
|
20
|
+
# Returns true if this job is currently being processed, false
|
21
|
+
# otherwise.
|
16
22
|
def working?
|
17
23
|
job_working?
|
18
24
|
end
|
19
25
|
|
26
|
+
# Returns true if this job has been completed, false otherwise.
|
20
27
|
def completed?
|
21
28
|
job_completed?
|
22
29
|
end
|
23
30
|
|
31
|
+
# Returns true if this job has failed, false otherwise.
|
24
32
|
def failed?
|
25
33
|
job_failed?
|
26
34
|
end
|
27
35
|
|
36
|
+
# Always returns false; as far as I know, you can't tell if a job has
|
37
|
+
# been killed.
|
28
38
|
def killed?
|
29
|
-
# as far as i know, you can't tell if a job has been killed
|
30
39
|
false
|
31
40
|
end
|
32
41
|
|
42
|
+
# Returns the status of the job, which can be any of the boolean methods
|
43
|
+
# or +unknown+.
|
33
44
|
def status
|
34
45
|
job = delayed_job
|
35
46
|
return 'queued' if job_queued?(job)
|
36
47
|
return 'working' if job_working?(job)
|
37
48
|
return 'completed' if job_completed?(job)
|
38
49
|
return 'failed' if job_failed?(job)
|
39
|
-
|
50
|
+
return 'unknown'
|
40
51
|
end
|
41
52
|
|
53
|
+
# Returns a human readable message describing the status. If the job has
|
54
|
+
# failed, this will include the error message, otherwise the status
|
55
|
+
# itself will be returned.
|
42
56
|
def message
|
43
57
|
(job = delayed_job) && job.failed? ?
|
44
58
|
job.last_error.lines.first.chomp.gsub(/^\{/, '') :
|
@@ -1,16 +1,8 @@
|
|
1
1
|
module GovernorBackground
|
2
2
|
module Delayed
|
3
|
-
class Performer < Struct.new(:
|
3
|
+
class Performer < Struct.new(:job_name, :arguments)
|
4
4
|
def perform
|
5
|
-
|
6
|
-
article.send(method_name)
|
7
|
-
else
|
8
|
-
article.send(method_name, *arguments)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def error(job, exception)
|
13
|
-
# handle failure
|
5
|
+
GovernorBackground.retrieve(job_name).call(*arguments)
|
14
6
|
end
|
15
7
|
end
|
16
8
|
end
|
@@ -1,20 +1,23 @@
|
|
1
1
|
module GovernorBackground
|
2
2
|
class Handler
|
3
3
|
class << self
|
4
|
-
|
4
|
+
# Handles incoming jobs, routes them to Delayed_Job or Resque (whichever
|
5
|
+
# one is installed within the app), and adds them to the +JobManager+.
|
6
|
+
def run_in_background(job_name, *arguments)
|
5
7
|
job = if delayed_job?
|
6
|
-
Delayed::Job.new(
|
8
|
+
Delayed::Job.new(job_name, ::Delayed::Job.enqueue(Delayed::Performer.new(job_name, arguments)))
|
7
9
|
elsif resque?
|
8
|
-
|
10
|
+
resque_args = arguments.map do |arg|
|
11
|
+
arg.is_a?(ActiveRecord::Base) ? Resque::Resource.new(arg) : arg
|
12
|
+
end
|
9
13
|
if resque_with_status?
|
10
|
-
|
11
|
-
Resque::Job.new(object, method, Resque::PerformerWithState.create(:resource => resource_key, :id => id, :method_name => method, :arguments => arguments))
|
14
|
+
Resque::Job.new(job_name, Resque::PerformerWithState.create(:job_name => job_name, :arguments => resque_args))
|
12
15
|
else
|
13
|
-
::Resque.enqueue(Resque::Performer,
|
16
|
+
::Resque.enqueue(Resque::Performer, job_name, *resque_args)
|
14
17
|
nil # not much use in holding on to state if we can't track it
|
15
18
|
end
|
16
19
|
end
|
17
|
-
JobManager.add(job)
|
20
|
+
JobManager.add(job) if job
|
18
21
|
end
|
19
22
|
|
20
23
|
private
|
@@ -1,17 +1,25 @@
|
|
1
1
|
module GovernorBackground
|
2
|
+
# Tracks the progress of jobs, enables reporting of finished jobs, and
|
3
|
+
# cleans stale jobs.
|
2
4
|
class JobManager
|
3
5
|
@@finished_statuses = %w(completed failed killed).freeze
|
4
6
|
cattr_reader :jobs
|
5
7
|
class << self
|
6
8
|
@@jobs = []
|
9
|
+
# Adds a job to the queue.
|
7
10
|
def add(job)
|
8
11
|
@@jobs << job
|
9
12
|
end
|
10
13
|
|
14
|
+
# Purges any jobs that are older than the specified time. This only
|
15
|
+
# removes the job wrapper from memory, it does not cancel a job if it is
|
16
|
+
# still running.
|
11
17
|
def clean(time = 1.day.ago)
|
12
18
|
@@jobs.reject!{|j| j.created_at < time}
|
13
19
|
end
|
14
20
|
|
21
|
+
# Returns and purges any jobs that have either been completed, failed,
|
22
|
+
# or killed.
|
15
23
|
def finished_jobs
|
16
24
|
finished_jobs = @@jobs.select{|j| @@finished_statuses.include? j.status }
|
17
25
|
@@jobs -= finished_jobs
|
@@ -1,46 +1,58 @@
|
|
1
|
-
module
|
1
|
+
module GovernorBackground
|
2
2
|
module Resque
|
3
|
+
# A wrapper around a Resque job.
|
3
4
|
class Job
|
4
|
-
attr_reader :
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
attr_reader :name, :created_at
|
6
|
+
# Initializes the job with a job name (passed from
|
7
|
+
# <code>GovernorBackground.run</code>) and a unique identifier for the
|
8
|
+
# Resque job.
|
9
|
+
def initialize(name, job_id)
|
10
|
+
@name = name
|
8
11
|
@id = job_id
|
9
12
|
@created_at = Time.now
|
10
13
|
end
|
11
14
|
|
15
|
+
# Returns the status of the job, which can be any of the boolean methods
|
16
|
+
# or +unknown+.
|
12
17
|
def status
|
13
|
-
|
14
|
-
(job = resque_status) ? job.status : 'killed'
|
18
|
+
(job = resque_status) ? job.status : 'unknown'
|
15
19
|
end
|
16
20
|
|
21
|
+
# Returns true if this job is currently waiting in the queue, false
|
22
|
+
# otherwise.
|
17
23
|
def queued?
|
18
24
|
proxy :queued?
|
19
25
|
end
|
20
26
|
|
27
|
+
# Returns true if this job is currently being processed, false
|
28
|
+
# otherwise.
|
21
29
|
def working?
|
22
30
|
proxy :working?
|
23
31
|
end
|
24
32
|
|
33
|
+
# Returns true if this job has been completed, false otherwise.
|
25
34
|
def completed?
|
26
35
|
proxy :completed?
|
27
36
|
end
|
28
37
|
|
38
|
+
# Returns true if this job has failed, false otherwise.
|
29
39
|
def failed?
|
30
40
|
proxy :failed?
|
31
41
|
end
|
32
42
|
|
43
|
+
# Returns true if this job has been killed, false otherwise.
|
33
44
|
def killed?
|
34
45
|
proxy :killed?
|
35
46
|
end
|
36
47
|
|
48
|
+
# Returns a human readable message describing the status.
|
37
49
|
def message
|
38
50
|
proxy :message, ''
|
39
51
|
end
|
40
52
|
|
41
53
|
private
|
42
54
|
def resque_status
|
43
|
-
Resque::Status.get(@id)
|
55
|
+
::Resque::Status.get(@id)
|
44
56
|
end
|
45
57
|
|
46
58
|
def proxy(method, default=false)
|
@@ -1,15 +1,14 @@
|
|
1
1
|
module GovernorBackground
|
2
2
|
module Resque
|
3
|
+
# Used when the resque-status gem hasn't been installed. If resque-status
|
4
|
+
# hasn't been installed, jobs won't be added to the JobManager, as there
|
5
|
+
# won't be any way to track the state, and so progress will not be
|
6
|
+
# reported.
|
3
7
|
class Performer
|
4
8
|
@queue = :governor
|
5
9
|
|
6
|
-
def self.perform(
|
7
|
-
|
8
|
-
if arguments.blank?
|
9
|
-
article.send(method_name)
|
10
|
-
else
|
11
|
-
article.send(method_name, arguments)
|
12
|
-
end
|
10
|
+
def self.perform(job_name, *arguments)
|
11
|
+
GovernorBackground.retrieve(job_name).call(*arguments)
|
13
12
|
end
|
14
13
|
end
|
15
14
|
end
|
@@ -1,20 +1,16 @@
|
|
1
|
+
require 'resque/job_with_status'
|
1
2
|
module GovernorBackground
|
2
3
|
module Resque
|
4
|
+
# Used when the resque-status gem has been installed.
|
3
5
|
class PerformerWithState < ::Resque::JobWithStatus
|
4
6
|
def self.queue
|
5
7
|
:governor
|
6
8
|
end
|
7
9
|
|
8
10
|
def perform
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
article = Governor.resources[resource].to.find(id)
|
13
|
-
if options.has_key?('arguments') && options['arguments'].present?
|
14
|
-
article.send(method_name, arguments)
|
15
|
-
else
|
16
|
-
article.send(method_name)
|
17
|
-
end
|
11
|
+
job_name = options['job_name']
|
12
|
+
arguments = options['arguments']
|
13
|
+
GovernorBackground.retrieve(job_name).call(*arguments)
|
18
14
|
end
|
19
15
|
end
|
20
16
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module GovernorBackground
|
2
|
+
module Resque
|
3
|
+
# Provides a way for ActiveRecord objects to be serialized to Resque.
|
4
|
+
# ActiveRecord objects can't be sent directly to Resque, as Resque
|
5
|
+
# serializes everything to JSON. +Resource+ provides an intermediate step.
|
6
|
+
# Resources are serialized to a JSON representation of the resource type
|
7
|
+
# (as registered in Governor) and the ID. When resources are deserialized,
|
8
|
+
# they are automatically converted into the ActiveRecord objects they
|
9
|
+
# represent.
|
10
|
+
class Resource
|
11
|
+
attr_reader :resource_key, :id
|
12
|
+
def initialize(ar_resource)
|
13
|
+
@resource_key = ar_resource.class.name.tableize
|
14
|
+
@id = ar_resource.id
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_hash
|
18
|
+
self.instance_values.merge('json_class' => self.class.name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.json_create(o)
|
22
|
+
Governor.resources[o['resource_key'].to_sym].to.find(o['id'])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -18,7 +18,7 @@ module GovernorBackground
|
|
18
18
|
|
19
19
|
it "has a status of completed if it can't be found" do
|
20
20
|
::Delayed::Job.expects(:find_by_id).with(article.id).at_least_once.returns nil
|
21
|
-
job = Job.new(
|
21
|
+
job = Job.new(:job_name, stub(:id => article.id))
|
22
22
|
job.should be_completed
|
23
23
|
job.status.should == 'completed'
|
24
24
|
end
|
@@ -40,7 +40,7 @@ module GovernorBackground
|
|
40
40
|
private
|
41
41
|
def get_job_for(delayed_job)
|
42
42
|
::Delayed::Job.expects(:find_by_id).with(delayed_job.id).at_least_once.returns delayed_job
|
43
|
-
Job.new(
|
43
|
+
Job.new(:job_name, delayed_job)
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
@@ -14,13 +14,7 @@ module GovernorBackground
|
|
14
14
|
|
15
15
|
module Resque
|
16
16
|
class Job
|
17
|
-
attr_reader :
|
18
|
-
def initialize(resource, method_name, job_id)
|
19
|
-
@resource = resource
|
20
|
-
@method_name = method_name
|
21
|
-
@id = job_id
|
22
|
-
@created_at = Time.now
|
23
|
-
end
|
17
|
+
attr_reader :name, :id, :created_at # make ID accessible for testing
|
24
18
|
end
|
25
19
|
end
|
26
20
|
|
@@ -33,12 +27,12 @@ module GovernorBackground
|
|
33
27
|
context "delayed_job" do
|
34
28
|
it "adds jobs successfully" do
|
35
29
|
expect {
|
36
|
-
Handler.run_in_background(
|
30
|
+
Handler.run_in_background(:job_name)
|
37
31
|
}.to change { ::Delayed::Job.count }.by 1
|
38
32
|
end
|
39
33
|
|
40
34
|
it "handles any number of arguments" do
|
41
|
-
Handler.run_in_background(
|
35
|
+
Handler.run_in_background(:job_name, 1, 2, 3)
|
42
36
|
performer = YAML.load ::Delayed::Job.first.handler
|
43
37
|
performer.arguments.should == [1, 2, 3]
|
44
38
|
end
|
@@ -47,16 +41,16 @@ module GovernorBackground
|
|
47
41
|
context "resque" do
|
48
42
|
it "adds jobs successfully" do
|
49
43
|
Handler.use_resque = true
|
50
|
-
Handler.run_in_background(
|
44
|
+
Handler.run_in_background(:job_name)
|
51
45
|
id = JobManager.jobs.first.id
|
52
|
-
Resque::PerformerWithState.should have_queued(id, {:
|
46
|
+
Resque::PerformerWithState.should have_queued(id, {:job_name => :job_name, :arguments => []}).in(:governor)
|
53
47
|
end
|
54
48
|
|
55
49
|
it "can accept any number of arguments" do
|
56
50
|
Handler.use_resque = true
|
57
|
-
Handler.run_in_background(
|
51
|
+
Handler.run_in_background(:job_name, 1, 2, 3)
|
58
52
|
id = JobManager.jobs.first.id
|
59
|
-
Resque::PerformerWithState.should have_queued(id, {:
|
53
|
+
Resque::PerformerWithState.should have_queued(id, {:job_name => :job_name, :arguments => [1, 2, 3]}).in(:governor)
|
60
54
|
end
|
61
55
|
end
|
62
56
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module GovernorBackground
|
4
|
+
module Resque
|
5
|
+
describe Resource do
|
6
|
+
before do
|
7
|
+
@article = Factory(:article, :author => Factory(:user))
|
8
|
+
@resource = Resource.new(@article)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "deserializing from JSON returns original article" do
|
12
|
+
JSON.parse(@resource.to_json).should == @article
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -2,8 +2,14 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe GovernorBackground do
|
4
4
|
it "passes arguments to the handler" do
|
5
|
-
|
6
|
-
GovernorBackground
|
7
|
-
|
5
|
+
GovernorBackground::Handler.expects(:run_in_background).with(:job_name, 1, 2, 3)
|
6
|
+
GovernorBackground.run(:job_name, 1, 2, 3)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "registers jobs" do
|
10
|
+
block = Proc.new do |a, b, c|
|
11
|
+
end
|
12
|
+
GovernorBackground.register(:job_name, &block)
|
13
|
+
GovernorBackground.retrieve(:job_name).should == block
|
8
14
|
end
|
9
15
|
end
|
data/spec/rails_app/Gemfile
CHANGED
data/spec/rails_app/Gemfile.lock
CHANGED
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Liam Morley
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05-
|
18
|
+
date: 2011-05-15 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -140,37 +140,37 @@ dependencies:
|
|
140
140
|
- !ruby/object:Gem::Dependency
|
141
141
|
type: :development
|
142
142
|
prerelease: false
|
143
|
-
name:
|
143
|
+
name: json
|
144
144
|
version_requirements: &id009 !ruby/object:Gem::Requirement
|
145
145
|
none: false
|
146
146
|
requirements:
|
147
|
-
- -
|
147
|
+
- - ">="
|
148
148
|
- !ruby/object:Gem::Version
|
149
|
-
hash:
|
149
|
+
hash: 3
|
150
150
|
segments:
|
151
|
-
- 3
|
152
151
|
- 0
|
153
|
-
|
154
|
-
version: 3.0.beta
|
152
|
+
version: "0"
|
155
153
|
requirement: *id009
|
156
154
|
- !ruby/object:Gem::Dependency
|
157
155
|
type: :development
|
158
156
|
prerelease: false
|
159
|
-
name:
|
157
|
+
name: will_paginate
|
160
158
|
version_requirements: &id010 !ruby/object:Gem::Requirement
|
161
159
|
none: false
|
162
160
|
requirements:
|
163
|
-
- -
|
161
|
+
- - ~>
|
164
162
|
- !ruby/object:Gem::Version
|
165
|
-
hash:
|
163
|
+
hash: 31098121
|
166
164
|
segments:
|
165
|
+
- 3
|
167
166
|
- 0
|
168
|
-
|
167
|
+
- beta
|
168
|
+
version: 3.0.beta
|
169
169
|
requirement: *id010
|
170
170
|
- !ruby/object:Gem::Dependency
|
171
171
|
type: :development
|
172
172
|
prerelease: false
|
173
|
-
name:
|
173
|
+
name: devise
|
174
174
|
version_requirements: &id011 !ruby/object:Gem::Requirement
|
175
175
|
none: false
|
176
176
|
requirements:
|
@@ -184,7 +184,7 @@ dependencies:
|
|
184
184
|
- !ruby/object:Gem::Dependency
|
185
185
|
type: :development
|
186
186
|
prerelease: false
|
187
|
-
name:
|
187
|
+
name: governor_background
|
188
188
|
version_requirements: &id012 !ruby/object:Gem::Requirement
|
189
189
|
none: false
|
190
190
|
requirements:
|
@@ -198,7 +198,7 @@ dependencies:
|
|
198
198
|
- !ruby/object:Gem::Dependency
|
199
199
|
type: :development
|
200
200
|
prerelease: false
|
201
|
-
name:
|
201
|
+
name: delayed_job
|
202
202
|
version_requirements: &id013 !ruby/object:Gem::Requirement
|
203
203
|
none: false
|
204
204
|
requirements:
|
@@ -212,7 +212,7 @@ dependencies:
|
|
212
212
|
- !ruby/object:Gem::Dependency
|
213
213
|
type: :development
|
214
214
|
prerelease: false
|
215
|
-
name: resque
|
215
|
+
name: resque
|
216
216
|
version_requirements: &id014 !ruby/object:Gem::Requirement
|
217
217
|
none: false
|
218
218
|
requirements:
|
@@ -226,7 +226,7 @@ dependencies:
|
|
226
226
|
- !ruby/object:Gem::Dependency
|
227
227
|
type: :development
|
228
228
|
prerelease: false
|
229
|
-
name:
|
229
|
+
name: resque-status
|
230
230
|
version_requirements: &id015 !ruby/object:Gem::Requirement
|
231
231
|
none: false
|
232
232
|
requirements:
|
@@ -240,7 +240,7 @@ dependencies:
|
|
240
240
|
- !ruby/object:Gem::Dependency
|
241
241
|
type: :development
|
242
242
|
prerelease: false
|
243
|
-
name:
|
243
|
+
name: resque_spec
|
244
244
|
version_requirements: &id016 !ruby/object:Gem::Requirement
|
245
245
|
none: false
|
246
246
|
requirements:
|
@@ -251,6 +251,20 @@ dependencies:
|
|
251
251
|
- 0
|
252
252
|
version: "0"
|
253
253
|
requirement: *id016
|
254
|
+
- !ruby/object:Gem::Dependency
|
255
|
+
type: :development
|
256
|
+
prerelease: false
|
257
|
+
name: dynamic_form
|
258
|
+
version_requirements: &id017 !ruby/object:Gem::Requirement
|
259
|
+
none: false
|
260
|
+
requirements:
|
261
|
+
- - ">="
|
262
|
+
- !ruby/object:Gem::Version
|
263
|
+
hash: 3
|
264
|
+
segments:
|
265
|
+
- 0
|
266
|
+
version: "0"
|
267
|
+
requirement: *id017
|
254
268
|
description: A middle-tier plugin for the Rails 3-based Governor blogging system, allowing you to tie in additional services that might want to perform blog-related services in the background.
|
255
269
|
email: liam@carpeliam.com
|
256
270
|
executables: []
|
@@ -281,11 +295,13 @@ files:
|
|
281
295
|
- lib/governor_background/resque/job.rb
|
282
296
|
- lib/governor_background/resque/performer.rb
|
283
297
|
- lib/governor_background/resque/performer_with_state.rb
|
298
|
+
- lib/governor_background/resque/resource.rb
|
284
299
|
- spec/governor_background/delayed/job_spec.rb
|
285
300
|
- spec/governor_background/handler_spec.rb
|
286
301
|
- spec/governor_background/job_manager_spec.rb
|
287
302
|
- spec/governor_background/resque/performer_spec.rb
|
288
303
|
- spec/governor_background/resque/performer_with_state_spec.rb
|
304
|
+
- spec/governor_background/resque/resource_spec.rb
|
289
305
|
- spec/governor_background_spec.rb
|
290
306
|
- spec/rails_app/.gitignore
|
291
307
|
- spec/rails_app/Gemfile
|
@@ -386,6 +402,7 @@ test_files:
|
|
386
402
|
- spec/governor_background/job_manager_spec.rb
|
387
403
|
- spec/governor_background/resque/performer_spec.rb
|
388
404
|
- spec/governor_background/resque/performer_with_state_spec.rb
|
405
|
+
- spec/governor_background/resque/resource_spec.rb
|
389
406
|
- spec/governor_background_spec.rb
|
390
407
|
- spec/rails_app/app/controllers/application_controller.rb
|
391
408
|
- spec/rails_app/app/controllers/home_controller.rb
|