jober 0.0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
- require_relative "spec_helper"
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
2
 
3
3
  class MyChain < Jober::Task
4
- every 3
4
+ interval 3
5
5
 
6
6
  def perform
7
7
  sleep 5
@@ -1,7 +1,7 @@
1
- require_relative "spec_helper"
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
2
 
3
3
  class A < Jober::Task
4
- every 3000
4
+ interval 3000
5
5
 
6
6
  def perform
7
7
  10.times do |i|
@@ -12,7 +12,7 @@ class A < Jober::Task
12
12
  end
13
13
 
14
14
  class B < Jober::Queue
15
- every 3
15
+ interval 3
16
16
 
17
17
  def perform(x)
18
18
  SO["b"] += x
@@ -20,18 +20,29 @@ class B < Jober::Queue
20
20
  end
21
21
 
22
22
  class C < Jober::QueueBatch
23
- every 3
23
+ interval 3
24
24
 
25
25
  def perform(batch)
26
- SO["c"] = batch.flatten
26
+ SO["c"] += batch.flatten
27
+ end
28
+ end
29
+
30
+ class D < Jober::Task
31
+ workers 2
32
+
33
+ def perform
34
+ SO["wrk:#{@worker_id}:#{@workers_count}"] = 1
27
35
  end
28
36
  end
29
37
 
30
38
  describe "integration" do
31
39
  it "should work" do
32
40
  SO["b"] = 0
33
- run_manager_for(5, [A, B, C])
41
+ SO["c"] = []
42
+ run_manager_for(5, [A, B, C, D])
34
43
  SO["b"].should == 45
35
44
  SO["c"].should == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
45
+
46
+ SO.keys("wrk:*").sort.should == ["Jober::shared:wrk:0:2", "Jober::shared:wrk:1:2"]
36
47
  end
37
48
  end
@@ -0,0 +1,25 @@
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
+
3
+ class A1 < Jober::Task; end
4
+ class A2 < Jober::Queue; end
5
+ class A3 < Jober::QueueBatch; end
6
+ class A4 < Jober::UniqueQueue; end
7
+
8
+ class Jober::B1 < Jober::Task; end
9
+
10
+ describe "Jober" do
11
+ it "classes" do
12
+ Jober.internal_classes_names.each do |k|
13
+ Jober.classes.should_not include(eval(k))
14
+ end
15
+ end
16
+
17
+ it "find_class" do
18
+ Jober.find_class("A1").should == A1
19
+ Jober.find_class("A1234").should == nil
20
+ Jober.find_class("B1").should == Jober::B1
21
+ Jober.find_class("Jober::B1").should == Jober::B1
22
+ Jober.find_class("B2").should == nil
23
+ Jober.find_class("Jober::B2").should == nil
24
+ end
25
+ end
@@ -1,4 +1,4 @@
1
- require_relative "spec_helper"
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
2
 
3
3
  class LongTask1 < Jober::Task
4
4
  def perform
@@ -0,0 +1,16 @@
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
+
3
+ class TestQ1 < Jober::Queue
4
+ end
5
+
6
+ describe "use namespace for keys" do
7
+ it "should work" do
8
+ TestQ1.queue_name.should == 'Jober::queue:test_q1'
9
+ Jober.namespace = "bla"
10
+ TestQ1.set_queue_name(TestQ1.short_name)
11
+ TestQ1.queue_name.should == 'Jober:bla:queue:test_q1'
12
+ Jober.namespace = nil
13
+ TestQ1.set_queue_name(TestQ1.short_name)
14
+ TestQ1.queue_name.should == 'Jober::queue:test_q1'
15
+ end
16
+ end
@@ -1,7 +1,7 @@
1
- require_relative "spec_helper"
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
2
 
3
3
  class Man1 < Jober::Task
4
- every 2
4
+ interval 2
5
5
 
6
6
  def perform
7
7
  sleep 3
@@ -9,7 +9,7 @@ class Man1 < Jober::Task
9
9
  end
10
10
 
11
11
  class Man2 < Jober::Task
12
- every 2
12
+ interval 2
13
13
 
14
14
  def perform
15
15
  sleep 2
@@ -35,5 +35,12 @@ describe "manage pids" do
35
35
  sleep 1.1
36
36
  m.pids.size.should == 2
37
37
  end
