https://github.com/samuelgiles/lazy_resque
Move Resque enqueues out of the Rails request cycle to help decrease time to first byte.
https://github.com/samuelgiles/lazy_resque
rails resque ruby
Last synced: 3 months ago
JSON representation
Move Resque enqueues out of the Rails request cycle to help decrease time to first byte.
- Host: GitHub
- URL: https://github.com/samuelgiles/lazy_resque
- Owner: samuelgiles
- Created: 2018-06-14T18:09:26.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-14T19:37:38.000Z (over 7 years ago)
- Last Synced: 2024-04-26T07:02:06.194Z (over 1 year ago)
- Topics: rails, resque, ruby
- Language: Ruby
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README

# Lazy Resque
Move Resque enqueues out of the Rails request cycle to help decrease time to first byte.
## Install
From gem:
```
gem 'lazy_resque'
```From source:
```
gem 'lazy_resque', git: 'git@github.com:samuelgiles/lazy_resque.git'
```## Use
Trigger the actual enqueue of the jobs after the request in an `after_action` block, this can be done
by including `LazyResque::ControllerEnqueue` which will automatically add the `after_action` for you.```ruby
class ApplicationController < ActionController::Base
include LazyResque::ControllerEnqueuedef index
Resque.lazy_enqueue(MyResqueJob, 1234, 'some_job_data')
end
end
```## Why?
Though moving a long running task to a Resque job is beneficial the actual enqueuing process isn't free:
- Any `before_enqueue` hooks are run
- A new job instance is initialized and validated
- Arguments are dumped/encoded into JSON
- Redis `rpush` takes places requiring communication with the Redis serverThis is fine for 1-2 enqueues in a request cycle but can add up to a measurable amount of time if your enqueuing several jobs.
You don't lose anything by moving the actual enqueuing outside the request cycle and it saves precious milliseconds.
## How?

Internally when you call `Resque.lazy_enqueue` the job class and arguments are stored in an array on a per request thread variable, then at the end of the request via the `after_action` callback the queue is looped through and the jobs are actually sent to Resque.