delayer 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/delayer/error.rb +1 -1
- data/lib/delayer/extend.rb +18 -19
- data/lib/delayer/procedure.rb +10 -7
- data/lib/delayer/version.rb +3 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e740459b8f4d9abb415778feffba4e4a80c506df2ccd393e443cd5aecf28d1d
|
4
|
+
data.tar.gz: 0fd9a1f1c7d3dad89a77712df97bf069d524ba7db66fc2e1158e8c6846643eee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efd0f011041ddc6124f86ff2cd18466e8fe52e317ed7dbe3b54210d613115db513a5d57e5566d619c836c63fc96a478567c60838750b44e2845bf3c085abb7cc
|
7
|
+
data.tar.gz: 1adbcf303d86d6179d4a554a6d4c09c1f5dc9006d56bcce890028c4498498268aa6a1396d2576255972f3f4f329dae8234770256098b7a8406dbde9b3a2cf9f5
|
data/lib/delayer/error.rb
CHANGED
data/lib/delayer/extend.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Delayer
|
4
4
|
attr_reader :priority
|
@@ -19,10 +19,10 @@ module Delayer
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
def initialize(priority = self.class.instance_eval{ @default_priority }, *
|
22
|
+
def initialize(priority = self.class.instance_eval { @default_priority }, *_args, &proc)
|
23
23
|
self.class.validate_priority priority
|
24
24
|
@priority = priority
|
25
|
-
@procedure = Procedure.new(self, &
|
25
|
+
@procedure = Procedure.new(self, &proc)
|
26
26
|
end
|
27
27
|
|
28
28
|
# Cancel this job
|
@@ -57,16 +57,16 @@ module Delayer
|
|
57
57
|
# ==== Return
|
58
58
|
# self
|
59
59
|
def run(current_expire = @expire)
|
60
|
-
if
|
61
|
-
run_once
|
60
|
+
if current_expire == 0
|
61
|
+
run_once until empty?
|
62
62
|
else
|
63
63
|
@end_time = Time.new.to_f + @expire
|
64
|
-
run_once while
|
64
|
+
run_once while !empty? && (@end_time >= Time.new.to_f)
|
65
65
|
@end_time = nil
|
66
66
|
end
|
67
67
|
if @remain_hook
|
68
68
|
@remain_received = !empty?
|
69
|
-
@remain_hook.call if @remain_received
|
69
|
+
@remain_hook.call if @remain_received
|
70
70
|
end
|
71
71
|
rescue Exception => e
|
72
72
|
@exception = e
|
@@ -74,7 +74,7 @@ module Delayer
|
|
74
74
|
end
|
75
75
|
|
76
76
|
def expire?
|
77
|
-
if defined?(@end_time)
|
77
|
+
if defined?(@end_time) && @end_time
|
78
78
|
@end_time < Time.new.to_f
|
79
79
|
else
|
80
80
|
false
|
@@ -88,7 +88,7 @@ module Delayer
|
|
88
88
|
if @bucket.first
|
89
89
|
@busy = true
|
90
90
|
procedure = forward
|
91
|
-
procedure = forward while @bucket.first
|
91
|
+
procedure = forward while @bucket.first && procedure.canceled?
|
92
92
|
procedure.run unless procedure.canceled?
|
93
93
|
end
|
94
94
|
ensure
|
@@ -97,7 +97,7 @@ module Delayer
|
|
97
97
|
|
98
98
|
# Return if some jobs processing now.
|
99
99
|
# ==== Args
|
100
|
-
# [args]
|
100
|
+
# [args]
|
101
101
|
# ==== Return
|
102
102
|
# true if Delayer processing job
|
103
103
|
def busy?
|
@@ -137,10 +137,8 @@ module Delayer
|
|
137
137
|
procedure.next = @bucket.first
|
138
138
|
@bucket.priority_of[priority] = @bucket.first = procedure
|
139
139
|
end
|
140
|
-
if @bucket.last
|
141
|
-
|
142
|
-
end
|
143
|
-
if @remain_hook and not @remain_received
|
140
|
+
@bucket.last = @bucket.priority_of[priority] if @bucket.last
|
141
|
+
if @remain_hook && !@remain_received
|
144
142
|
@remain_received = true
|
145
143
|
@remain_hook.call
|
146
144
|
end
|
@@ -148,8 +146,8 @@ module Delayer
|
|
148
146
|
self
|
149
147
|
end
|
150
148
|
|
151
|
-
def register_remain_hook
|
152
|
-
@remain_hook =
|
149
|
+
def register_remain_hook(&proc)
|
150
|
+
@remain_hook = proc
|
153
151
|
end
|
154
152
|
|
155
153
|
def get_prev_point(priority)
|
@@ -157,7 +155,7 @@ module Delayer
|
|
157
155
|
@bucket.priority_of[priority]
|
158
156
|
else
|
159
157
|
next_index = @priorities.index(priority) - 1
|
160
|
-
get_prev_point @priorities[next_index] if
|
158
|
+
get_prev_point @priorities[next_index] if next_index >= 0
|
161
159
|
end
|
162
160
|
end
|
163
161
|
|
@@ -180,8 +178,9 @@ module Delayer
|
|
180
178
|
# [Delayer::NoLowerLevelError] stash_enter!が呼ばれていない時
|
181
179
|
# [Delayer::RemainJobsError] ジョブが残っているのにこのメソッドを呼んだ時
|
182
180
|
def stash_exit!
|
183
|
-
raise Delayer::NoLowerLevelError, 'stash_exit! called in level 0.'
|
184
|
-
raise Delayer::RemainJobsError, 'Current level has remain jobs. It must be empty current level jobs in call this method.'
|
181
|
+
raise Delayer::NoLowerLevelError, 'stash_exit! called in level 0.' unless @bucket.stashed
|
182
|
+
raise Delayer::RemainJobsError, 'Current level has remain jobs. It must be empty current level jobs in call this method.' unless empty?
|
183
|
+
|
185
184
|
@bucket = @bucket.stashed
|
186
185
|
end
|
187
186
|
|
data/lib/delayer/procedure.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
-
#
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Delayer
|
4
4
|
class Procedure
|
5
5
|
attr_reader :state, :delayer
|
6
6
|
attr_accessor :next
|
7
7
|
def initialize(delayer, &proc)
|
8
|
-
@delayer
|
8
|
+
@delayer = delayer
|
9
|
+
@proc = proc
|
9
10
|
@state = :stop
|
10
11
|
@next = nil
|
11
12
|
@delayer.class.register(self)
|
@@ -17,9 +18,10 @@ module Delayer
|
|
17
18
|
# ==== Return
|
18
19
|
# node
|
19
20
|
def run
|
20
|
-
unless
|
21
|
-
raise Delayer::StateError(@state),
|
21
|
+
unless @state == :stop
|
22
|
+
raise Delayer::StateError(@state), 'call twice Delayer::Procedure'
|
22
23
|
end
|
24
|
+
|
23
25
|
@state = :run
|
24
26
|
@proc.call
|
25
27
|
@state = :done
|
@@ -32,9 +34,10 @@ module Delayer
|
|
32
34
|
# ==== Return
|
33
35
|
# self
|
34
36
|
def cancel
|
35
|
-
unless
|
36
|
-
raise Delayer::StateError(@state),
|
37
|
+
unless @state == :stop
|
38
|
+
raise Delayer::StateError(@state), 'cannot cancel Delayer::Procedure'
|
37
39
|
end
|
40
|
+
|
38
41
|
@state = :cancel
|
39
42
|
self
|
40
43
|
end
|
@@ -43,7 +46,7 @@ module Delayer
|
|
43
46
|
# ==== Return
|
44
47
|
# true if canceled this task
|
45
48
|
def canceled?
|
46
|
-
|
49
|
+
@state == :cancel
|
47
50
|
end
|
48
51
|
|
49
52
|
# insert node between self and self.next
|
data/lib/delayer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delayer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toshiaki Asai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
|
-
rubygems_version: 3.0.
|
100
|
+
rubygems_version: 3.0.6
|
101
101
|
signing_key:
|
102
102
|
specification_version: 4
|
103
103
|
summary: Delay the processing
|