Resque is widely used Redis-backed Ruby library for creating background jobs. One thing we thought Resque missed, is killing a forked child of Resque worker based on consumed memories.
resque-worker-killer gem provides automatic kill of a forked child of Resque worker based on process memory size (RSS) not to exceed the maximum allowed memory size.
The name was inspired by unicorn-worker-killer :-p
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install resque-worker-killer
Use the plugin:
require 'resque' require 'resque-worker-killer' class MyJob extend :::: @queue = :example_queue extend :::: @worker_killer_monitor_interval = 0.5 # sec @worker_killer_mem_limit = 300_000 # KB @worker_killer_max_term = 10 # try TERM 10 times, then KILL @worker_killer_verbose = false # verbose log @worker_killer_logger = .logger def self.perform(*args) puts 'started' sleep 10 puts 'finished' rescue ::TermException => e # env TERM_CHILD=1 puts 'terminated' end end
Resque requires to set
TERM_CHILD environment variable to accept killing a forked child with SIGTERM.
$ TERM_CHILD=1 bundle exec rake resque:work
@worker_killter_max_term = 0to send SIGKILL immediately
@worker_killer_max_term > 0to try SIGTERM first, then SIGKILL
Resque::TermException is raised if a forked child is killed by SIGTERM.
@worker_killer_monitor_interval: Monotring interval to check RSS size (default: 1.0 sec)
@worker_killer_mem_limit: RSS usage limit, in killobytes (default: 300MB)
@worker_killer_max_term: Try kiling child process with SIGTERM in
max_termtimes, then SIGKILL if it still does not die. Please note that setting
TERM_CHILDenvironment variable is required to accept killing the child with SIGTERM. The default is, 10 with
TERM_CHILD=1, 0 without
@worker_killer_verbose: Verbose log
@worker_killer_logger: Logger instance (default: Resque.logger)
Bug reports and pull requests are welcome on GitHub at https://github.com/sonots/resque-worker-killer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.