jober 0.3.37 → 0.3.38

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 68d2b76b5ce65c40e2dd400e56da949542b58ad1
4
- data.tar.gz: b1bf5509a4eb9046d54a8f210543608b9cd7c685
3
+ metadata.gz: 00c159d15e3e82299c7bab18a5a9793925955a09
4
+ data.tar.gz: c59fe0b176b51fc601e04a647b73209fd0519e62
5
5
  SHA512:
6
- metadata.gz: b85813cff723ce9c897b5737c949c53f223621df6a17aceda7d717e868eeb83dedbb43f5c39897a540ba602a44bf69562a6f2b2a92e5324a73ec633ae0aadde4
7
- data.tar.gz: 9aedb0ffbe32e15120d4f6ba5036a5f7a93b00487c987114b54b63a8a2ff16654d3da141f77b1a0254f641fa4281711967aac83697530969c8767c77eba66304
6
+ metadata.gz: b70718209990268efaa926bf9044d1bccf52cc010b3d215f365607a0771dd2461025749a9cdb77797f5dd4ded4b1da59e33c3af7bc49fc3c7354fca9b7f06cf3
7
+ data.tar.gz: 99c68a86e99ec22e92b480abcd9d6e61a627ba31f13ff0f38a0591b59fbfb24f9fe62957d071ff4a7c0c77c4053439edd8fb69e0266fffe076f015ade2b59404
data/bin/jober CHANGED
@@ -36,13 +36,21 @@ end.parse!
36
36
 
37
37
  if req = options[:require]
38
38
  require File.expand_path(req)
39
+ else
40
+ filenames = []
41
+ filenames << File.expand_path(ENV['APP_ROOT']) if ENV['APP_ROOT']
42
+ filenames << File.expand_path('config/environment')
43
+ filenames << File.expand_path('config/environment.rb')
44
+ filenames.each do |filename|
45
+ require filename if File.exist?(filename)
46
+ end
39
47
  end
40
48
 
41
49
  klass_name = options[:klass]
42
50
  klass = Jober.find_class(klass_name)
43
51
 
44
52
  unless klass
45
- puts "not found class #{klass_name}"
53
+ puts "Please set class_name by option -c"
46
54
  exit(1)
47
55
  end
48
56
 
data/lib/jober.rb CHANGED
@@ -9,6 +9,7 @@ module Jober
9
9
  autoload :AbstractTask, 'jober/abstract_task'
10
10
  autoload :Task, 'jober/task'
11
11
  autoload :Queue, 'jober/queue'
12
+ autoload :SlowQueue, 'jober/slow_queue'
12
13
  autoload :QueueBatch, 'jober/queue_batch'
13
14
  autoload :UniqueQueue, 'jober/unique_queue'
14
15
  autoload :Logger, 'jober/logger'
@@ -40,7 +41,7 @@ module Jober
40
41
  end
41
42
 
42
43
  def internal_classes_names
