Skip to content

Instantly share code, notes, and snippets.

@zxiest
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save zxiest/4b13cb24b0fca5a3cf75 to your computer and use it in GitHub Desktop.

Select an option

Save zxiest/4b13cb24b0fca5a3cf75 to your computer and use it in GitHub Desktop.
require 'celluloid'
require 'active_job'
class MasterQueue
include Celluloid
attr_accessor :jobs, :current_job
def initialize
@jobs = Queue.new
@current_job = nil
@timer = every(1) {
if !@jobs.empty? && (@current_job.nil? || @current_job.complete)
@current_job = @jobs.pop
@current_job.perform
end
puts "fired timer. jobs: #{@jobs.size}"
}
end
def push_job(job)
@jobs << job
end
end
class MyJob < ActiveJob::Base
attr_accessor :name
attr_accessor :complete
def initialize(name)
@name = name
end
def perform
sleep( rand(1) )
puts "performing: #{@name}"
@complete = true
end
end
queue = MasterQueue.new
10.times { |i|
queue.push_job( MyJob.new(i) )
}
while(true)
sleep(5)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment