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

View File

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