-
-
Save sshaw/027f427f6295ee4d9264 to your computer and use it in GitHub Desktop.
SuckerPunch + GDBM: Don't think GDBM can be used with 2 writers...
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 "gdbm" | |
require "sucker_punch" | |
class IdBasedJob | |
include SuckerPunch::Job | |
def perform(db, ids) | |
ids.each do |id| | |
SuckerPunch.logger.info(sprintf "Running %s [%d]\n", id, Thread.current.object_id) | |
if do_some_thang | |
SuckerPunch.logger.info "Success, deleting ID #{id}" | |
db.delete(id) | |
end | |
sleep 3 | |
end | |
end | |
private | |
def do_some_thang | |
rand(10) % 2 == 0 | |
end | |
end | |
class Q | |
extend Forwardable | |
def_delegators :@db, :size, :clear | |
alias :clear! :clear | |
def initialize(db) | |
@db = GDBM.new(db, nil, GDBM::WRCREAT | GDBM::SYNC) | |
end | |
def enqueue(pin) | |
backlog = @db.keys | |
@db[pin.to_s] = Time.now.to_s | |
backlog.unshift pin.to_s | |
IdBasedJob.new.async.perform(@db, backlog) | |
end | |
end | |
q = Q.new("queue") | |
10.times do |i| | |
q.enqueue i | |
end | |
while q.size.nonzero? | |
puts "Queue size: #{q.size}" | |
sleep 2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment