simple_worker 0.3.14 → 0.3.15

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -103,3 +103,22 @@ Setup:
103
103
 
104
104
  Now you can use your workers like their part of your app!
105
105
 
106
+
107
+ Configuration Options
108
+ ---------------------
109
+
110
+ ### Global Attributes
111
+
112
+ These are attributes that can be set as part of your config block then will be set on
113
+ all your worker objects automatically. This is particularly good for things like database
114
+ connection info or things that you would need to use across the board.
115
+
116
+ Eg:
117
+
118
+ config.global_attributes[:db_user] = "sa"
119
+ config.global_attributes[:db_pass] = "pass"
120
+
121
+ Then in your worker, you must have:
122
+
123
+ attr_accessor :db_user, :db_pass
124
+
@@ -1,181 +1,211 @@
1
- # This is an abstract module that developers creating works can mixin/include to use the SimpleWorker special functions.
2
-
3
- require 'digest/md5'
4
-
5
- module SimpleWorker
6
-
7
- class Base
8
-
9
- attr_accessor :task_set_id, :task_id, :schedule_id
10
-
11
- class << self
12
- attr_accessor :subclass, :caller_file
13
- @merged = []
14
- @merged_workers = []
15
-
16
- def reset!
17
- @merged = []
18
- @merged_workers = []
19
- end
20
-
21
- def inherited(subclass)
22
- subclass.reset!
23
-
24
- # puts "subclass.inspect=" + subclass.inspect
25
- # puts 'existing caller=' + (subclass.instance_variable_defined?(:@caller_file) ? subclass.instance_variable_get(:@caller_file).inspect : "nil")
26
- # puts "caller=" + caller.inspect
27
- # splits = caller[0].split(":")
28
- # caller_file = splits[0] + ":" + splits[1]
29
- caller_file = caller[0][0...(caller[0].index(":in"))]
30
- caller_file = caller_file[0...(caller_file.rindex(":"))]
31
- # puts 'caller_file=' + caller_file
32
- # don't need these class_variables anymore probably
33
- subclass.instance_variable_set(:@caller_file, caller_file)
34
-
35
- super
36
- end
37
-
38
- # merges the specified files.
39
- def merge(*files)
40
- files.each do |f|
41
- unless File.exist? f
42
- raise "File not found: " + f
43
- end
44
- require f
45
- @merged << File.expand_path(f)
46
- end
47
- end
48
-
49
- def merge_worker(file, class_name)
50
- puts 'merge_worker in ' + self.name
51
- merge(file)
52
- @merged_workers << [File.expand_path(file), class_name]
53
- end
54
- end
55
-
56
-
57
- def log(str)
58
- puts str.to_s
59
- end
60
-
61
- def user_dir
62
- "."
63
- end
64
-
65
- def set_progress(hash)
66
- puts 'set_progress: ' + hash.inspect
67
- end
68
-
69
- def who_am_i?
70
- return self.class.name
71
- end
72
-
73
- def uploaded?
74
- self.class.instance_variable_defined?(:@uploaded) && self.class.instance_variable_get(:@uploaded)
75
- end
76
-
77
-
78
- # Will send in all instance_variables.
79
- def queue
80
- puts 'in queue'
81
- upload_if_needed
82
-
83
- response = SimpleWorker.service.queue(self.class.name, sw_get_data)
84
- puts 'queue response=' + response.inspect
85
- @task_set_id = response["task_set_id"]
86
- @task_id = response["tasks"][0]["task_id"]
87
- response
88
- end
89
-
90
- def status
91
- SimpleWorker.service.status(task_id)
92
- end
93
-
94
- def upload
95
- upload_if_needed
96
- end
97
-
98
- def schedule(schedule)
99
- upload_if_needed
100
-
101
- response = SimpleWorker.service.schedule(self.class.name, sw_get_data, schedule)
102
- puts 'schedule response=' + response.inspect
103
- @schedule_id = response["schedule_id"]
104
- response
105
- end
106
-
107
- def schedule_status
108
- SimpleWorker.service.schedule_status(schedule_id)
109
- end
110
-
111
- # Callbacks for developer
112
- def before_upload
113
-
114
- end
115
-
116
- def after_upload
117
-
118
- end
119
-
120
- def before_run
121
-
122
- end
123
-
124
- def after_run
125
-
126
- end
127
-
128
- private
129
-
130
- def upload_if_needed
131
-
132
- before_upload
133
-
134
- puts 'upload_if_needed'
135
- # Todo, watch for this file changing or something so we can reupload
136
- unless uploaded?
137
- subclass = self.class
138
- rfile = subclass.instance_variable_get(:@caller_file) # Base.caller_file # File.expand_path(Base.subclass)
139
- puts 'rfile=' + rfile.inspect
140
- puts 'self.class.name=' + subclass.name
141
- merged = self.class.instance_variable_get(:@merged)
142
- puts 'merged1=' + merged.inspect
143
- superclass = subclass
144
- # Also get merged from subclasses up to SimpleWorker::Base
145
- while (superclass = superclass.superclass)
146
- puts 'superclass=' + superclass.name
147
- break if superclass.name == SimpleWorker::Base.name
148
- super_merged = superclass.instance_variable_get(:@merged)
149
- # puts 'merging caller file: ' + superclass.instance_variable_get(:@caller_file).inspect
150
- super_merged << superclass.instance_variable_get(:@caller_file)
151
- merged = super_merged + merged
152
- puts 'merged with superclass=' + merged.inspect
153
-
154
- end
155
- SimpleWorker.service.upload(rfile, subclass.name, :merge=>merged)
156
- self.class.instance_variable_set(:@uploaded, true)
157
- else
158
- puts 'already uploaded for ' + self.class.name
159
- end
160
- puts 'uploading merged workers'
161
- self.class.instance_variable_get(:@merged_workers).each do |mw|
162
- # to support merges in the secondary worker, we should instantiate it here, then call "upload"
163
- puts 'instantiating and uploading ' + mw[1]
164
- Kernel.const_get(mw[1]).new.upload
165
- # SimpleWorker.service.upload(mw[0], mw[1])
166
- end
167
-
168
- after_upload
169
- end
170
-
171
- def sw_get_data
172
- data = {}
173
- self.instance_variables.each do |iv|
174
- data[iv] = instance_variable_get(iv)
175
- end
176
- return data
177
- end
178
-
179
-
180
- end
181
- end
1
+ # This is an abstract module that developers creating works can mixin/include to use the SimpleWorker special functions.
2
+
3
+ require 'digest/md5'
4
+
5
+ module SimpleWorker
6
+
7
+ class Base
8
+
9
+ attr_accessor :task_set_id, :task_id, :schedule_id
10
+
11
+ class << self
12
+ attr_accessor :subclass, :caller_file
13
+ @merged = []
14
+ @merged_workers = []
15
+
16
+ def reset!
17
+ @merged = []
18
+ @merged_workers = []
19
+ end
20
+
21
+ def inherited(subclass)
22
+ subclass.reset!
23
+
24
+ # puts "subclass.inspect=" + subclass.inspect
25
+ # puts 'existing caller=' + (subclass.instance_variable_defined?(:@caller_file) ? subclass.instance_variable_get(:@caller_file).inspect : "nil")
26
+ # puts "caller=" + caller.inspect
27
+ # splits = caller[0].split(":")
28
+ # caller_file = splits[0] + ":" + splits[1]
29
+ caller_file = caller[0][0...(caller[0].index(":in"))]
30
+ caller_file = caller_file[0...(caller_file.rindex(":"))]
31
+ # puts 'caller_file=' + caller_file
32
+ # don't need these class_variables anymore probably
33
+ subclass.instance_variable_set(:@caller_file, caller_file)
34
+
35
+ super
36
+ end
37
+
38
+ # merges the specified files.
39
+ def merge(*files)
40
+ files.each do |f|
41
+ unless File.exist? f
42
+ raise "File not found: " + f
43
+ end
44
+ require f
45
+ @merged << File.expand_path(f)
46
+ end
47
+ end
48
+
49
+ def merge_worker(file, class_name)
50
+ puts 'merge_worker in ' + self.name
51
+ merge(file)
52
+ @merged_workers << [File.expand_path(file), class_name]
53
+ end
54
+ end
55
+
56
+
57
+ def log(str)
58
+ puts str.to_s
59
+ end
60
+
61
+ def user_dir
62
+ "."
63
+ end
64
+
65
+ def set_progress(hash)
66
+ puts 'set_progress: ' + hash.inspect
67
+ end
68
+
69
+ def who_am_i?
70
+ return self.class.name
71
+ end
72
+
73
+ def uploaded?
74
+ self.class.instance_variable_defined?(:@uploaded) && self.class.instance_variable_get(:@uploaded)
75
+ end
76
+
77
+ # Call this if you want to run locally and get some extra features from this gem like global attributes.
78
+ def run_local
79
+ # puts 'run_local'
80
+ set_global_attributes
81
+ run
82
+ end
83
+
84
+ def set_global_attributes
85
+ ga = SimpleWorker.config.global_attributes
86
+ if ga && ga.size > 0
87
+ ga.each_pair do |k,v|
88
+ # puts "k=#{k} v=#{v}"
89
+ if self.respond_to?(k)
90
+ self.send("#{k}=", v)
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ # Will send in all instance_variables.
97
+ def queue
98
+ # puts 'in queue'
99
+ set_global_attributes
100
+ upload_if_needed
101
+
102
+ response = SimpleWorker.service.queue(self.class.name, sw_get_data)
103
+ # puts 'queue response=' + response.inspect
104
+ @task_set_id = response["task_set_id"]
105
+ @task_id = response["tasks"][0]["task_id"]
106
+ response
107
+ end
108
+
109
+ def status
110
+ SimpleWorker.service.status(task_id)
111
+ end
112
+
113
+ def upload
114
+ upload_if_needed
115
+ end
116
+
117
+ #
118
+ # schedule: hash of scheduling options that can include:
119
+ # Required:
120
+ # - start_at: Time of first run - DateTime or Time object.
121
+ # Optional:
122
+ # - run_every: Time in seconds between runs. If ommitted, task will only run once.
123
+ # - delay_type: Fixed Rate or Fixed Delay. Default is fixed_delay.
124
+ # - end_at: Scheduled task will stop running after this date (optional, if ommitted, runs forever or until cancelled)
125
+ # - run_times: Task will run exactly :run_times. For instance if :run_times is 5, then the task will run 5 times.
126
+ #
127
+ def schedule(schedule)
128
+ set_global_attributes
129
+ upload_if_needed
130
+
131
+ response = SimpleWorker.service.schedule(self.class.name, sw_get_data, schedule)
132
+ # puts 'schedule response=' + response.inspect
133
+ @schedule_id = response["schedule_id"]
134
+ response
135
+ end
136
+
137
+ def schedule_status
138
+ SimpleWorker.service.schedule_status(schedule_id)
139
+ end
140
+
141
+ # Callbacks for developer
142
+ def before_upload
143
+
144
+ end
145
+
146
+ def after_upload
147
+
148
+ end
149
+
150
+ def before_run
151
+
152
+ end
153
+
154
+ def after_run
155
+
156
+ end
157
+
158
+ private
159
+
160
+ def upload_if_needed
161
+
162
+ before_upload
163
+
164
+ puts 'upload_if_needed'
165
+ # Todo, watch for this file changing or something so we can reupload
166
+ unless uploaded?
167
+ subclass = self.class
168
+ rfile = subclass.instance_variable_get(:@caller_file) # Base.caller_file # File.expand_path(Base.subclass)
169
+ puts 'rfile=' + rfile.inspect
170
+ puts 'self.class.name=' + subclass.name
171
+ merged = self.class.instance_variable_get(:@merged)
172
+ puts 'merged1=' + merged.inspect
173
+ superclass = subclass
174
+ # Also get merged from subclasses up to SimpleWorker::Base
175
+ while (superclass = superclass.superclass)
176
+ puts 'superclass=' + superclass.name
177
+ break if superclass.name == SimpleWorker::Base.name
178
+ super_merged = superclass.instance_variable_get(:@merged)
179
+ # puts 'merging caller file: ' + superclass.instance_variable_get(:@caller_file).inspect
180
+ super_merged << superclass.instance_variable_get(:@caller_file)
181
+ merged = super_merged + merged
182
+ puts 'merged with superclass=' + merged.inspect
183
+
184
+ end
185
+ SimpleWorker.service.upload(rfile, subclass.name, :merge=>merged)
186
+ self.class.instance_variable_set(:@uploaded, true)
187
+ else
188
+ puts 'already uploaded for ' + self.class.name
189
+ end
190
+ puts 'uploading merged workers'
191
+ self.class.instance_variable_get(:@merged_workers).each do |mw|
192
+ # to support merges in the secondary worker, we should instantiate it here, then call "upload"
193
+ puts 'instantiating and uploading ' + mw[1]
194
+ Kernel.const_get(mw[1]).new.upload
195
+ # SimpleWorker.service.upload(mw[0], mw[1])
196
+ end
197
+
198
+ after_upload
199
+ end
200
+
201
+ def sw_get_data
202
+ data = {}
203
+ self.instance_variables.each do |iv|
204
+ data[iv] = instance_variable_get(iv)
205
+ end
206
+ return data
207
+ end
208
+
209
+
210
+ end
211
+ end
@@ -1,8 +1,20 @@
1
1
  module SimpleWorker
