simple_worker 0.3.11 → 0.3.13

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -34,6 +34,17 @@ Here's an example worker that sends an email:
34
34
  end
35
35
  end
36
36
 
37
+ Test It Locally
38
+ ---------------
39
+
40
+ Let's say someone does something in your app and you want to send an email about it.
41
+
42
+ worker = EmailWorker.new
43
+ worker.to = current_user.email
44
+ worker.subject = "Here is your mail!"
45
+ worker.body = "This is the body"
46
+ **worker.run**
47
+
37
48
  Queue up your Worker
38
49
  --------------------
39
50
 
@@ -43,7 +54,7 @@ Let's say someone does something in your app and you want to send an email about
43
54
  worker.to = current_user.email
44
55
  worker.subject = "Here is your mail!"
45
56
  worker.body = "This is the body"
46
- worker.queue
57
+ **worker.queue**
47
58
 
48
59
  Schedule your Worker
49
60
  --------------------
@@ -55,7 +66,12 @@ action in your application. This is almost the same as queuing your worker.
55
66
  worker.to = current_user.email
56
67
  worker.subject = "Here is your mail!"
57
68
  worker.body = "This is the body"
58
- worker.schedule(:start_at=>1.hours.since)
69
+ **worker.schedule(:start_at=>1.hours.since)**
70
+
71
+
72
+
73
+ Schedule your Worker Recurring
74
+ ------------------------------
59
75
 
60
76
  The alternative is when you want to user it like Cron. In this case you'll probably
61
77
  want to write a script that will schedule, you don't want to schedule it everytime your
@@ -71,4 +87,19 @@ Create a file called 'schedule_email_worker.rb' and add this:
71
87
  worker.body = "This is the body"
72
88
  worker.schedule(:start_at=>1.hours.since, :run_every=>3600)
73
89
 
