Skip to content

Commit

Permalink
Fixed bug where auto_run would only run tasks once (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralsina committed Jul 4, 2023
1 parent 9006075 commit 7198f55
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Fixed bug saving .croupier, was missing all inputs
* Added tests for `TaskManager.save_run`
* Fixed bug in inotify watcher path lookup
* Fixed bug where auto_run would only run tasks once

## Version 0.3.3

Expand Down
23 changes: 23 additions & 0 deletions spec/croupier_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,29 @@ describe "TaskManager" do
end
end

it "should run on every modification of inputs" do
with_scenario("basic") do
TaskManager.auto_run(targets: ["output3"])
# At this point output3 doesn't exist
File.exists?("output3").should be_false
# This triggers building output3
File.open("input", "w") << "bar1"
# The timing here is tricky, we need to wait longer
# than the watch interval, but not too long because
# that makes the test slow
sleep 0.02.seconds
File.exists?("output3").should be_true
# We delete things, and then trigger another build
File.delete("output3")
File.delete("input")
File.open("input", "w") << "bar2"
Fiber.yield
sleep 0.02.seconds
TaskManager.auto_stop
File.exists?("output3").should be_true
end
end

it "should not be triggered by deps for not specified targets" do
with_scenario("basic") do
TaskManager.auto_run(targets: ["output5"])
Expand Down
17 changes: 11 additions & 6 deletions src/croupier.cr
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,6 @@ module Croupier
t = tasks.fetch(output, nil)
next if t.nil? || finished.includes?(t)
next unless run_all || t.stale? || t.@always_run

Log.debug { "Running task for #{output}" }
begin
t.run unless dry_run
Expand Down Expand Up @@ -525,10 +524,13 @@ module Croupier
# stop order and break the loop without running, so
# we can't see the side effects without sleeping in
# the tests.
sleep 0.1.seconds
sleep 0.01.seconds
next if @queued_changes.empty?
Log.info { "Detected changes in #{@queued_changes}" }
self.modified += @queued_changes
# Mark all targets as stale
targets.each { |t| tasks[t].stale = true }
@modified += @queued_changes
Log.debug { "Modified: #{@modified}" }
run_tasks(targets: targets)
# Only clean queued changes after a successful run
@queued_changes.clear
Expand Down Expand Up @@ -560,13 +562,16 @@ module Croupier

@@watcher.on_event do |event|
# It's a file we care about, add it to the queue
Log.debug { "Detected change in #{event.path}" }
Log.trace { "Event: #{event}" }
path = event.name || event.path
Log.debug { "Detected change in #{path}" }
Log.trace { "Event: #{event}" }
@queued_changes << path.to_s if target_inputs.includes? path.to_s
end

watch_flags = LibInotify::IN_CLOSE_WRITE | LibInotify::IN_CREATE | LibInotify::IN_MODIFY
watch_flags = LibInotify::IN_DELETE |
LibInotify::IN_CREATE |
LibInotify::IN_MODIFY |
LibInotify::IN_CLOSE

target_inputs.each do |input|
if File.exists? input
Expand Down

0 comments on commit 7198f55

Please sign in to comment.