38
+
39
+ st = Jober.stats
40
+ st['man1'][:crashed].should_not be
41
+ st['man2'][:crashed].should be
42
+
43
+ st['man1'][:finished].should be
44
+ st['man2'][:finished].should_not be
38
45
  end
39
46
  end
@@ -1,4 +1,4 @@
1
- require_relative "spec_helper"
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
2
 
3
3
  class MyBatchQueue < Jober::QueueBatch
4
4
  batch_size 6
@@ -14,7 +14,7 @@ end
14
14
  describe "QueueBatch" do
15
15
  it "should set internals" do
16
16
  w = MyBatchQueue.new
17
- w.queue_name.should == 'Jober:queue:my_batch'
17
+ w.queue_name.should == 'Jober::queue:my_batch'
18
18
  end
19
19
 
20
20
  it "should execute" do
@@ -1,7 +1,8 @@
1
- require_relative "spec_helper"
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
2
 
3
3
  class FkMyParallel < Jober::Queue
4
- 5.times { |i| every 3 }
4
+ interval 3
5
+ workers 5
5
6
 
6
7
  def perform(arg)
7
8
  SO["fork:#{$$}"] ||= 0
@@ -1,4 +1,4 @@
1
- require_relative "spec_helper"
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
2
 
3
3
  class MyQueue < Jober::Queue
4
4
  def initialize(*args)
@@ -23,7 +23,7 @@ end
23
23
  describe "Queue" do
24
24
  it "should set internals" do
25
25
  w = MyQueue.new
26
- w.queue_name.should == 'Jober:queue:my'
26
+ w.queue_name.should == 'Jober::queue:my'
27
27
  end
28
28
 
29
29
  it "should execute" do
@@ -39,7 +39,6 @@ describe "Queue" do
39
39
 
40
40
  it "custom queue name" do
41
41
  10.times { Jasdfoadsfjaf.enqueue }
42
- p Jober.llens
43
42
  Jober.llens['human_name'].should == 10
44
43
  end
45
44
  end
@@ -1,4 +1,4 @@
1
- require_relative "spec_helper"
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
2
 
3
3
  describe "SO" do
4
4
  it "should work" do
@@ -4,6 +4,8 @@ Bundler.require :default
4
4
  Jober::SharedObject
5
5
  SO = Jober::SharedObject
6
6
 
7
+ Jober.logger = Logger.new("#{File.dirname(__FILE__)}/spec.log")
8
+
7
9
  RSpec.configure do |config|
8
10
  config.expect_with(:rspec) { |c| c.syntax = :should }
9
11
 
@@ -1,4 +1,4 @@
1
- require_relative "spec_helper"
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
2
 
3
3
  class MyQueue1 < Jober::Queue
4
4
  def perform
@@ -25,9 +25,11 @@ describe "Stats" do
25
25
  MyQueue1.enqueue
26
26
  run_manager_for(3, [MyQueue1, MyQueue2])
27
27
  h = Jober.stats
28
- h['my1'][:start].should be
29
- h['my1'][:end].should be
28
+ h['my1'][:started].should be
29
+ h['my1'][:finished].should be
30
30
  h['my1'][:duration].should be_within(0.1).of(2.0)
31
- h['my2'].should == {:start=>nil, :end=>nil, :duration=>nil}
31
+ h['my2'][:started].should be
32
+ h['my2'][:finished].should be
33
+ h['my2'][:duration].should be_within(0.1).of(0.0)
32
34
  end
33
35
  end
