simple_worker 0.1.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,114 @@
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
+ end
14
+
15
+ def self.inherited(subclass)
16
+ puts "New subclass: #{subclass}"
17
+ puts "subclass.inspect=" + subclass.inspect
18
+ puts 'existing caller=' + (subclass.class_variable_defined?(:@@caller_file) ? subclass.class_variable_get(:@@caller_file).inspect : "nil")
19
+ puts "caller=" + caller.inspect
20
+ splits = caller[0].split(":")
21
+ caller_file = splits[0] + ":" + splits[1]
22
+ puts 'caller_file=' + caller_file
23
+ # don't need these class_variables anymore probably
24
+ subclass.class_variable_set(:@@caller_file, caller_file)
25
+
26
+ end
27
+
28
+
29
+ def log(str)
30
+ puts str.to_s
31
+ end
32
+
33
+ def set_progress(hash)
34
+ puts 'set_progress: ' + hash.inspect
35
+ end
36
+
37
+ def who_am_i?
38
+ return self.class.name
39
+ end
40
+
41
+ def uploaded?
42
+ self.class.class_variable_defined?(:@@uploaded) && self.class.class_variable_get(:@@uploaded)
43
+ end
44
+
45
+
46
+ # Will send in all instance_variables.
47
+ def queue
48
+ upload_if_needed
49
+
50
+ response = SimpleWorker.service.queue(self.class.name, sw_get_data)
51
+ puts 'queue response=' + response.inspect
52
+ @task_set_id = response["task_set_id"]
53
+ @task_id = response["tasks"][0]["task_id"]
54
+ response
55
+ end
56
+
57
+ def status
58
+ SimpleWorker.service.status(task_id)
59
+ end
60
+
61
+
62
+ def schedule(schedule)
63
+ upload_if_needed
64
+
65
+ response = SimpleWorker.service.schedule(self.class.name, sw_get_data, schedule)
66
+ puts 'schedule response=' + response.inspect
67
+ @schedule_id = response["schedule_id"]
68
+ response
69
+ end
70
+
71
+ def schedule_status
72
+ SimpleWorker.service.schedule_status(schedule_id)
73
+ end
74
+
75
+
76
+ # def queue_other(class_name, data)
77
+ # SimpleWorker.service.queue(class_name, data)
78
+ # end
79
+ #
80
+ # def schedule_other(class_name, data, schedule)
81
+ # SimpleWorker.service.schedule(class_name, data, schedule)
82
+ # end
83
+
84
+ private
85
+
86
+ def upload_if_needed
87
+
88
+ # $LOADED_FEATURES.each_with_index { |feature, idx|
89
+ # puts "#{ sprintf("%2s", idx) } #{feature}"
90
+ # if feature[feature.rindex("/")..feature.length] ==
91
+ #}
92
+ unless uploaded?
93
+ subclass = self.class
94
+ rfile = subclass.class_variable_get(:@@caller_file) # Base.caller_file # File.expand_path(Base.subclass)
95
+ puts 'rfile=' + rfile.inspect
96
+ puts 'self.class.name=' + subclass.name
97
+ SimpleWorker.service.upload(rfile, subclass.name)
98
+ self.class.class_variable_set(:@@uploaded, true)
99
+ else
100
+ puts 'already uploaded for ' + self.class.name
101
+ end
102
+ end
103
+
104
+ def sw_get_data
105
+ data = {}
106
+ self.instance_variables.each do |iv|
107
+ data[iv] = instance_variable_get(iv)
108
+ end
109
+ return data
110
+ end
111
+
112
+
113
+ end
114
+ end
@@ -0,0 +1,9 @@
1
+ module SimpleWorker
2
+ class Config
3
+ attr_accessor :access_key,
4
+ :secret_key,
5
+ :host
6
+ end
7
+
8
+ end
9
+
data/lib/simple_worker.rb CHANGED
@@ -1,19 +1,57 @@
1
1
  require 'appoxy_api'
2
- require File.join(File.dirname(__FILE__), 'worker')
3
-
2
+ require File.join(File.dirname(__FILE__), 'simple_worker', 'base')
3
+ require File.join(File.dirname(__FILE__), 'simple_worker', 'config')
4
4
 
5
5
  module SimpleWorker
6
6
 
7
+ class << self
8
+ attr_accessor :config,
9
+ :service
10
+
11
+ end
12
+
13
+ def self.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
+
7
19
  class Service < Appoxy::Api::Client
8
20
 
21
+ attr_accessor :config
22
+
9
23
  def initialize(access_key, secret_key, options={})
10
24
  puts 'Starting SimpleWorker::Service...'
25
+ self.config = options[:config] if options[:config]
11
26
  super("http://api.simpleworkr.com/api/", access_key, secret_key, options)
