worker 0.3.0 → 0.6.0

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
  SHA256:
3
- metadata.gz: 230e471bd7fe37454a7ba582b15e4f06ef17bdc037b021d903a60e3f3307e0c9
4
- data.tar.gz: 3ac9095f860d85be292ca7a7b9533ad4153ddbfcb7ac736b58852cc9cb0ec85a
3
+ metadata.gz: 0ed9501a4b408fdaa2ed8148c365c6b8ddf3c4283cb15d55542eb77604248348
4
+ data.tar.gz: 666d8b603800b3365d4586b6505fb262bb1a9b02eb8063d37b11f62e8a2a5f99
5
5
  SHA512:
6
- metadata.gz: 40a5843f71ff52a7754ae8b3735d5b01797523057a6076a71ceea8c092edfa4d6b8e1c4ab6a71282c7c4629b7d5a0c372b9ed472cef6cab51780676503725ac4
7
- data.tar.gz: e01fbdbb7419d413a303a42ec4cf78e7641f7b1bf4578f13d32e868ec40b8364663e849587f2c708bf2429b4c4658380f76ab9f749f53bff05c457ddc672444f
6
+ metadata.gz: be216fe2c0cd8161bd59d651fa7a5d68cba397b6d725dc1dd35aec27c01adf7183f888328b114a32af7f5cfeb80f8b2081b9001d92e688ef70e915ccd9eb5a62
7
+ data.tar.gz: '0925deeae5faf50999ed7337bc440808149c18448ce6da765e0b7404c7b125c3508d92a94c0c049be5b62bcef0e372b863e572f96c615a7d06a40f78d1c1d4b5'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- worker (0.3.0)
4
+ worker (0.6.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -16,3 +16,5 @@ end
16
16
  binary.perform 1, 2
17
17
  unary.perform 1
18
18
  unary_array.perform [1]
19
+
20
+ puts "ok"
@@ -0,0 +1,34 @@
1
+ require_relative "../lib/worker"
2
+
3
+ async_adder = Worker.new do |a,b|
4
+ sleep 0.25
5
+ a+b
6
+ end
7
+
8
+ async_adder_puts = Worker.new do |a,b|
9
+ sleep 0.25
10
+ puts a+b
11
+ end
12
+
13
+ defer = async_adder.perform_async 1, 2
14
+ print "result is: "
15
+ puts defer.value
16
+
17
+ puts "random order putsing:"
18
+ 5.times do |i|
19
+ async_adder_puts.perform_async(i, 1)
20
+ end
21
+ async_adder_puts.join
22
+
23
+
24
+ no_value = Worker.new do
25
+ sleep 0.1
26
+ 1
27
+ end
28
+
29
+ begin
30
+ defer = no_value.perform_async
31
+ defer.value!
32
+ rescue Worker::Defer::ValueError
33
+ puts "no value"
34
+ end
@@ -0,0 +1,49 @@
1
+ require_relative "../lib/worker"
2
+
3
+ raiser = Worker.new do
4
+ raise "always"
5
+ end
6
+
7
+ begin
8
+ raiser.perform
9
+ rescue
10
+ puts "did raise"
11
+ end
12
+
13
+ did_retry = 0
14
+ begin
15
+ retryer = Worker.new retry: 1 do
16
+ puts "about to raise: #{did_retry}"
17
+ did_retry += 1
18
+ raise "always"
19
+ end
20
+ end
21
+
22
+ begin
23
+ retryer.perform
24
+ rescue
25
+ end
26
+
27
+ puts "---"
28
+
29
+ did_retry = 0
30
+ backoff_retryer = Worker.new retry: 8, backoff: 0.1, backoff_max: 0.5 do
31
+ puts "about to raise: #{did_retry}"
32
+ did_retry += 1
33
+ raise "always"
34
+ end
35
+
36
+ begin
37
+ backoff_retryer.perform
38
+ rescue
39
+ end
40
+
41
+ safe_worker = Worker.new retry: 3, raise: false do
42
+ raise "doesn't matter"
43
+ ensure
44
+ puts "I'm safe"
45
+ end
46
+
47
+ safe_worker.perform
48
+
49
+ puts "ok"
@@ -2,12 +2,40 @@ class Worker
2
2
  class Ctx
3
3
  end
4
4
 
5
- def initialize(&block)
5
+ class Defer
6
+ class ValueError < StandardError
7
+ end
8
+
9
+ def initialize(&block)
10
+ @value = Queue.new
11
+
12
+ Thread.new do
13
+ @value.push block.call
14
+ end
15
+ end
16
+
17
+ def value
18
+ @value.pop
19
+ end
20
+
21
+ def value!
22
+ if @value.length == 1
23
+ value
24
+ else
25
+ raise ValueError
26
+ end
27
+ end
28
+ end
29
+
30
+ def initialize(opts={}, &block)
6
31
  @in = Queue.new
7
32
  @out = Queue.new
8
33
  @block = block
9
34
  @ctx = Worker::Ctx.new
35
+ @defers = Queue.new
10
36
 
37
+ @retries = 0
38
+ @opts = opts
11
39
  run!
12
40
  end
13
41
 
@@ -20,6 +48,39 @@ class Worker
20
48
  else
21
49
  ret
22
50
  end
51
+ rescue Exception => ex
52
+ backoff = @opts.dig(:backoff) || 0.1
53
+ backoff_max = @opts.dig(:backoff_max)
54
+ retries_max = @opts.dig(:retry) || 0
55
+
56
+ if @retries == retries_max
57
+ @retries = 0
58
+ return if @opts.dig(:raise) == false
59
+ raise ex
60
+ end
61
+ @retries += 1
62
+ sleeping = @retries * backoff
63
+ sleeping = backoff_max if backoff_max && sleeping > backoff_max
64
+
65
+ sleep sleeping
66
+ retry
67
+ end
68
+
69
+ def perform_async(*args)
70
+ defer = Defer.new do
71
+ ret = perform(*args)
72
+ @defers.pop
73
+ ret
74
+ end
75
+ @defers.push defer
76
+ defer
77
+ end
78
+
79
+ def join
80
+ loop do
81
+ break if @defers.size == 0
82
+ sleep 0.1
83
+ end
23
84
  end
24
85
 
25
86
  def run!
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "worker"
7
- spec.version = "0.3.0"
7
+ spec.version = "0.6.0"
8
8
  spec.authors = ["Matti Paksula"]
9
9
  spec.email = ["matti.paksula@iki.fi"]
10
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matti Paksula
@@ -73,6 +73,8 @@ files:
73
73
  - bin/setup
74
74
  - examples/adder_memo.rb
75
75
  - examples/args.rb
76
+ - examples/async.rb
77
+ - examples/exception.rb
76
78
  - examples/simple.rb
77
79
  - lib/worker.rb
78
80
  - worker.gemspec