@@ -34,11 +34,18 @@ module Marmot
34
34
35
35
alias Callback = Proc (Task , Nil )
36
36
37
+ Log = ::Log .for(self )
38
+ if level = ENV [" MARMOT_DEBUG" ]?
39
+ Log .level = ::Log ::Severity ::Debug
40
+ end
41
+
37
42
@@tasks = Array (Task ).new
38
- @@stopped = true
43
+ @@running = false
39
44
@@stop_channel = Channel (Nil ).new
40
45
41
46
abstract class Task
47
+ Log = Marmot ::Log .for(self , Marmot ::Log .level)
48
+
42
49
@canceled = false
43
50
@callback : Callback = - > (t : Task ) {}
44
51
@@ -48,6 +55,7 @@ module Marmot
48
55
#
49
56
# A canceled task cannot be uncanceled.
50
57
def cancel
58
+ Log .debug { " Task #{ self } canceled" }
51
59
@canceled = true
52
60
end
53
61
@@ -135,27 +143,34 @@ module Marmot
135
143
end
136
144
137
145
class RepeatTask < Task
146
+ Log = Task ::Log .for(self , Task ::Log .level)
147
+
138
148
def initialize (@span : Time ::Span , @first_run : Bool , @callback : Callback )
139
149
end
140
150
141
151
protected def wait_next_tick : Nil
142
152
if @first_run
143
153
@first_run = false
144
154
else
155
+ Log .debug { " Task #{ self } sleeping #{ @span } " }
145
156
sleep @span
146
157
end
147
158
end
148
159
end
149
160
150
161
extend self
151
162
152
- # Runs a task every day at *hour* and *minute*.
153
- def cron (hour, minute, second = 0 , & block : Callback ) : Task
154
- task = CronTask .new(hour, minute, second, block)
163
+ private def add_task (task : Task ) : Task
155
164
@@tasks << task
156
165
task
157
166
end
158
167
168
+ # Runs a task every day at *hour* and *minute*.
169
+ def cron (hour, minute, second = 0 , & block : Callback ) : Task
170
+ Log .debug { " New task to run every day at #{ hour } :#{ minute } :#{ second } " }
171
+ add_task CronTask .new(hour, minute, second, block)
172
+ end
173
+
159
174
# Runs a task when a value is received on a channel.
160
175
#
161
176
# To access the value, you need to restrict the type of the task, and use
@@ -166,19 +181,17 @@ module Marmot
166
181
# Marmot.on(channel) { |task| puts task.as(OnChannelTask).value }
167
182
# ```
168
183
def on (channel, & block : Callback ) : Task
169
- task = OnChannelTask .new(channel, block)
170
- @@tasks << task
171
- task
184
+ Log .debug { " New task to run on message on #{ channel } " }
185
+ add_task OnChannelTask .new(channel, block)
172
186
end
173
187
174
188
# Runs a task every given *span*.
175
189
#
176
190
# If first run is true, it will run as soon as the scheduler runs.
177
191
# Else it will wait *span* time before running for first time.
178
192
def repeat (span : Time ::Span , first_run = false , & block : Callback ) : Task
179
- task = RepeatTask .new(span, first_run, block)
180
- @@tasks << task
181
- task
193
+ Log .debug { " New task to repeat every #{ span } " }
194
+ add_task RepeatTask .new(span, first_run, block)
182
195
end
183
196
184
197
# Cancels all the tasks.
@@ -190,25 +203,31 @@ module Marmot
190
203
#
191
204
# This blocks until `#stop` is called or all tasks are cancelled.
192
205
def run : Nil
193
- @@stopped = false
206
+ Log .debug { " Marmot running" }
207
+
208
+ @@running = true
194
209
@@stop_channel = Channel (Nil ).new
195
210
remove_canceled_tasks
196
211
197
212
if @@tasks .size == 0
213
+ Log .debug { " No task to run! Stopping." }
198
214
return
199
215
end
200
216
201
217
@@tasks .map(& .start)
202
218
203
- while ! @@stopped
219
+ while @@running
220
+ Log .debug { " Waiting for a task to run among #{ @@tasks .size} tasks" }
204
221
begin
205
- m = Channel .receive_first([@@stop_channel ] + @@tasks .map(& .tick))
222
+ task = Channel .receive_first([@@stop_channel ] + @@tasks .map(& .tick))
206
223
rescue Channel ::ClosedError
224
+ Log .debug { " Marmot stopped" }
207
225
break
208
226
end
209
227
210
- if m.is_a?(Task )
211
- m.run
228
+ if task.is_a?(Task )
229
+ Log .debug { " Running task #{ task } " }
230
+ task.run
212
231
end
213
232
214
233
remove_canceled_tasks
@@ -220,8 +239,8 @@ module Marmot
220
239
221
240
# Stops scheduling the tasks.
222
241
def stop
223
- if ! @@stopped
224
- @@stopped = true
242
+ if @@running
243
+ @@running = false
225
244
@@stop_channel .close
226
245
end
227
246
end
0 commit comments