@@ -0,0 +1,103 @@
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
+
3
+ class AA1 < Jober::Task
4
+ interval 10
5
+ end
6
+
7
+ class AA2 < AA1
8
+ workers 2
9
+ end
10
+
11
+ class AA3 < AA2
12
+ interval 15
13
+ end
14
+
15
+ class AA4 < AA3
16
+ end
17
+
18
+ class Loop1 < Jober::Task
19
+ interval 1
20
+ def perform
21
+ SO["x"] ||= 0
22
+ SO["x"] += 1
23
+ end
24
+ end
25
+
26
+ class Loop2 < Jober::Task
27
+ interval 3
28
+ def perform
29
+ SO["y"] ||= 0
30
+ SO["y"] += 1
31
+ end
32
+ end
33
+
34
+ describe "Task" do
35
+ it "interval interval should be inherited" do
36
+ AA1.get_interval.should == 10
37
+ AA2.get_interval.should == 10
38
+ AA3.get_interval.should == 15
39
+ AA4.get_interval.should == 15
40
+ end
41
+
42
+ it "workers should not be inherited" do
43
+ AA1.get_workers.should == 1
44
+ AA2.get_workers.should == 2
45
+ AA3.get_workers.should == 1
46
+ end
47
+
48
+ it "run_loop" do
49
+ t = Thread.new { Loop1.new.run_loop }
50
+ sleep 3.5
51
+ t.kill
52
+ SO["x"].should == 4
53
+ st = Jober.stats['loop1']
54
+ (st[:finished] - st[:started]).should be_within(0.001).of(0)
55
+ (Time.now - st[:finished]).should be_within(0.7).of(1)
56
+ end
57
+
58
+ it "run_loop should wait, until interval ready" do
59
+ Loop2.new.execute
60
+ t = Thread.new { Loop2.new.run_loop }
61
+ sleep 4
62
+ t.kill
63
+ SO["y"].should == 2
64
+ end
65
+
66
+ it "run_loop should wait, until interval ready" do
67
+ l = Loop2.new
68
+ l.execute
69
+ t = Thread.new { l.run_loop }
70
+ sleep 2
71
+ l.stopped = true
72
+ sleep 0.3
73
+ SO["y"].should == 1
74
+ end
75
+
76
+ it "if skip_delay!, should start imidiately" do
77
+ l = Loop2.new
78
+ l.execute
79
+ Loop2.skip_delay!
80
+ t = Thread.new { l.run_loop }
81
+ sleep 2
82
+ l.stopped = true
83
+ sleep 0.3
84
+ SO["y"].should == 2
85
+ end
86
+
87
+ it "if skip_delay by option, should start imidiately" do
88
+ l = Loop2.new(:skip_delay => true)
89
+ l.execute
90
+ t = Thread.new { l.run_loop }
91
+ sleep 2
92
+ l.stopped = true
93
+ sleep 0.3
94
+ SO["y"].should == 2
95
+ end
96
+
97
+ it "skip_delay!" do
98
+ Loop2.pop_skip_delay_flag!.should == false
99
+ Loop2.skip_delay!
100
+ Loop2.pop_skip_delay_flag!.should == true
101
+ Loop2.pop_skip_delay_flag!.should == false
102
+ end
103
+ end
@@ -1,4 +1,4 @@
1
- require_relative "spec_helper"
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
2
 
3
3
  class WorkerTask < Jober::Task
4
4
  def initialize(*args)
@@ -14,16 +14,15 @@ class WorkerTask < Jober::Task
14
14
  end
15
15
 
16
16
  class Task2 < Jober::Task
17
- every 3, :bla
18
- every 5
19
-
20
- def bla
21
- end
22
-
17
+ interval 15
18
+ workers 2
23
19
  def perform
24
20
  end
25
21
  end
26
22
 
23
+ class Jober::Bla < Jober::Task
24
+ end
25
+
27
26
  describe "Task" do
28
27
  it "should execute" do
29
28
  w = WorkerTask.new
@@ -32,13 +31,15 @@ describe "Task" do
32
31
 
33
32
  it "should set short_name" do
34
33
  WorkerTask.short_name.should == 'worker'
34
+ Jober::Bla.short_name.should == 'bla'
35
35
  end
36
36
 
37
37
  it "should register class" do
38
38
  Jober.classes.should include(WorkerTask)
39
39
  end
40
40
 
41
- it "set some schedulers" do
42
- Task2.workers.should == [[3, :bla], [5, :perform]]
41
+ it "set some settings" do
42
+ Task2.get_workers.should == 2
43
+ Task2.get_interval.should == 15
43
44
  end
44
45
  end