27
+ self.host = self.config.host if self.config && self.config.host
12
28
  end
13
29
 
14
30
  # Options:
15
31
  # - :callback_url
16
32
  def upload(filename, class_name, options={})
33
+
34
+ # check whether it should upload again
35
+ tmp = Dir.tmpdir()
36
+ puts 'tmp=' + tmp.to_s
37
+ md5file = "simple_workr_#{class_name.gsub("::", ".")}.md5"
38
+ existing_md5 = nil
39
+ f = File.join(tmp, md5file)
40
+ if File.exists?(f)
41
+ existing_md5 = IO.read(f)
42
+ puts 'existing_md5=' + existing_md5
43
+ end
44
+ # sys.classes[subclass].__file__
45
+ # puts '__FILE__=' + Base.subclass.__file__.to_s
46
+ md5 = Digest::MD5.hexdigest(File.read(filename))
47
+ puts "new md5=" + md5
48
+ if md5 != existing_md5
49
+ puts 'new code, so uploading'
50
+ File.open(f, 'w') { |f| f.write(md5) }
51
+ else
52
+ puts 'same code, not uploading'
53
+ end
54
+
17
55
  mystring = nil
18
56
  file = File.open(filename, "r") do |f|
19
57
  mystring = f.read
@@ -23,8 +61,13 @@ module SimpleWorker
23
61
  ret
24
62
  end
25
63
 
26
- #
27
- # data:
64
+ def add_sw_params(hash_to_send)
65
+ hash_to_send["sw_access_key"] = self.access_key
66
+ hash_to_send["sw_secret_key"] = self.secret_key
67
+ end
68
+
69
+ # class_name: The class name of a previously upload class, eg: MySuperWorker
70
+ # data: Arbitrary hash of your own data that your task will need to run.
28
71
  def queue(class_name, data={})
29
72
  if !data.is_a?(Array)
30
73
  data = [data]
@@ -32,6 +75,7 @@ module SimpleWorker
32
75
  hash_to_send = {}
33
76
  hash_to_send["payload"] = data
34
77
  hash_to_send["class_name"] = class_name
78
+ add_sw_params(hash_to_send)
35
79
  if defined?(RAILS_ENV)
36
80
  hash_to_send["rails_env"] = RAILS_ENV
37
81
  end
@@ -68,7 +112,8 @@ module SimpleWorker
68
112
  hash_to_send["payload"] = data
69
113
  hash_to_send["class_name"] = class_name
70
114
  hash_to_send["schedule"] = schedule
71
- puts 'about to send ' + hash_to_send.inspect
115
+ add_sw_params(hash_to_send)
116
+ # puts 'about to send ' + hash_to_send.inspect
72
117
  ret = post("scheduler/schedule", hash_to_send)
73
118
  ret
74
119
  end
@@ -93,6 +138,12 @@ module SimpleWorker
93
138
  ret
94
139
  end
95
140
 
141
+ def schedule_status(schedule_id)
142
+ data = {"schedule_id"=>schedule_id}
143
+ ret = get("scheduler/status", data)
144
+ ret
145
+ end
146
+
96
147
  def log(task_id)
97
148
  data = {"task_id"=>task_id}
98
149
  ret = get("task/log", data)
@@ -0,0 +1,7 @@
1
+
2
+
3
+ require File.join(File.dirname(__FILE__), "./test_worker_2")
4
+
5
+ worker = TestWorker2.new
6
+ puts worker.who_am_i?
7
+ puts worker.who_am_i2?
@@ -1,6 +1,12 @@
1
1
  require 'test/unit'
2
2
  require 'yaml'
3
- require File.join(File.dirname(__FILE__), '../lib/simple_worker')
3
+ begin
4
+ require File.join(File.dirname(__FILE__), '../lib/simple_worker')
5
+ rescue
6
+ require 'simple_worker'
7
+ end
8
+ require File.join(File.dirname(__FILE__), "./test_worker")
9
+ require File.join(File.dirname(__FILE__), "./test_worker_2")
4
10
 
5
11
  class SimpleWorkerTests < Test::Unit::TestCase
6
12
 
@@ -12,12 +18,51 @@ class SimpleWorkerTests < Test::Unit::TestCase
12
18
 
13
19
  @worker = SimpleWorker::Service.new(@access_key, @secret_key)
14
20
  @worker.host = "http://localhost:3000/api/"
21
+
22
+ # new style
23
+ SimpleWorker.configure do |config|
24
+ config.access_key = @access_key
25
+ config.secret_key = @secret_key
26
+ config.host = "http://localhost:3000/api/"
27
+ end
15
28
  end
16
29
 
17
30
  def teardown
