Skip to content

Commit 9bb8169

Browse files
committedDec 27, 2020
Add debug logs, use env var MARMOT_DEBUG to show them
1 parent d89edba commit 9bb8169

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed
 

‎src/marmot.cr

+36-17
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,18 @@ module Marmot
3434

3535
alias Callback = Proc(Task, Nil)
3636

37+
Log = ::Log.for(self)
38+
if level = ENV["MARMOT_DEBUG"]?
39+
Log.level = ::Log::Severity::Debug
40+
end
41+
3742
@@tasks = Array(Task).new
38-
@@stopped = true
43+
@@running = false
3944
@@stop_channel = Channel(Nil).new
4045

4146
abstract class Task
47+
Log = Marmot::Log.for(self, Marmot::Log.level)
48+
4249
@canceled = false
4350
@callback : Callback = ->(t : Task) {}
4451

@@ -48,6 +55,7 @@ module Marmot
4855
#
4956
# A canceled task cannot be uncanceled.
5057
def cancel
58+
Log.debug { "Task #{self} canceled" }
5159
@canceled = true
5260
end
5361

@@ -135,27 +143,34 @@ module Marmot
135143
end
136144

137145
class RepeatTask < Task
146+
Log = Task::Log.for(self, Task::Log.level)
147+
138148
def initialize(@span : Time::Span, @first_run : Bool, @callback : Callback)
139149
end
140150

141151
protected def wait_next_tick : Nil
142152
if @first_run
143153
@first_run = false
144154
else
155+
Log.debug { "Task #{self} sleeping #{@span}" }
145156
sleep @span
146157
end
147158
end
148159
end
149160

150161
extend self
151162

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
155164
@@tasks << task
156165
task
157166
end
158167

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+
159174
# Runs a task when a value is received on a channel.
160175
#
161176
# To access the value, you need to restrict the type of the task, and use
@@ -166,19 +181,17 @@ module Marmot
166181
# Marmot.on(channel) { |task| puts task.as(OnChannelTask).value }
167182
# ```
168183
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)
172186
end
173187

174188
# Runs a task every given *span*.
175189
#
176190
# If first run is true, it will run as soon as the scheduler runs.
177191
# Else it will wait *span* time before running for first time.
178192
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)
182195
end
183196

184197
# Cancels all the tasks.
@@ -190,25 +203,31 @@ module Marmot
190203
#
191204
# This blocks until `#stop` is called or all tasks are cancelled.
192205
def run : Nil
193-
@@stopped = false
206+
Log.debug { "Marmot running" }
207+
208+
@@running = true
194209
@@stop_channel = Channel(Nil).new
195210
remove_canceled_tasks
196211

197212
if @@tasks.size == 0
213+
Log.debug { "No task to run! Stopping." }
198214
return
199215
end
200216

201217
@@tasks.map(&.start)
202218

203-
while !@@stopped
219+
while @@running
220+
Log.debug { "Waiting for a task to run among #{@@tasks.size} tasks" }
204221
begin
205-
m = Channel.receive_first([@@stop_channel] + @@tasks.map(&.tick))
222+
task = Channel.receive_first([@@stop_channel] + @@tasks.map(&.tick))
206223
rescue Channel::ClosedError
224+
Log.debug { "Marmot stopped" }
207225
break
208226
end
209227

210-
if m.is_a?(Task)
211-
m.run
228+
if task.is_a?(Task)
229+
Log.debug { "Running task #{task}" }
230+
task.run
212231
end
213232

214233
remove_canceled_tasks
@@ -220,8 +239,8 @@ module Marmot
220239

221240
# Stops scheduling the tasks.
222241
def stop
223-
if !@@stopped
224-
@@stopped = true
242+
if @@running
243+
@@running = false
225244
@@stop_channel.close
226245
end
227246
end

0 commit comments

Comments
 (0)