@@ -0,0 +1,28 @@
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
+
3
+ class MyUniqueBatchQueue < Jober::UniqueQueueBatch
4
+ batch_size 6
5
+
6
+ attr_reader :res
7
+
8
+ def perform(batch)
9
+ @res ||= []
10
+ @res << batch
11
+ end
12
+ end
13
+
14
+ describe "QueueBatch" do
15
+ it "should set internals" do
16
+ w = MyUniqueBatchQueue.new
17
+ w.queue_name.should == 'Jober::queue:my_unique_batch'
18
+ end
19
+
20
+ it "should execute" do
21
+ 10.times { |i| 10.times { MyUniqueBatchQueue.enqueue(i) } }
22
+ MyUniqueBatchQueue.new.execute.res.flatten.sort.should == (0..9).to_a
23
+ end
24
+
25
+ it "should register class" do
26
+ Jober.classes.should include(MyUniqueBatchQueue)
27
+ end
28
+ end
@@ -1,4 +1,4 @@
1
- require_relative "spec_helper"
1
+ require "#{File.dirname(__FILE__)}/spec_helper"
2
2
 
3
3
  class MyUniqueQueue < Jober::UniqueQueue
4
4
  def initialize(*args)
@@ -16,7 +16,7 @@ end
16
16
  describe "Queue" do
17
17
  it "should set internals" do
18
18
  w = MyUniqueQueue.new
19
- w.queue_name.should == 'Jober:queue:my_unique'
19
+ w.queue_name.should == 'Jober::queue:my_unique'
20
20
  end
21
21
 
22
22
  it "should execute Only for unique values" 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.0.1
4
+ version: '0.2'
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-04-22 00:00:00.000000000 Z
11
+ date: 2015-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -66,26 +66,58 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activerecord
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  description: Simple background jobs, queues.
70
98
  email:
71
99
  - "'kostya27@gmail.com'"
72
100
  executables:
73
101
  - jober
74
- - manager
102
+ - jober_manager
75
103
  extensions: []
76
104
  extra_rdoc_files: []
77
105
  files:
78
106
  - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
79
109
  - Gemfile
80
110
  - LICENSE.txt
81
111
  - Rakefile
112
+ - bench/queue.rb
82
113
  - bin/jober
83
- - bin/manager
114
+ - bin/jober_manager
84
115
  - examples/classes.rb
85
116
  - examples/man.rb
86
117
  - jober.gemspec
87
118
  - lib/jober.rb
88
119
  - lib/jober/abstract_task.rb
120
+ - lib/jober/ar_loop.rb
89
121
  - lib/jober/logger.rb
90
122
  - lib/jober/manager.rb
91
123
  - lib/jober/queue.rb
@@ -93,10 +125,14 @@ files:
93
125
  - lib/jober/shared_object.rb
94
126
  - lib/jober/task.rb
95
127
  - lib/jober/unique_queue.rb
128
+ - lib/jober/unique_queue_batch.rb
96
129
  - lib/jober/version.rb
130
+ - spec/ar_loop_spec.rb
97
131
  - spec/chain_spec.rb
98
132
  - spec/integration_spec.rb
133
+ - spec/jober_spec.rb
99
134
  - spec/kill_task_spec.rb
135
+ - spec/namespace_spec.rb
100
136
  - spec/pids_spec.rb
101
137
  - spec/queue_batch_spec.rb
102
138
  - spec/queue_parallel_spec.rb
@@ -104,7 +140,9 @@ files:
104
140
  - spec/shared_object_spec.rb
105
141
  - spec/spec_helper.rb
106
142
  - spec/stats_spec.rb
143
+ - spec/task_loop_spec.rb
107
144
  - spec/task_spec.rb
145
+ - spec/unique_queue_batch_spec.rb
108
146
  - spec/unique_queue_spec.rb
109
147
  homepage: ''
110
148
  licenses:
@@ -131,9 +169,12 @@ signing_key:
131
169
  specification_version: 4
132
170
  summary: Simple background jobs, queues.
133
171
  test_files:
172
+ - spec/ar_loop_spec.rb
134
173
  - spec/chain_spec.rb
135
174
  - spec/integration_spec.rb
175
+ - spec/jober_spec.rb
136
176
  - spec/kill_task_spec.rb
177
+ - spec/namespace_spec.rb
137
178
  - spec/pids_spec.rb
138
179
  - spec/queue_batch_spec.rb
139
180
  - spec/queue_parallel_spec.rb
@@ -141,5 +182,7 @@ test_files:
141
182
  - spec/shared_object_spec.rb
142
183
  - spec/spec_helper.rb
143
184
  - spec/stats_spec.rb
185
+ - spec/task_loop_spec.rb
144
186
  - spec/task_spec.rb
187
+ - spec/unique_queue_batch_spec.rb
145
188
  - spec/unique_queue_spec.rb