@@ -43,10 +43,21 @@ def initialize(opts = {})
43
43
# @raise [ArgumentError] if no block is given
44
44
def post ( intended_time , &task )
45
45
time = TimerSet . calculate_schedule_time ( intended_time ) . to_f
46
- if super ( time , &task )
47
- check_processing_thread!
46
+ raise ArgumentError . new ( 'no block given' ) unless block_given?
47
+
48
+ mutex . synchronize do
49
+ return false unless running?
50
+
51
+ if ( time - Time . now . to_f ) <= 0.01
52
+ @executor . post ( &task )
53
+ else
54
+ @queue . push ( Task . new ( time , task ) )
55
+ check_processing_thread!
56
+ end
57
+
48
58
true
49
59
end
60
+
50
61
end
51
62
52
63
alias_method :kill , :shutdown
@@ -84,20 +95,12 @@ def self.calculate_schedule_time(intended_time, now = Time.now)
84
95
# @!visibility private
85
96
Task = Struct . new ( :time , :op ) do
86
97
include Comparable
98
+
87
99
def <=>( other )
88
100
self . time <=> other . time
89
101
end
90
102
end
91
103
92
- # @!visibility private
93
- def execute ( time , &task )
94
- if ( time - Time . now . to_f ) <= 0.01
95
- @executor . post ( &task )
96
- else
97
- @queue . push ( Task . new ( time , task ) )
98
- end
99
- end
100
-
101
104
# @!visibility private
102
105
def shutdown_execution
103
106
@queue . clear
@@ -114,15 +117,13 @@ def shutdown_execution
114
117
#
115
118
# @!visibility private
116
119
def check_processing_thread!
117
- mutex . synchronize do
118
- return if shutdown? || @queue . empty?
119
- if @thread && @thread . status == 'sleep'
120
- @thread . wakeup
121
- elsif @thread . nil? || ! @thread . alive?
122
- @thread = Thread . new do
123
- Thread . current . abort_on_exception = true
124
- process_tasks
125
- end
120
+ return if shutdown? || @queue . empty?
121
+ if @thread && @thread . status == 'sleep'
122
+ @thread . wakeup
123
+ elsif @thread . nil? || !@thread . alive?
124
+ @thread = Thread . new do
125
+ Thread . current . abort_on_exception = true
126
+ process_tasks
126
127
end
127
128
end
128
129
end
0 commit comments