simple_worker 0.3.19 → 0.3.20
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +22 -10
- data/lib/railtie.rb +1 -1
- data/lib/simple_worker.rb +4 -0
- data/lib/simple_worker/base.rb +59 -37
- data/lib/simple_worker/config.rb +3 -3
- data/lib/simple_worker/service.rb +12 -2
- data/test/models/model_2.rb +22 -0
- data/test/test_worker_2.rb +1 -0
- metadata +5 -3
data/README.markdown
CHANGED
@@ -45,10 +45,12 @@ Let's say someone does something in your app and you want to send an email about
|
|
45
45
|
worker.to = current_user.email
|
46
46
|
worker.subject = "Here is your mail!"
|
47
47
|
worker.body = "This is the body"
|
48
|
-
|
48
|
+
worker.run_local
|
49
49
|
|
50
|
-
|
51
|
-
|
50
|
+
Once you've got it working locally, the next step is to run it on the SimpleWorker cloud.
|
51
|
+
|
52
|
+
Queue up your Worker on the SimpleWorker Cloud
|
53
|
+
----------------------------------------------
|
52
54
|
|
53
55
|
Let's say someone does something in your app and you want to send an email about it.
|
54
56
|
|
@@ -56,7 +58,9 @@ Let's say someone does something in your app and you want to send an email about
|
|
56
58
|
worker.to = current_user.email
|
57
59
|
worker.subject = "Here is your mail!"
|
58
60
|
worker.body = "This is the body"
|
59
|
-
|
61
|
+
worker.queue
|
62
|
+
|
63
|
+
This will send it off to the SimpleWorker cloud.
|
60
64
|
|
61
65
|
Schedule your Worker
|
62
66
|
--------------------
|
@@ -68,7 +72,7 @@ action in your application. This is almost the same as queuing your worker.
|
|
68
72
|
worker.to = current_user.email
|
69
73
|
worker.subject = "Here is your mail!"
|
70
74
|
worker.body = "This is the body"
|
71
|
-
|
75
|
+
worker.schedule(:start_at=>1.hours.since)
|
72
76
|
|
73
77
|
Check Status
|
74
78
|
------------
|
@@ -79,7 +83,7 @@ If you still have access to the worker object, just call:
|
|
79
83
|
|
80
84
|
If you only have the job ID, call:
|
81
85
|
|
82
|
-
SimpleWorker.status(job_id)
|
86
|
+
SimpleWorker.service.status(job_id)
|
83
87
|
|
84
88
|
This will return a hash like:
|
85
89
|
|
@@ -91,22 +95,27 @@ This will return a hash like:
|
|
91
95
|
"duration"=>nil,
|
92
96
|
"progress"=>{"percent"=>25}}
|
93
97
|
|
94
|
-
TODO: How to access log.
|
95
98
|
|
96
99
|
Logging
|
97
100
|
-------
|
98
101
|
|
102
|
+
In your worker, just call the log method with the string you want logged:
|
103
|
+
|
99
104
|
log "Starting to do something..."
|
100
105
|
|
101
|
-
The log will be available for viewing via the SimpleWorker UI or via log in the API
|
106
|
+
The log will be available for viewing via the SimpleWorker UI or via log in the API:
|
107
|
+
|
108
|
+
SimpleWorker.service.log(job_id)
|
102
109
|
|
103
110
|
Setting Progress
|
104
111
|
----------------
|
105
112
|
|
106
|
-
|
113
|
+
This is just a way to let your users know where the job is at if required.
|
107
114
|
|
108
|
-
|
115
|
+
set_progress(:percent => 25, :message => "We are a quarter of the way there!")
|
109
116
|
|
117
|
+
You can actually put anything in this hash and it will be returned with a call to status. We recommend using
|
118
|
+
the format above for consistency and to get some additional features where we look for these values.
|
110
119
|
|
111
120
|
Schedule a Recurring Job - CRON
|
112
121
|
------------------------------
|
@@ -178,6 +187,9 @@ Or simpler yet, try using relative paths:
|
|
178
187
|
merge "../app/models/user"
|
179
188
|
merge "../app/models/account.rb"
|
180
189
|
|
190
|
+
The opposite can be done as well with "unmerge" and can be useful when using Rails to exclude classes that are automatically
|
191
|
+
merged.
|
192
|
+
|
181
193
|
|
182
194
|
Bringing in other Workers
|
183
195
|
---------------------
|
data/lib/railtie.rb
CHANGED
data/lib/simple_worker.rb
CHANGED
data/lib/simple_worker/base.rb
CHANGED
@@ -12,10 +12,12 @@ module SimpleWorker
|
|
12
12
|
attr_accessor :subclass, :caller_file
|
13
13
|
@merged = []
|
14
14
|
@merged_workers = []
|
15
|
+
@unmerged = []
|
15
16
|
|
16
17
|
def reset!
|
17
18
|
@merged = []
|
18
19
|
@merged_workers = []
|
20
|
+
@unmerged = []
|
19
21
|
end
|
20
22
|
|
21
23
|
def inherited(subclass)
|
@@ -35,30 +37,47 @@ module SimpleWorker
|
|
35
37
|
super
|
36
38
|
end
|
37
39
|
|
40
|
+
def check_for_file(f)
|
41
|
+
f = f.to_str
|
42
|
+
unless ends_with?(f, ".rb")
|
43
|
+
f << ".rb"
|
44
|
+
end
|
45
|
+
exists = false
|
46
|
+
if File.exist? f
|
47
|
+
exists = true
|
48
|
+
else
|
49
|
+
# try relative
|
50
|
+
# p caller
|
51
|
+
f2 = File.join(File.dirname(caller[3]), f)
|
52
|
+
puts 'f2=' + f2
|
53
|
+
if File.exist? f2
|
54
|
+
exists = true
|
55
|
+
f = f2
|
56
|
+
end
|
57
|
+
end
|
58
|
+
unless exists
|
59
|
+
raise "File not found: " + f
|
60
|
+
end
|
61
|
+
f = File.expand_path(f)
|
62
|
+
require f
|
63
|
+
f
|
64
|
+
end
|
65
|
+
|
38
66
|
# merges the specified files.
|
39
67
|
# todo: don't allow multiple files per merge, just one like require
|
40
68
|
def merge(*files)
|
41
69
|
files.each do |f|
|
42
|
-
f = f
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
exists = true
|
54
|
-
f = f2
|
55
|
-
end
|
56
|
-
end
|
57
|
-
unless exists
|
58
|
-
raise "File not found: " + f
|
59
|
-
end
|
60
|
-
require f
|
61
|
-
@merged << File.expand_path(f)
|
70
|
+
f = check_for_file(f)
|
71
|
+
@merged << f
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Opposite of merge, this will omit the files you specify from being merged in. Useful in Rails apps
|
76
|
+
# where a lot of things are auto-merged by default like your models.
|
77
|
+
def unmerge(*files)
|
78
|
+
files.each do |f|
|
79
|
+
f = check_for_file(f)
|
80
|
+
@unmerged << f
|
62
81
|
end
|
63
82
|
end
|
64
83
|
|
@@ -80,7 +99,7 @@ module SimpleWorker
|
|
80
99
|
end
|
81
100
|
|
82
101
|
def user_dir
|
83
|
-
"
|
102
|
+
"./"
|
84
103
|
end
|
85
104
|
|
86
105
|
def set_progress(hash)
|
@@ -189,37 +208,40 @@ module SimpleWorker
|
|
189
208
|
puts 'upload_if_needed ' + self.class.name
|
190
209
|
# Todo, watch for this file changing or something so we can reupload
|
191
210
|
unless uploaded?
|
192
|
-
merged
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
puts 'subclass
|
211
|
+
merged = self.class.instance_variable_get(:@merged)
|
212
|
+
unmerged = self.class.instance_variable_get(:@unmerged)
|
213
|
+
# puts 'merged1=' + merged.inspect
|
214
|
+
|
215
|
+
subclass = self.class
|
216
|
+
rfile = subclass.instance_variable_get(:@caller_file) # Base.caller_file # File.expand_path(Base.subclass)
|
217
|
+
# puts 'subclass file=' + rfile.inspect
|
218
|
+
# puts 'subclass.name=' + subclass.name
|
199
219
|
superclass = subclass
|
200
220
|
# Also get merged from subclasses up to SimpleWorker::Base
|
201
221
|
while (superclass = superclass.superclass)
|
202
|
-
puts 'superclass=' + superclass.name
|
222
|
+
# puts 'superclass=' + superclass.name
|
203
223
|
break if superclass.name == SimpleWorker::Base.name
|
204
224
|
super_merged = superclass.instance_variable_get(:@merged)
|
205
225
|
# puts 'merging caller file: ' + superclass.instance_variable_get(:@caller_file).inspect
|
206
226
|
super_merged << superclass.instance_variable_get(:@caller_file)
|
207
227
|
merged = super_merged + merged
|
208
|
-
puts 'merged with superclass=' + merged.inspect
|
228
|
+
# puts 'merged with superclass=' + merged.inspect
|
209
229
|
end
|
210
230
|
merged += SimpleWorker.config.models if SimpleWorker.config.models
|
211
|
-
SimpleWorker.service.upload(rfile, subclass.name, :merge=>merged)
|
231
|
+
SimpleWorker.service.upload(rfile, subclass.name, :merge=>merged, :unmerge=>unmerged)
|
212
232
|
self.class.instance_variable_set(:@uploaded, true)
|
213
233
|
else
|
214
234
|
puts 'already uploaded for ' + self.class.name
|
215
235
|
end
|
216
236
|
merged_workers = self.class.instance_variable_get(:@merged_workers)
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
237
|
+
if merged_workers.size > 0
|
238
|
+
puts 'now uploading merged workers ' + merged_workers.inspect
|
239
|
+
merged_workers.each do |mw|
|
240
|
+
# to support merges in the secondary worker, we should instantiate it here, then call "upload"
|
241
|
+
puts 'instantiating and uploading ' + mw[1]
|
242
|
+
Kernel.const_get(mw[1]).new.upload
|
222
243
|
# SimpleWorker.service.upload(mw[0], mw[1])
|
244
|
+
end
|
223
245
|
end
|
224
246
|
|
225
247
|
after_upload
|
@@ -231,7 +253,7 @@ module SimpleWorker
|
|
231
253
|
data[iv] = instance_variable_get(iv)
|
232
254
|
end
|
233
255
|
|
234
|
-
config_data
|
256
|
+
config_data = SimpleWorker.config.get_atts_to_send
|
235
257
|
data[:sw_config] = config_data
|
236
258
|
return data
|
237
259
|
end
|
data/lib/simple_worker/config.rb
CHANGED
@@ -25,9 +25,9 @@ module SimpleWorker
|
|
25
25
|
|
26
26
|
def get_atts_to_send
|
27
27
|
config_data = {}
|
28
|
-
config_data['database'] = database if database
|
29
|
-
config_data['global_attributes'] = global_attributes if global_attributes
|
30
|
-
config_data['host'] = host if host
|
28
|
+
config_data['database'] = self.database if self.database
|
29
|
+
config_data['global_attributes'] = self.global_attributes if self.global_attributes
|
30
|
+
config_data['host'] = self.host if self.host
|
31
31
|
config_data
|
32
32
|
end
|
33
33
|
|
@@ -27,7 +27,7 @@ module SimpleWorker
|
|
27
27
|
existing_md5 = IO.read(f)
|
28
28
|
end
|
29
29
|
|
30
|
-
filename = build_merged_file(filename, options[:merge]) if options[:merge]
|
30
|
+
filename = build_merged_file(filename, options[:merge], options[:unmerge]) if options[:merge]
|
31
31
|
|
32
32
|
# sys.classes[subclass].__file__
|
33
33
|
# puts '__FILE__=' + Base.subclass.__file__.to_s
|
@@ -56,10 +56,20 @@ module SimpleWorker
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
def build_merged_file(filename, merge)
|
59
|
+
def build_merged_file(filename, merge, unmerge)
|
60
60
|
merge = merge.dup
|
61
61
|
merge << filename
|
62
|
+
#puts "merge before uniq! " + merge.inspect
|
62
63
|
merge.uniq!
|
64
|
+
# puts "merge after uniq! " + merge.inspect
|
65
|
+
|
66
|
+
if unmerge
|
67
|
+
unmerge.each do |x|
|
68
|
+
deleted = merge.delete x
|
69
|
+
puts "Unmerging #{x}. Success? #{deleted}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
63
73
|
fname2 = File.join(Dir.tmpdir(), File.basename(filename))
|
64
74
|
# puts 'fname2=' + fname2
|
65
75
|
# puts 'merged_file_array=' + merge.inspect
|
@@ -0,0 +1,22 @@
|
|
1
|
+
begin
|
2
|
+
require File.join(File.dirname(__FILE__), '../../lib/simple_worker')
|
3
|
+
rescue Exception => ex
|
4
|
+
puts 'ERROR!!! ' + ex.message
|
5
|
+
# require 'simple_worker'
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
class Model2
|
10
|
+
attr_accessor :heidi, :ho
|
11
|
+
|
12
|
+
include SimpleWorker::UsedInWorker
|
13
|
+
|
14
|
+
def say_hello
|
15
|
+
log "Hi there sir"
|
16
|
+
end
|
17
|
+
|
18
|
+
# testk laksdfj klasj df
|
19
|
+
def test
|
20
|
+
log 'test'
|
21
|
+
end
|
22
|
+
end
|
data/test/test_worker_2.rb
CHANGED
@@ -9,6 +9,7 @@ end
|
|
9
9
|
class TestWorker2 < SimpleWorker::Base
|
10
10
|
|
11
11
|
merge File.join(File.dirname(__FILE__), 'models', 'model_1.rb')
|
12
|
+
unmerge 'models/model_2.rb'
|
12
13
|
merge_worker File.join(File.dirname(__FILE__), 'second_worker.rb'), 'SecondWorker'
|
13
14
|
|
14
15
|
attr_accessor :s3_key, :times, :x
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 20
|
9
|
+
version: 0.3.20
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Travis Reeder
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-01-07 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- rails/init.rb
|
50
50
|
- README.markdown
|
51
51
|
- test/models/model_1.rb
|
52
|
+
- test/models/model_2.rb
|
52
53
|
- test/requiring_worker.rb
|
53
54
|
- test/scheduled_worker.rb
|
54
55
|
- test/second_worker.rb
|
@@ -92,6 +93,7 @@ specification_version: 3
|
|
92
93
|
summary: Classified
|
93
94
|
test_files:
|
94
95
|
- test/models/model_1.rb
|
96
|
+
- test/models/model_2.rb
|
95
97
|
- test/requiring_worker.rb
|
96
98
|
- test/scheduled_worker.rb
|
97
99
|
- test/second_worker.rb
|