Skip to content

Instantly share code, notes, and snippets.

@jackwillis
Last active June 2, 2017 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackwillis/4b65779371e0c25be8e6e7ec8bc7c8dc to your computer and use it in GitHub Desktop.
Save jackwillis/4b65779371e0c25be8e6e7ec8bc7c8dc to your computer and use it in GitHub Desktop.
Concurrent mini-Rake
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