simple_worker 0.3.24 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  # This is an abstract module that developers creating works can mixin/include to use the SimpleWorker special functions.
2
2
 
3
3
  require 'digest/md5'
4
+ require 'base64'
4
5
 
5
6
  module SimpleWorker
6
7
 
@@ -123,6 +124,7 @@ module SimpleWorker
123
124
  def run_local
124
125
  # puts 'run_local'
125
126
  set_auto_attributes
127
+ init_database
126
128
  begin
127
129
  run
128
130
  rescue => ex
@@ -134,6 +136,15 @@ module SimpleWorker
134
136
  end
135
137
  end
136
138
 
139
+ def init_database
140
+ if SimpleWorker.config.database
141
+ require 'active_record'
142
+ if !ActiveRecord::Base.connected?
143
+ ActiveRecord::Base.establish_connection(SimpleWorker.config.database)
144
+ end
145
+ end
146
+ end
147
+
137
148
  def set_auto_attributes
138
149
  set_global_attributes
139
150
  end
@@ -267,9 +278,12 @@ module SimpleWorker
267
278
 
268
279
  def sw_get_data
269
280
  data = {}
281
+
282
+ payload = {}
270
283
  self.instance_variables.each do |iv|
271
- data[iv] = instance_variable_get(iv)
284
+ payload[iv] = instance_variable_get(iv)
272
285
  end
286
+ data[:attr_encoded] = Base64.encode64(payload.to_json)
273
287
 
274
288
  config_data = SimpleWorker.config.get_atts_to_send
275
289
  data[:sw_config] = config_data
@@ -0,0 +1,7 @@
1
+ require 'simple_record'
2
+
3
+ class CoolModel < SimpleRecord::Base
4
+
5
+ has_strings :name
6
+
7
+ end
@@ -0,0 +1,14 @@
1
+
2
+ class CoolWorker < SimpleWorker::Base
3
+ attr_accessor :array_of_models
4
+ merge 'cool_model'
5
+
6
+
7
+ def run
8
+ 10.times do |i|
9
+ puts "HEY THERE PUTS #{i}"
10
+ log "HEY THERE LOG #{i}"
11
+ sleep 1
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ require 'mysql2'
2
+
3
+ class DbWorker < SimpleWorker::Base
4
+ attr_accessor :array_of_models
5
+ merge 'trace_object'
6
+
7
+ def run
8
+ to = TraceObject.find(:first)
9
+ log to.inspect
10
+ @object = to
11
+ end
12
+
13
+ def ob
14
+ @object
15
+ end
16
+
17
+
18
+ end
@@ -13,26 +13,20 @@ require_relative "test_worker_3"
13
13
  class TestBase < Test::Unit::TestCase
14
14
 
15
15
  def setup
16
- @config = YAML::load(File.open(File.expand_path("~/.test_configs/simple_worker.yml")))
16
+ @config = YAML::load(File.open(File.expand_path("~/.test_configs/simple_worker.yml")))
17
17
  #puts @config.inspect
18
18
  @access_key = @config['simple_worker']['access_key']
19
19
  @secret_key = @config['simple_worker']['secret_key']
20
20
 
21
21
  # new style
22
22
  SimpleWorker.configure do |config|
23
- config.access_key = @access_key
24
- config.secret_key = @secret_key
25
- # config.host = "http://localhost:3000/api/"
23
+ config.access_key = @access_key
24
+ config.secret_key = @secret_key
25
+ config.host = @config['simple_worker']['host']
26
26
  config.global_attributes["db_user"] = "sa"
27
27
  config.global_attributes["db_pass"] = "pass"
28
- config.database = {
29
- :adapter => "mysql2",
30
- :host => "localhost",
31
- :database => "appdb",
32
- :username => "appuser",
33
- :password => "secret"
34
- }
28
+ config.database = @config['database']
35
29
 
36
30
  end
37
31
  end
38
- end
32
+ end
@@ -1,14 +1,19 @@
1
+ require 'active_record'
1
2
  require_relative 'test_base'
3
+ require_relative 'cool_worker'
4
+ require_relative 'cool_model'
5
+ require_relative 'trace_object'
6
+ require_relative 'db_worker'
2
7
 
3
8
  class SimpleWorkerTests < TestBase
4
9
 
5
10
 
6
11
  def test_new_worker_style
7
12
  # Add something to queue, get task ID back
8
- tw = TestWorker2.new
13
+ tw = TestWorker2.new
9
14
  tw.s3_key = "active style runner"
10
- tw.times = 3
11
- tw.x = true
15
+ tw.times = 3
16
+ tw.x = true
12
17
 