43
- @internal_classes_names ||= (%w{Manager ThreadedManager AbstractTask Task Queue} +
44
+ @internal_classes_names ||= (%w{Manager ThreadedManager AbstractTask Task Queue SlowQueue} +
44
45
  %w{QueueBatch UniqueQueue UniqueQueueBatch Logger SharedObject ARLoop Exception}).map { |k| "Jober::#{k}" }
45
46
  end
46
47
 
@@ -32,7 +32,7 @@ class Jober::AbstractTask
32
32
  attr_accessor :short_name
33
33
  end
34
34
 
35
- attr_reader :finished, :stopped, :worker_id, :workers_count
35
+ attr_reader :finished, :stopped, :worker_id, :workers_count, :unique_id
36
36
 
37
37
  def self.inherited(base)
38
38
  Jober.add_class(base)
@@ -51,6 +51,7 @@ class Jober::AbstractTask
51
51
  @worker_id = (opts[:worker_id] || 0).to_i
52
52
  @workers_count = (opts[:workers_count] || 1).to_i
53
53
  @skip_delay = opts[:skip_delay]
54
+ @unique_id = opts[:unique_id]
54
55
  end
55
56
 
56
57
  def before_execute
data/lib/jober/logger.rb CHANGED
@@ -12,6 +12,7 @@ module Jober::Logger
12
12
  def logger_tag
13
13
  @logger_tag ||= begin
14
14
  tag = '[' + self.class.to_s
15
+ tag += "(#{unique_id})" if respond_to?(:unique_id) && unique_id.to_i > 0
15
16
  tag += " #{@worker_id}-#{@workers_count}" if @worker_id && @workers_count && @workers_count > 1
16
17
  tag += ']'
17
18
  tag
data/lib/jober/queue.rb CHANGED
@@ -22,6 +22,10 @@ class Jober::Queue < Jober::Task
22
22
  Jober.redis.rpush(queue_name, Jober.dump_args(*args))
23
23
  end
24
24
 
25
+ def self.dequeue(*args)
26
+ Jober.redis.lpush(queue_name, Jober.dump_args(*args))
27
+ end
28
+
25
29
  def self.len
26
30
  Jober.redis.llen(self.queue_name)
27
31
  end
@@ -0,0 +1,12 @@
1
+ class Jober::SlowQueue < Jober::Queue
2
+
3
+ # one event per perform
4
+ def run
5
+ if args = pop
6
+ perform(*args)
7
+ info { "processed event args: #{args.inspect}" }
8
+ else
9
+ info { "no pending events" }
10
+ end
11
+ end
12
+ end
@@ -13,13 +13,16 @@ class Jober::ThreadedManager
13
13
  def initialize(klasses = nil, opts = {})
14
14
  @klasses = Array(klasses || Jober.auto_classes)
15
15
  @stopped = false
16
+ h = Hash.new(0)
16
17
  @objects = @klasses.map do |klass|
17
18
  if klass.is_a?(String)
18
19
  klass_str = klass
19
20
  klass = Jober.find_class(klass_str)
20
21
  raise "unknown class #{klass_str}" unless klass
21
22
  end
22
- klass.new(opts)
23
+ obj = klass.new(opts.merge(:unique_id => h[klass]))
24
+ h[klass] += 1
25
+ obj
23
26
  end
24
27
  end
25
28
 
data/lib/jober/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Jober
2
- VERSION = "0.3.37"
2
+ VERSION = "0.3.38"
3
3
  end
@@ -0,0 +1,42 @@
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
+
3
+ class MySlowQueue < Jober::SlowQueue
4
+ interval 0.5
5
+ def perform(x)
6
+ $slow_counter += x
7
+ end
8
+ end
9
+
10
+ describe "SlowQueue" do
11
+ before { $slow_counter = 0 }
12
+
13
+ it "should set internals" do
14
+ w = MySlowQueue.new
15
+ w.queue_name.should == 'Jober::queue:my_slow'
16
+ end
17
+
18
+ it "should execute" do
19
+ MySlowQueue.enqueue(1)
20
+ MySlowQueue.enqueue(2)
21
+ MySlowQueue.enqueue(3)
22
+ MySlowQueue.new.execute
23
+ $slow_counter.should == 1
24
+ end
25
+
26
+ it "run_loop" do
27
+ MySlowQueue.enqueue(1)
28
+ MySlowQueue.enqueue(2)
29
+ MySlowQueue.enqueue(3)
30
+ t = Thread.new do
31
+ MySlowQueue.new.run_loop
32
+ end
33
+ sleep 0.1
34
+ $slow_counter.should == 1
35
+ sleep 0.6
36
+ $slow_counter.should == 3
37
+ sleep 0.6
38
+ $slow_counter.should == 6
39
+ t.kill
40
+ end
41
+
42
+ end
@@ -3,6 +3,7 @@ require "#{File.dirname(__FILE__)}/spec_helper"
3
3
  class T1 < Jober::Task
4
4
  def perform
5
5
  SO["t1"] += 1
6
+ SO["un"] += [unique_id]
6
7
  end
7
8
  end
8
9
 
@@ -57,6 +58,7 @@ describe Jober::ThreadedManager do
57
58
  SO["t3"] = 0
58
59
  SO["t41"] = 0
59
60
  SO["t5"] = 0
61
+ SO["un"] = []
60
62
  end
61
63
 
62
64
  it "just work" do
@@ -64,9 +66,10 @@ describe Jober::ThreadedManager do
64
66
  SO["t1"].should == 1
65
67
  end
66
68
 
67
- xit "run multiple identical tasks" do
68
- run_threaded_manager_for(0.2, [T1, T1, T1])
69
- SO["t"].should == 3
69
+ it "run multiple identical tasks" do
70
+ run_threaded_manager_for(0.5, [T1, T1, T1])
71
+ sleep 0.2
72
+ SO["un"].sort.should == [0, 1, 2]
70
73
  end
71
74
 
72
75
  it "run multiple tasks" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jober
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.37
4
+ version: 0.3.38
5
5
  platform: ruby
6
6
  authors:
7
7
  - "'Konstantin Makarchev'"
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-01 00:00:00.000000000 Z
11
+ date: 2015-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -125,6 +125,7 @@ files:
125
125
  - lib/jober/queue.rb
126
126
  - lib/jober/queue_batch.rb
127
127
  - lib/jober/shared_object.rb
128
+ - lib/jober/slow_queue.rb
128
129
  - lib/jober/task.rb
129
130
  - lib/jober/threaded_manager.rb
130
131
  - lib/jober/unique_queue.rb
@@ -141,6 +142,7 @@ files:
141
142
  - spec/queue_parallel_spec.rb
142
143
  - spec/queue_spec.rb
143
144
  - spec/shared_object_spec.rb
145
+ - spec/slow_queue_spec.rb
144
146
  - spec/spec_helper.rb
145
147
  - spec/stats_spec.rb
146
148
  - spec/task_loop_spec.rb
@@ -168,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
170
  version: '0'
169
171
  requirements: []
170
172
  rubyforge_project:
171
- rubygems_version: 2.2.2
173
+ rubygems_version: 2.4.7
172
174
  signing_key:
173
175
  specification_version: 4
174
176
  summary: Simple background jobs, queues.
@@ -184,6 +186,7 @@ test_files:
184
186
  - spec/queue_parallel_spec.rb
185
187
  - spec/queue_spec.rb
186
188
  - spec/shared_object_spec.rb
189
+ - spec/slow_queue_spec.rb
187
190
  - spec/spec_helper.rb
188
191
  - spec/stats_spec.rb
189
192
  - spec/task_loop_spec.rb