18
31
 
19
32
  end
20
33
 
34
+ def test_new_worker_style
35
+ # Add something to queue, get task ID back
36
+ tw = TestWorker2.new
37
+ tw.s3_key = "active style runner"
38
+ tw.times = 3
39
+ tw.x = true
40
+
41
+ # schedule up a task
42
+ start_at = 10.seconds.since
43
+ response_hash_single = tw.schedule(:start_at=>start_at, :run_every=>30, :run_times=>3)
44
+ puts 'response_hash=' + response_hash_single.inspect
45
+
46
+ 10.times do |i|
47
+ puts "status #{i}: " + tw.schedule_status.inspect
48
+ end
49
+
50
+ # queue up a task
51
+ response_hash_single = tw.queue
52
+ puts 'response_hash=' + response_hash_single.inspect
53
+ puts 'task_set_id=' + tw.task_set_id
54
+ puts 'task_id=' + tw.task_id
55
+ 10.times do |i|
56
+ puts "status #{i}: " + tw.status.inspect
57
+ break if tw.status["status"] == "complete"
58
+ sleep 2
59
+ end
60
+
61
+ assert tw.status["status"] == "complete"
62
+
63
+ end
64
+
65
+
21
66
  def test_queue
22
67
 
23
68
 
@@ -74,5 +119,6 @@ class SimpleWorkerTests < Test::Unit::TestCase
74
119
  puts 'response_hash=' + response_hash.inspect
75
120
 
76
121
  end
122
+
77
123
  end
78
124
 
data/test/test_worker.rb CHANGED
@@ -1,10 +1,18 @@
1
- require 'simple_worker'
2
-
1
+ begin
2
+ require File.join(File.dirname(__FILE__), '../lib/simple_worker')
3
+ rescue Exception
4
+ require 'simple_worker'
5
+ end
3
6
  # Bump for new checksum.
4
7
  class TestWorker < SimpleWorker::Base
5
8
 
9
+ attr_accessor :s3_key, :times
10
+
6
11
  def run(data=nil)
7
12
  log 'running the runner for leroy '.upcase + ' with data: ' + data.inspect
13
+
14
+ log 's3_key instance_variable = ' + self.s3_key
15
+
8
16
  @times = data["times"].to_i
9
17
  @times.times do |i|
10
18
  log 'running at ' + i.to_s
@@ -0,0 +1,32 @@
1
+ begin
2
+ require File.join(File.dirname(__FILE__), '../lib/simple_worker')
3
+ rescue Exception
4
+ require 'simple_worker'
5
+ end
6
+
7
+ # Bump for new checksum.
8
+ class TestWorker2 < SimpleWorker::Base
9
+
10
+ attr_accessor :s3_key, :times, :x
11
+
12
+ def who_am_i2?
13
+ return self.class.name
14
+ end
15
+
16
+ def run(data=nil)
17
+ log 'running the runner for leroy '.upcase + ' with data: ' + data.inspect
18
+
19
+ log 's3_key instance_variable = ' + self.s3_key
20
+ times.times do |i|
21
+ log 'running at ' + i.to_s
22
+ sleep 1
23
+ set_progress(:percent=> (i / times * 100))
24
+ end
25
+ end
26
+
27
+ def set_complete(params=nil)
28
+ log 'SET COMPLETE YAY!' + params[:task_set_id]
29
+ end
30
+
31
+ end
32
+
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 2
9
- version: 0.1.2
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
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-04-17 00:00:00 -07:00
17
+ date: 2010-05-12 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -39,7 +39,8 @@ extra_rdoc_files:
39
39
  - README.markdown
40
40
  files:
41
41
  - lib/simple_worker.rb
42
- - lib/worker.rb
42
+ - lib/simple_worker/base.rb
43
+ - lib/simple_worker/config.rb
43
44
  - README.markdown
44
45
  has_rdoc: true
45
46
  homepage: http://github.com/appoxy/simple_worker
@@ -73,5 +74,7 @@ specification_version: 3
73
74
  summary: Classified
74
75
  test_files:
75
76
  - test/scheduled_worker.rb
77
+ - test/test_inheritance.rb
76
78
  - test/test_simple_worker.rb
77
79
  - test/test_worker.rb
80
+ - test/test_worker_2.rb
data/lib/worker.rb DELETED
@@ -1,14 +0,0 @@
1
- # This is an abstract module that developers creating works can mixin/include to use the SimpleWorker special functions.
2
- module SimpleWorker
3
-
4
- class Base
5
-
6
- def log(str)
7
- puts str.to_s
8
- end
9
-
10
- def set_progress(hash)
11
- puts 'set_progress: ' + hash.inspect
12
- end
13
- end
14
- end