2
+
3
+
4
+ # Config is used to setup the SimpleWorker client.
5
+ # You must set the access_key and secret_key.
6
+ #
7
+ # config.global_attributes allows you to specify attributes that will automatically be set on every worker,
8
+ # this is good for database connection information or things that will be used across the board.
2
9
  class Config
3
10
  attr_accessor :access_key,
4
11
  :secret_key,
5
- :host
12
+ :host,
13
+ :global_attributes
14
+
15
+ def initialize
16
+ @global_attributes = {}
17
+ end
6
18
  end
7
19
 
8
20
  end
data/test/test_base.rb CHANGED
@@ -26,6 +26,9 @@ class TestBase < Test::Unit::TestCase
26
26
  config.access_key = @access_key
27
27
  config.secret_key = @secret_key
28
28
  config.host = "http://localhost:3000/api/"
29
+ config.global_attributes["db_user"] = "sa"
30
+ config.global_attributes["db_pass"] = "pass"
31
+
29
32
  end
30
33
  end
31
34
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- require 'test_base'
3
+ require_relative 'test_base'
4
4
 
5
5
  class SimpleWorkerTests < TestBase
6
6
 
@@ -38,6 +38,18 @@ class SimpleWorkerTests < TestBase
38
38
  assert tw.status["status"] == "complete"
