Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created November 1, 2012 19:49
Show Gist options
  • Save havenwood/3996000 to your computer and use it in GitHub Desktop.
Save havenwood/3996000 to your computer and use it in GitHub Desktop.
Maglev Queue
require 'maglev/rcqueue'
Store = Maglev::PERSISTENT_ROOT
Maglev.abort_transaction
Store[:queue] ||= RCQueue.new
20.times do |t|
Store[:queue] << Proc.new do
sleep 1
puts "job ##{t}"
end
end
Maglev.commit_transaction
loop do
Maglev.abort_transaction
until Store[:queue].empty?
Thread.new do
Store[:queue].shift.call
end
Maglev.commit_transaction
end
sleep 0.5
end
# Add more tasks from another instance of maglev irb:
Store = Maglev::PERSISTENT_ROOT
Maglev.abort_transaction
5.times do
Store[:queue] << Proc.new do
sleep 1
puts 'another task!'
end
end
Maglev.commit_transaction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment