Replace homebrew scheduling logic with Rufus

This commit is contained in:
Bill Rossi 2025-08-17 06:46:32 -04:00
parent 9f6e2e059a
commit a85bd2b5ab
2 changed files with 22 additions and 22 deletions

View File

@ -1,3 +1,5 @@
require "rufus-scheduler"
DEFAULT_LESSON_PLAN = [
[15, "login and catch up on the week"],
[45, "review chapter vocab and grammar"],
@ -6,30 +8,34 @@ DEFAULT_LESSON_PLAN = [
]
class Lesson
attr_reader :lesson_plan, :channel, :thread, :running
attr_reader :lesson_plan, :channel, :thread, :running, :scheduler
def initialize(lesson_plan=DEFAULT_LESSON_PLAN)
def initialize(lesson_plan=DEFAULT_LESSON_PLAN, scheduler: Rufus::Scheduler.new)
@lesson_plan = lesson_plan
@running = false
@scheduler = scheduler
end
def stop!
thread.kill if thread
@running = false
@scheduler.shutdown
end
def start!(channel)
stop!
@channel = channel
@thread = Thread.new do
@running = true
lesson_plan.each do |time, message|
lesson_plan.each_with_index do |(time, message), index|
offset = lesson_plan.map(&:first)[0..index - 1].sum(0)
offset = 0 if index == 0
scheduler.in "#{offset}m" do
channel.send_message "#{time} minutes to #{message}"
sleep(time * 60)
end
end
end_offset = lesson_plan.map(&:first).sum(0)
scheduler.in "#{end_offset}m" do
channel.send_message "Lesson over!"
@running = false
end
@running = true
end
def running?

View File

@ -3,20 +3,14 @@ require "minitest/autorun"
class TestLesson < Minitest::Test
def setup
@lesson = Lesson.new [[0.1 / 60, "test the thing"], [0.1 / 60, "finish testing"]]
@scheduler = Minitest::Mock.new
end
def test_start
@channel = Minitest::Mock.new
@channel.expect(:send_message, nil) { |msg| msg.include? "to test the thing" }
@channel.expect(:send_message, nil) { |msg| msg.include? "to finish testing" }
@channel.expect(:send_message, nil) { |msg| msg == "Lesson over!" }
assert_equal false, @lesson.running
@lesson.start!(@channel)
sleep(0.5)
# gonna be impossible to thread this needle
# assert_equal true, @lesson.running
sleep(0.5)
assert_equal false, @lesson.running
@lesson = Lesson.new [[1, "test the thing"], [3, "finish testing"]], scheduler: @scheduler
@scheduler.expect(:in, nil) { |time| time == "0m" }
@scheduler.expect(:in, nil) { |time| time == "1m" }
@scheduler.expect(:in, nil) { |time| time == "4m" }
@lesson.start! nil
end
end