worker-army 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bcedbff4a178a8d672661146ff5307b3c0bcbe90
4
- data.tar.gz: 8d94f2c1e5e3829f18a7b9f66db5e2b65e467547
3
+ metadata.gz: d99e9dc0f645797ece237c3c921c8347ef296704
4
+ data.tar.gz: a6a474a23035b5aadcb5429bbba28b04155dbb06
5
5
  SHA512:
6
- metadata.gz: 08c8ca3a72475abfc54d1316c175503b6b66679c04bf24a0cbae3ecd52540369cae604564828a9bc163d7ec43c8dcefc15325b6a80ed4e42bc9295043a76ffba
7
- data.tar.gz: 4e61962caf4ec1a08b431d798e2538f809370ff4a5a619535e5569bdb2363a159522e279bcdbff0b3c79d1e8b8e80e1cc4fa6b5554fee1eea27d1253c7a0d5b1
6
+ metadata.gz: 393216db68a6b8a97d091750bf12ee5e5fe532cf16de6ceb3e63bce3e495869e3ba5b576c26f5c5959a0f8537bdffee01b39b748e8baa82123c59ff9dc54cf3a
7
+ data.tar.gz: 2cc3f0ad6f50383db1a026d4b4afa99e0de14e62d2ba48fa6e0c5be1931acde14da565d321a34a208f6735103d97533562c40c1ca8b2a8785f7860164104165b
data/README.md CHANGED
@@ -42,7 +42,7 @@ or use Foreman (checkout `Procfile`)
42
42
 
43
43
  $ foreman start
44
44
 
45
- You easily run the worker-army server on heroku. It should work out of the box. You're just going to have provide the configuration environment variables.
45
+ You easily run the worker-army server on heroku. It should work out of the box (you'll need to setup a redis database though). You're just going to have provide the configuration environment variables.
46
46
 
47
47
  You can open the server status overview by calling the server root url:
48
48
 
@@ -64,9 +64,9 @@ Provide an (optional) URL as the last argument and worker-army will return the j
64
64
 
65
65
  ## Workers
66
66
 
67
- You can start up a worker with a job class assigned with the following:
67
+ You can start up a worker with numerous job classes assigned to it with the following:
68
68
 
69
- $ rake start_worker[ExampleJob]
69
+ $ rake start_worker[ExampleJob,AnotherJob]
70
70
 
71
71
  ## Jobs
72
72
 
data/Rakefile CHANGED
@@ -42,14 +42,17 @@ Rake::RDocTask.new do |rdoc|
42
42
  end
43
43
 
44
44
  require File.dirname(__FILE__) + '/lib/worker-army'
45
- task 'start_example_worker' do
46
- WorkerArmy::Worker.new(ExampleJob.new).process_queue
45
+ task 'start_example_workers' do
46
+ WorkerArmy::Worker.new([ExampleJob.new, AnotherJob.new]).process_queue
47
47
  end
48
48
 
49
- desc "Start a worker-army worker to execute a job class"
50
- task :start_worker, :job_class do |t, args|
51
- if args[:job_class]
52
- clazz = Object.const_get(args[:job_class].to_s)
53
- WorkerArmy::Worker.new(clazz.new).process_queue
49
+ desc "Start a worker-army worker to execute job classes"
50
+ task :start_worker, :job_classes do |t, args|
51
+ if args[:job_classes]
52
+ clazzes = []
53
+ args[:job_classes].to_s.split(",").each do |jc|
54
+ clazzes << Object.const_get(jc.lstrip.rstrip).new
55
+ end
56
+ WorkerArmy::Worker.new(clazzes).process_queue
54
57
  end
55
58
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.6.0
data/lib/worker-army.rb CHANGED
@@ -7,3 +7,4 @@ require File.dirname(__FILE__) + '/worker_army/queue'
7
7
  require File.dirname(__FILE__) + '/worker_army/worker'
8
8
  require File.dirname(__FILE__) + '/worker_army/client'
9
9
  require File.dirname(__FILE__) + '/worker_army/example_job'
10
+ require File.dirname(__FILE__) + '/worker_army/another_job'
@@ -0,0 +1,10 @@
1
+ class AnotherJob
2
+ attr_accessor :log
3
+
4
+ def perform(data = {})
5
+ response_data = {foo2: 'bar2'}
6
+ log.debug("in another job with data: #{data}")
7
+ sleep 2
8
+ response_data
9
+ end
10
+ end
@@ -3,7 +3,7 @@ class ExampleJob
3
3
 
4
4
  def perform(data = {})
5
5
  response_data = {foo: 'bar'}
6
- log.debug("in example worker with data: #{data}")
6
+ log.debug("in example job with data: #{data}")
7
7
  sleep 2
8
8
  response_data
9
9
  end
@@ -48,9 +48,9 @@ module WorkerArmy
48
48
  queue_name: queue_name }
