Last active
June 2, 2017 05:34
-
-
Save jackwillis/4b65779371e0c25be8e6e7ec8bc7c8dc to your computer and use it in GitHub Desktop.
Concurrent mini-Rake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'concurrent' | |
TASKS = Concurrent::Map.new | |
class Task | |
def initialize(ref, dep_refs = [], &action) | |
@ref = ref | |
@dep_refs = dep_refs | |
@promise = Concurrent::Promise.new(&action) | |
end | |
def dependencies | |
@dep_refs.map { |ref| TASKS[ref] } | |
end | |
def meta_promise | |
Concurrent::Promise.zip(*dependencies.map(&:meta_promise), @promise) | |
end | |
def invoke | |
meta_promise.wait | |
end | |
end | |
def task(ref, dep_refs = [], &action) | |
TASKS[ref] = Task.new(ref, dep_refs, &action) | |
end | |
############################################## | |
def work(msg) | |
sleep rand | |
puts msg | |
end | |
task :serve, [:mac] do | |
puts 'Serving mac & cheese' | |
end | |
task :mac, [:ingredients, :boil] do | |
puts 'Making mac & cheese' | |
end | |
task :cheese do | |
work 'Getting cheese, butter and milk' | |
end | |
task :pasta do | |
work 'Getting pasta' | |
end | |
task :ingredients, [:cheese, :pasta] do | |
puts 'Got ingredients' | |
end | |
task :boil, [:pour, :stove, :ingredients] do | |
work 'Boiling water' | |
end | |
task :pour do | |
work 'Pouring water into the pot' | |
end | |
task :stove do | |
work 'Check that the stove is working' | |
end | |
TASKS[:serve].invoke |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment