Skip to content

Instantly share code, notes, and snippets.

View bleonard's full-sized avatar

Brian Leonard bleonard

View GitHub Profile
class HardWorker
include TResque::Worker
inputs :name, :count
def work
# do something with self.name, self.count
end
end
# enqueue
class HardWorker
include Sidekiq::Worker
def perform(name, count)
# do something with name, count
end
end
# enqueue
HardWorker.perform_async('bob', 5)
# Whenever the user changes
subscribe 'user_may_have_changed', bus_observer_touched: 'user' do |attributes|
# update the profile in ElasticSearch
ProfileStoreWorker.enqueue(user_id: attributes['id'])
end
class JobsController < ApplicationController
def confirm
@job = Job.find(params[:id])
authorize @job, :confirm? # authorization
op = Organic::JobConfirmOp.new(current_user)
op.submit!(params.merge(job_id: @job.id)) # perform action
render :show # render template
end
end
class InvoiceJobOp < ::Backend::Op
include Mixins::AtomicOperation # all in same transaction
field :hours
field :job_id
validates :job_id, presence: true
validate :validate_hour # hours given
validate :validate_assignment # tasker is assigned
# ... other checks
class Main
def process_all
mutex = Mutex.new
queue = self.paths.dup
self.thread_count.times.map {
Thread.new do
while path = mutex.synchronize { queue.pop }
fetcher = ThreadedFetcher.new(path)
fetcher.fetch!
class Fetcher
def initialize(path)
@path = path
end
def fetch!
VCR.use_cassette('#{@path}/fetched') do
response = Net::HTTP.get_response(URI("api.http://example.com/#{@path}"))
process(response)
end
class VCR::Client
def current_cassette
cassetes.last
end
def configure
yield configuration
end
def configuration
module VCR
extend self
def current_cassette
cassetes.last
end
def configure
yield configuration
end
<head>
<title>NewName</title>
</head>