49
49
  end
50
50
 
51
- def pop(job_class_name, queue_prefix = "queue")
51
+ def pop(job_instances, queue_prefix = "queue")
52
52
  raise "No redis connection!" unless Queue.redis_instance
53
- return Queue.redis_instance.blpop("#{queue_prefix}_#{job_class_name}")
53
+ return Queue.redis_instance.blpop(job_instances.collect {|j| "#{queue_prefix}_#{j.class.name}"})
54
54
  end
55
55
 
56
56
  def save_result(data)
@@ -6,26 +6,33 @@ require File.dirname(__FILE__) + '/base'
6
6
 
7
7
  module WorkerArmy
8
8
  class Worker < Base
9
- attr_accessor :queue, :job, :worker_name, :processed, :failed
10
- def initialize(job, worker_name = nil)
9
+ attr_accessor :queue, :jobs, :job_names, :worker_name, :processed, :failed
10
+ def initialize(jobs, worker_name = nil)
11
11
  @queue = WorkerArmy::Queue.new
12
- @job = job
12
+ @log = WorkerArmy::Log.new.log
13
+ @job_names = []
14
+ @jobs = jobs
15
+ if @jobs and @jobs.size > 0
16
+ @jobs.each do |job|
17
+ job.log = @log if job.respond_to?(:log)
18
+ @job_names << job.class.name
19
+ end
20
+ end
21
+ @job_names = @job_names.uniq
13
22
  @worker_name = worker_name
14
23
  @host_name = Socket.gethostname
15
24
  @processed = 0
16
25
  @failed = 0
17
26
  @config = self.config
18
- @log = WorkerArmy::Log.new.log
19
27
  end
20
28
 
21
29
  def process_queue
22
- raise "No job class set!" unless @job
23
- @job.log = @log if @job.respond_to?(:log)
24
- @queue.ping(worker_pid: Process.pid, job_name: @job.class.name, host_name: @host_name,
30
+ raise "No job classes set!" if @jobs.nil? or @jobs.size == 0
31
+ @queue.ping(worker_pid: Process.pid, job_names: @job_names.join(", "), host_name: @host_name,
25
32
  timestamp: Time.now.utc.to_i)
26
- @log.info("Worker #{@host_name}-#{Process.pid} => Queue: queue_#{@job.class.name}")
33
+ @job_names.each {|job| @log.info("Worker #{@host_name}-#{Process.pid} => Queue: queue_#{job.class.name}")}
27
34
  @log.info("Worker #{@host_name}-#{Process.pid} => Processed: #{@processed} - Failed: #{@failed}")
28
- list, element = @queue.pop(@job.class.name)
35
+ list, element = @queue.pop(@jobs)
29
36
  if list and element
30
37
  execute_job(list, element, 0)
31
38
  end
@@ -40,10 +47,10 @@ module WorkerArmy
40
47
  data = JSON.parse(element)
41
48
  job_id = data['job_id']
42
49
  callback_url = data['callback_url']
43
- if @job and @job.class.name == data['job_class']
50
+ if @jobs and @job_names.include?(data['job_class'])
44
51
  @queue.add_current_job(job_id)
45
52
  started_at = Time.now.utc.to_i
46
- response_data = @job.perform(data)
53
+ response_data = @jobs.select {|j| j.class.name == data['job_class']}.first.perform(data)
47
54
  response_data = {} unless response_data
48
55
  if callback_url and not callback_url.empty?
49
56
  response_data.merge!(callback_url: callback_url)
data/worker-army.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: worker-army 0.5.0 ruby lib
5
+ # stub: worker-army 0.6.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "worker-army"
9
- s.version = "0.5.0"
9
+ s.version = "0.6.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Oliver Kiessler"]
14
- s.date = "2014-06-05"
14
+ s.date = "2014-07-24"
15
15
  s.description = "Simple redis based worker queue with a HTTP/Rest interface"
16
16
  s.email = "kiessler@inceedo.com"
17
17
  s.executables = ["worker_army"]
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
32
32
  "bin/worker_army",
33
33
  "config.ru",
34
34
  "lib/worker-army.rb",
35
+ "lib/worker_army/another_job.rb",
35
36
  "lib/worker_army/base.rb",
36
37
  "lib/worker_army/client.rb",
37
38
  "lib/worker_army/example_job.rb",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: worker-army
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Kiessler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-05 00:00:00.000000000 Z
11
+ date: 2014-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -171,6 +171,7 @@ files:
171
171
  - bin/worker_army
172
172
  - config.ru
173
173
  - lib/worker-army.rb
174
+ - lib/worker_army/another_job.rb
174
175
  - lib/worker_army/base.rb
175
176
  - lib/worker_army/client.rb
176
177
  - lib/worker_army/example_job.rb