13
18
  # schedule up a task
14
19
  # start_at = 10.seconds.since
@@ -27,7 +32,7 @@ class SimpleWorkerTests < TestBase
27
32
  begin
28
33
  response_hash_single = tw.queue
29
34
  rescue => ex
30
- puts ex.message
35
+ puts ex.message
31
36
  end
32
37
  end
33
38
 
@@ -55,6 +60,68 @@ class SimpleWorkerTests < TestBase
55
60
 
56
61
  end
57
62
 
63
+
64
+ def test_data_passing
65
+ cool = CoolWorker.new
66
+ cool.array_of_models = [CoolModel.new(:name=>"name1"), CoolModel.new(:name=>"name2")]
67
+ cool.queue
68
+ status = wait_for_task(cool)
69
+ assert status["status"] == "complete"
70
+ log = SimpleWorker.service.log(cool.task_id)
71
+ puts 'log=' + log.inspect
72
+ assert log.length > 10
73
+
74
+ end
75
+
76
+ def test_exceptions
77
+ worker = TestWorker.new
78
+ worker.queue
79
+ status = wait_for_task(worker)
80
+ assert status["status"] == "error"
81
+ assert status["msg"].present?
82
+ end
83
+
84
+ def test_active_record
85
+ dbw = DbWorker.new
86
+ dbw.run_local
87
+ assert !dbw.ob.nil?
88
+ assert !dbw.ob.id.nil?
89
+
90
+ dbw.queue
91
+ # would be interesting if the object could update itself on complete. Like it would retrieve new values from
92
+ # finished job when calling status or something.
93
+
94
+ status = wait_for_task(dbw)
95
+ assert status["status"] == "complete"
96
+
97
+
98
+ end
99
+
100
+
101
+ def wait_for_task(params={})
102
+ tries = 0
103
+ status = nil
104
+ sleep 1
105
+ while tries < 60
106
+ status = status_for(params)
107
+ puts 'status = ' + status.inspect
108
+ if status["status"] == "complete" || status["status"] == "error"
109
+ break
110
+ end
111
+ sleep 2
112
+ end
113
+ status
114
+ end
115
+
116
+ def status_for(ob)
117
+ if ob.is_a?(Hash)
118
+ ob[:schedule_id] ? WORKER.schedule_status(ob[:schedule_id]) : WORKER.status(ob[:task_id])
119
+ else
120
+ ob.status
121
+ end
122
+ end
123
+
124
+
58
125
  def test_require_relative_merge
59
126
 
60
127
 
@@ -0,0 +1,6 @@
1
+ class TraceObject < ActiveRecord::Base
2
+ belongs_to :tracking_device
3
+ has_many :location_updates
4
+
5
+
6
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.24
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-02-21 00:00:00.000000000 -08:00
12
+ date: 2011-02-24 00:00:00.000000000 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: appoxy_api
17
- requirement: &27134628 !ruby/object:Gem::Requirement
17
+ requirement: &27678636 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *27134628
25
+ version_requirements: *27678636
26
26
  description: The official SimpleWorker gem for http://www.simpleworker.com
27
27
  email: travis@appoxy.com
28
28
  executables: []
@@ -39,6 +39,9 @@ files:
39
39
  - lib/simple_worker/used_in_worker.rb
40
40
  - rails/init.rb
41
41
  - README.markdown
42
+ - test/cool_model.rb
43
+ - test/cool_worker.rb
44
+ - test/db_worker.rb
42
45
  - test/models/model_1.rb
43
46
  - test/models/model_2.rb
44
47
  - test/requiring_worker.rb
@@ -51,6 +54,7 @@ files:
51
54
  - test/test_worker.rb
52
55
  - test/test_worker_2.rb
53
56
  - test/test_worker_3.rb
57
+ - test/trace_object.rb
54
58
  has_rdoc: true
55
59
  homepage: http://github.com/appoxy/simple_worker
56
60
  licenses: []
@@ -77,6 +81,9 @@ signing_key:
77
81
  specification_version: 3
78
82
  summary: The official SimpleWorker gem for http://www.simpleworker.com
79
83
  test_files:
84
+ - test/cool_model.rb
85
+ - test/cool_worker.rb
86
+ - test/db_worker.rb
80
87
  - test/models/model_1.rb
81
88
  - test/models/model_2.rb
82
89
  - test/requiring_worker.rb
@@ -89,3 +96,4 @@ test_files:
89
96
  - test/test_worker.rb
90
97
  - test/test_worker_2.rb
91
98
  - test/test_worker_3.rb
99
+ - test/trace_object.rb