39
39
 
40
40
  end
41
+
42
+ def test_global_attributes
43
+ worker = TestWorker3.new
44
+ worker.run_local
45
+
46
+ puts 'worker=' + worker.inspect
47
+
48
+ assert_equal "sa", worker.db_user
49
+ assert_equal "pass", worker.db_pass
50
+ assert_equal 123, worker.x
51
+
52
+ end
41
53
  #
42
54
  #
43
55
  # def test_queue
@@ -8,8 +8,8 @@ end
8
8
  # Bump for new checksum.sdf
9
9
  class TestWorker2 < SimpleWorker::Base
10
10
 
11
- merge 'models/model_1.rb'
12
- merge_worker 'second_worker.rb', 'SecondWorker'
11
+ merge File.join(File.dirname(__FILE__), 'models', 'model_1.rb')
12
+ merge_worker File.join(File.dirname(__FILE__), 'second_worker.rb'), 'SecondWorker'
13
13
 
14
14
  attr_accessor :s3_key, :times, :x
15
15
 
@@ -4,15 +4,16 @@ rescue Exception => ex
4
4
  puts ex.message
5
5
  require 'simple_worker'
6
6
  end
7
- require 'test_worker_2'
7
+ require_relative 'test_worker_2'
8
8
 
9
9
  class TestWorker3 < TestWorker2
10
10
 
11
11
 
12
- attr_accessor :s3_key, :times
12
+ attr_accessor :x, :db_user, :db_pass
13
13
 
14
14
  def run()
15
-
15
+ puts 'TestWorker3.run'
16
+ @x = 123
16
17
  end
17
18
 
18
19
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 14
9
- version: 0.3.14
8
+ - 15
9
+ version: 0.3.15
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: 2010-10-26 00:00:00 -07:00
17
+ date: 2010-11-18 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -58,8 +58,8 @@ homepage: http://github.com/appoxy/simple_worker
58
58
  licenses: []
59
59
 
60
60
  post_install_message:
61
- rdoc_options:
62
- - --charset=UTF-8
61
+ rdoc_options: []
62
+
63
63
  require_paths:
64
64
  - lib
65
65
  required_ruby_version: !ruby/object:Gem::Requirement