As more and more web applications integrate with third-party APIs and other external data, processing those external resources in the background more and more important. A simple job runner is a great start, however as your load increases, you very quickly outgrew that simplistic queuing system. We will cover where getting started using Resque and Redis, how to test your jobs, when it makes sense to use Resque, implementations of Resque in other languages, and look how I've used Resque.
5. Resque
https://github.com/defunkt/resque
• Open-source library for creating background
jobs
• Redis backend
• Support for multiple queues
• Queues are processed by workers
6. More Resque Features
• Monitor with god or monit
• Distributed processing
• Priorities
• Persistent queues
• Memory leak resistant
• Web interface
8. Redis
http://redis.io/
• Key-value datastore
• data structure server
• constant time pushes and pops
• atomic operations
9. All “Foreground”
class FeedsController
def create
feed = Feed.create!
feed.fetch
redirect_to feed_path(feed)
end
end
class Feed
def fetch
rss = RSS::Parse.parse(url, false)
feed.title = rss.title
end
end
10. Queue up creation
class FeedsController
def create
feed = Feed.create!
Queue.push(feed) # won’t block UI
redirect_to feed_path(feed)
end
end
class Queue
def push(feed)
queue << feed
end
end
11. Now with Resque
class FeedsController
def create
feed = Feed.create!
Resque.enqueue(FeedCreator, feed.id)
redirect_to feed_path(feed)
end
end
12. How to process jobs
class FeedCreator
@queue = :feed_creation
def perform(feed_id)
feed = Feed.find(feed_id)
rss = RSS::Parser.parse(feed.url,false)
feed.title = rss.title
# create all items in the rss
end
end
19. GitHub
35 different types of jobs:
• Warming caches
• Counting disk usage
• Building tarballs
• Building Rubygems
• Building graphs
• Updating search index
20. Groupon
• Resque for background jobs
• Notifications
• Lockable worker
• Redis for semaphores
24. Examples
it “adds a feed to the creation queue” do
post :create, :feed
FeedCreator.should have_queued(feed.id)
end
it “schedules a feed update” do
feed.update
FeedUpdater.should have_scheduled(feed.id)
end
25. Resque Heroku
Autoscaler
• Scales Heroku workers
• Start worker if none running
• Stops worker after jobs are processed
28. Resque Mailer
class MyMailer < ActionMailer::Base
include Resque::Mailer
end
MyMailer.subject_email(params).deliver
QUEUE=mailer rake environment resque:work
33. jesque
// Add a job to the queue
final Job job = new Job("TestAction",
new Object[]{ 1, 2, true, "test", Arrays.asList("inner",
4ß)});
final Client client = new ClientImpl(config);
client.enqueue("foo", job);
client.end();
// Start a worker to run jobs from the queue
final Worker worker = new WorkerImpl(config,
Arrays.asList("foo"), Arrays.asList(TestAction.class));
final Thread workerThread = new Thread(worker);
workerThread.start();
Software consulting company in Chicago, IL.\n\nPrimarily RoR, but have mobile, Java, .NET project from time to time\n\nAlways looking for good developers. Interested? Find me after the talk\n
\n
Useful for cpu- and network-intensive tasks\n\nProcesses that run on an interval\n
queue examples: \n* one for each email type; \n* one for updating feeds, one for posting to twitter\n\n
priority is assigned per-worker; specified by queue\n\nMemory leak -> worker forks a child process for every job\n
\n
Resque uses Redis as its database backend\n\ndata structure: string, hash, list, set\n\nlinked list gives great performance for insert/retrieve at head and tail\n\natomic operations: one action won&#x2019;t stomp on another\n
UI will block for each feed creation\nunder load, consume a ton of resources (each call to the controller forks a process???)\n
\n
enqueue takes a Ruby class and any number of arguments after that\n\nargs passed as JSON; can&#x2019;t pass entire objects, pass an id\n
\n
5 seconds is the default\n
\n
processors are POROs so testing is pretty straightforward\ntest side effects\n
\n
\n
\n
copies from Resque&#x2019;s README\n
semaphore - ensures a deal is still able to sold\n\natomic value of deal sales remaining across processess\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
\n
Ethan&#x2019;s geekfest on nosql db&#x2019;s - choose the right solution for the job\n\n
I picked Resque as a learning tool\n\nBreakable Toys\n