74
- Now your worker will be scheduled to run every hour.
90
+ Now run it and your worker will be scheduled to run every hour.
91
+
92
+ SimpleWorker on Rails
93
+ ---------------------
94
+
95
+ SimpleWorker only supports Rails 3+.
96
+
97
+ Setup:
98
+
99
+ - Make a workers directory at RAILS_ROOT/app/workers.
100
+ - In application.rb, uncomment config.autoload_paths and put:
101
+
102
+ config.autoload_paths += %W(#{config.paths.app}/workers)
103
+
104
+ Now you can use your workers like their part of your app!
105
+
@@ -21,14 +21,14 @@ module SimpleWorker
21
21
  def inherited(subclass)
22
22
  subclass.reset!
23
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
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
27
  # splits = caller[0].split(":")
28
28
  # caller_file = splits[0] + ":" + splits[1]
29
29
  caller_file = caller[0][0...(caller[0].index(":in"))]
30
30
  caller_file = caller_file[0...(caller_file.rindex(":"))]
31
- puts 'caller_file=' + caller_file
31
+ # puts 'caller_file=' + caller_file
32
32
  # don't need these class_variables anymore probably
33
33
  subclass.instance_variable_set(:@caller_file, caller_file)
34
34
 
data/lib/simple_worker.rb CHANGED
@@ -1,178 +1,180 @@
1
- require 'appoxy_api'
2
- require File.join(File.dirname(__FILE__), 'simple_worker', 'base')
3
- require File.join(File.dirname(__FILE__), 'simple_worker', 'config')
4
- require File.join(File.dirname(__FILE__), 'simple_worker', 'used_in_worker')
5
-
6
-
7
- module SimpleWorker
8
-
9
- class << self
10
- attr_accessor :config,
11
- :service
12
-
13
- def configure()
14
- SimpleWorker.config ||= Config.new
15
- yield(config)
16
- SimpleWorker.service = Service.new(config.access_key, config.secret_key, :config=>config)
17
- end
18
- end
19
-
20
- class Service < Appoxy::Api::Client
21
-
22
- attr_accessor :config
23
-
24
- def initialize(access_key, secret_key, options={})
25
- puts 'Starting SimpleWorker::Service...'
26
- self.config = options[:config] if options[:config]
27
- super("http://api.simpleworkr.com/api/", access_key, secret_key, options)
28
- self.host = self.config.host if self.config && self.config.host
29
- end
30
-
31
- # Options:
32
- # - :callback_url
33
- # - :merge => array of files to merge in with this file
34
- def upload(filename, class_name, options={})
35
-
36
- # check whether it should upload again
37
- tmp = Dir.tmpdir()
38
- puts 'tmp=' + tmp.to_s
39
- md5file = "simple_workr_#{class_name.gsub("::", ".")}.md5"
40
- existing_md5 = nil
41
- f = File.join(tmp, md5file)
42
- if File.exists?(f)
43
- existing_md5 = IO.read(f)
44
- puts 'existing_md5=' + existing_md5
45
- end
46
-
47
- filename = build_merged_file(filename, options[:merge]) if options[:merge]
48
-
49
- # sys.classes[subclass].__file__
50
- # puts '__FILE__=' + Base.subclass.__file__.to_s
51
- md5 = Digest::MD5.hexdigest(File.read(filename))
52
- puts "new md5=" + md5
53
- if md5 != existing_md5
54
- puts 'new code, so uploading'
55
- File.open(f, 'w') { |f| f.write(md5) }
56
- else
57
- puts 'same code, not uploading'
58
- end
59
-
60
- mystring = nil
61
- file = File.open(filename, "r") do |f|
62
- mystring = f.read
63
- end
64
- options = {"code"=>mystring, "class_name"=>class_name}
65
- ret = post("code/put", options)
66
- ret
67
- end
68
-
69
- def build_merged_file(filename, merge)
70
- merge = merge.dup
71
- merge << filename
72
- fname2 = File.join(Dir.tmpdir(), File.basename(filename))
73
- puts 'fname2=' + fname2
74
- puts 'merged_file_array=' + merge.inspect
75
- File.open(fname2, "w") do |f|
76
- merge.each do |m|
77
- f.write File.open(m, 'r') { |mo| mo.read }
78
- f.write "\n\n"
79
- end
80
- end
81
- fname2
82
- end
83
-
84
- def add_sw_params(hash_to_send)
85
- # todo: remove secret key?? Can use worker service from within a worker without it now
86
- hash_to_send["sw_access_key"] = self.access_key
87
- hash_to_send["sw_secret_key"] = self.secret_key
88
- end
89
-
90
- # class_name: The class name of a previously upload class, eg: MySuperWorker
91
- # data: Arbitrary hash of your own data that your task will need to run.
92
- def queue(class_name, data={})
93
- if !data.is_a?(Array)
94
- data = [data]
95
- end
96
- hash_to_send = {}
97
- hash_to_send["payload"] = data
98
- hash_to_send["class_name"] = class_name
99
- add_sw_params(hash_to_send)
100
- if defined?(RAILS_ENV)
101
- hash_to_send["rails_env"] = RAILS_ENV
102
- end
103
- return queue_raw(class_name, hash_to_send)
104
-
105
- end
106
-
107
- def queue_raw(class_name, data={})
108
- params = nil
109
- hash_to_send = data
110
- hash_to_send["class_name"] = class_name
111
- ret = post("queue/add", hash_to_send)
112
- ret
113
-
114
- end
115
-
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(class_name, data, schedule)
128
- raise "Schedule must be a hash." if !schedule.is_a? Hash
129
- # if !data.is_a?(Array)
130
- # data = [data]
131
- # end
132
- hash_to_send = {}
133
- hash_to_send["payload"] = data
134
- hash_to_send["class_name"] = class_name
135
- hash_to_send["schedule"] = schedule
136
- add_sw_params(hash_to_send)
137
- # puts 'about to send ' + hash_to_send.inspect
138
- ret = post("scheduler/schedule", hash_to_send)
139
- ret
140
- end
141
-
142
- def cancel_schedule(scheduled_task_id)
143
- raise "Must include a schedule id." if scheduled_task_id.blank?
144
- hash_to_send = {}
145
- hash_to_send["scheduled_task_id"] = scheduled_task_id
146
- ret = post("scheduler/cancel", hash_to_send)
147
- ret
148
- end
149
-
150
- def get_schedules()
151
- hash_to_send = {}
152
- ret = get("scheduler/list", hash_to_send)
153
- ret
154
- end
155
-
156
- def status(task_id)
157
- data = {"task_id"=>task_id}
158
- ret = get("task/status", data)
159
- ret
160
- end
161
-
162
- def schedule_status(schedule_id)
163
- data = {"schedule_id"=>schedule_id}
164
- ret = get("scheduler/status", data)
165
- ret
166
- end
167
-
168
- def log(task_id)
169
- data = {"task_id"=>task_id}
170
- ret = get("task/log", data)
171
- puts 'ret=' + ret.inspect
172
- # ret["log"] = Base64.decode64(ret["log"])
173
- ret
174
- end
175
-
176
-
177
- end
1
+ require 'appoxy_api'
2
+ require 'active_support/core_ext'
3
+ require File.join(File.dirname(__FILE__), 'simple_worker', 'base')
4
+ require File.join(File.dirname(__FILE__), 'simple_worker', 'config')
5
+ require File.join(File.dirname(__FILE__), 'simple_worker', 'used_in_worker')
6
+
7
+
8
+ module SimpleWorker
9
+
10
+ class << self
11
+ attr_accessor :config,
12
+ :service
13
+
14
+ def configure()
15
+ SimpleWorker.config ||= Config.new
16
+ yield(config)
17
+ SimpleWorker.service = Service.new(config.access_key, config.secret_key, :config=>config)
18
+ end
19
+ end
20
+
21
+ class Service < Appoxy::Api::Client
22
+
23
+ attr_accessor :config
24
+
25
+ def initialize(access_key, secret_key, options={})
26
+ puts 'Starting SimpleWorker::Service...'
27
+ self.config = options[:config] if options[:config]
28
+ super("http://api.simpleworker.com/api/", access_key, secret_key, options)
29
+ self.host = self.config.host if self.config && self.config.host
30
+ end
31
+
32
+ # Options:
33
+ # - :callback_url
34
+ # - :merge => array of files to merge in with this file
35
+ def upload(filename, class_name, options={})
36
+
37
+ # check whether it should upload again
38
+ tmp = Dir.tmpdir()
39
+ # puts 'tmp=' + tmp.to_s
40
+ md5file = "simple_workr_#{class_name.gsub("::", ".")}.md5"
41
+ existing_md5 = nil
42
+ f = File.join(tmp, md5file)
43
+ if File.exists?(f)
44
+ existing_md5 = IO.read(f)
45
+ # puts 'existing_md5=' + existing_md5
46
+ end
47
+
48
+ filename = build_merged_file(filename, options[:merge]) if options[:merge]
49
+
50
+ # sys.classes[subclass].__file__
51
+ # puts '__FILE__=' + Base.subclass.__file__.to_s
52
+ md5 = Digest::MD5.hexdigest(File.read(filename))
53
+ # puts "new md5=" + md5
54
+
55
+ if md5 != existing_md5
56
+ puts "#{class_name}: new code, so uploading"
57
+ File.open(f, 'w') { |f| f.write(md5) }
58
+ else
59
+ puts "#{class_name}: same code, not uploading"
60
+ end
61
+
62
+ mystring = nil
63
+ file = File.open(filename, "r") do |f|
64
+ mystring = f.read
65
+ end
66
+ options = {"code"=>mystring, "class_name"=>class_name}
67
+ ret = post("code/put", options)
68
+ ret
69
+ end
70
+
71
+ def build_merged_file(filename, merge)
72
+ merge = merge.dup
73
+ merge << filename
74
+ fname2 = File.join(Dir.tmpdir(), File.basename(filename))
75
+ # puts 'fname2=' + fname2
76
+ # puts 'merged_file_array=' + merge.inspect
77
+ File.open(fname2, "w") do |f|
78
+ merge.each do |m|
79
+ f.write File.open(m, 'r') { |mo| mo.read }
80
+ f.write "\n\n"
81
+ end
82
+ end
83
+ fname2
84
+ end
85
+
86
+ def add_sw_params(hash_to_send)
87
+ # todo: remove secret key?? Can use worker service from within a worker without it now
88
+ hash_to_send["sw_access_key"] = self.access_key
89
+ hash_to_send["sw_secret_key"] = self.secret_key
90
+ end
91
+
92
+ # class_name: The class name of a previously upload class, eg: MySuperWorker
93
+ # data: Arbitrary hash of your own data that your task will need to run.
94
+ def queue(class_name, data={})
95
+ if !data.is_a?(Array)
96
+ data = [data]
97
+ end
98
+ hash_to_send = {}
99
+ hash_to_send["payload"] = data
100
+ hash_to_send["class_name"] = class_name
101
+ add_sw_params(hash_to_send)
102
+ if defined?(RAILS_ENV)
103
+ hash_to_send["rails_env"] = RAILS_ENV
104
+ end
105
+ return queue_raw(class_name, hash_to_send)
106
+
107
+ end
108
+
109
+ def queue_raw(class_name, data={})
110
+ params = nil
111
+ hash_to_send = data
112
+ hash_to_send["class_name"] = class_name
113
+ ret = post("queue/add", hash_to_send)
114
+ ret
115
+
116
+ end
117
+
118
+
119
+ #
120
+ # schedule: hash of scheduling options that can include:
121
+ # Required:
122
+ # - start_at: Time of first run - DateTime or Time object.
123
+ # Optional:
124
+ # - run_every: Time in seconds between runs. If ommitted, task will only run once.
125
+ # - delay_type: Fixed Rate or Fixed Delay. Default is fixed_delay.
126
+ # - end_at: Scheduled task will stop running after this date (optional, if ommitted, runs forever or until cancelled)
127
+ # - run_times: Task will run exactly :run_times. For instance if :run_times is 5, then the task will run 5 times.
128
+ #
129
+ def schedule(class_name, data, schedule)
130
+ raise "Schedule must be a hash." if !schedule.is_a? Hash
131
+ # if !data.is_a?(Array)
132
+ # data = [data]
133
+ # end
134
+ hash_to_send = {}
135
+ hash_to_send["payload"] = data
136
+ hash_to_send["class_name"] = class_name
137
+ hash_to_send["schedule"] = schedule
138
+ add_sw_params(hash_to_send)
139
+ # puts 'about to send ' + hash_to_send.inspect
140
+ ret = post("scheduler/schedule", hash_to_send)
141
+ ret
142
+ end
143
+
144
+ def cancel_schedule(scheduled_task_id)
145
+ raise "Must include a schedule id." if scheduled_task_id.blank?
146
+ hash_to_send = {}
147
+ hash_to_send["scheduled_task_id"] = scheduled_task_id
148
+ ret = post("scheduler/cancel", hash_to_send)
149
+ ret
150
+ end
151
+
152
+ def get_schedules()
153
+ hash_to_send = {}
154
+ ret = get("scheduler/list", hash_to_send)
155
+ ret
156
+ end
157
+
158
+ def status(task_id)
159
+ data = {"task_id"=>task_id}
160
+ ret = get("task/status", data)
161
+ ret
162
+ end
163
+
164
+ def schedule_status(schedule_id)
165
+ data = {"schedule_id"=>schedule_id}
166
+ ret = get("scheduler/status", data)
167
+ ret
168
+ end
169
+
170
+ def log(task_id)
171
+ data = {"task_id"=>task_id}
172
+ ret = get("task/log", data)
173
+ # puts 'ret=' + ret.inspect
174
+ # ret["log"] = Base64.decode64(ret["log"])
175
+ ret
176
+ end
177
+
178
+
179
+ end
178
180
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_worker
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 3
9
- - 11
10
- version: 0.3.11
8
+ - 13
9
+ version: 0.3.13
11
10
  platform: ruby
12
11
  authors:
13
12
  - Travis Reeder
@@ -15,23 +14,35 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-08-27 00:00:00 -07:00
17
+ date: 2010-10-24 00:00:00 -07:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
- name: appoxy_api
21
+ name: active_support
23
22
  prerelease: false
24
23
  requirement: &id001 !ruby/object:Gem::Requirement
25
24
  none: false
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 3
30
28
  segments:
31
29
  - 0
32
30
  version: "0"
33
31
  type: :runtime
34
32
  version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: appoxy_api
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
35
46
  description: I could tell you, but then I'd have to...
36
47
  email: travis@appoxy.com
37
48
  executables: []
@@ -69,7 +80,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
69
80
  requirements:
70
81
  - - ">="
71
82
  - !ruby/object:Gem::Version
72
- hash: 3
73
83
  segments:
74
84
  - 0
75
85
  version: "0"
@@ -78,7 +88,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
88
  requirements:
79
89
  - - ">="
80
90
  - !ruby/object:Gem::Version
81
- hash: 3
82
91
  segments:
83
92
  - 0
84
93
  version: "0"