Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
seuros committed Mar 12, 2014
0 parents commit 07b9d97
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
.idea
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in capistrano-sidekiq.gemspec
gemspec
5 changes: 5 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Copyright (c) Abdelkader Boudih

capistrano-sidekiq is an Open Source project licensed under the terms of
the LGPLv3 license. Please see <http://www.gnu.org/licenses/lgpl-3.0.html>
for license text.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Capistrano::Sidekiq

Sidekiq integration for Capistrano

## Installation

Add this line to your application's Gemfile:

gem 'capistrano-sidekiq' , github: 'seuros/capistrano-sidekiq'


And then execute:

$ bundle

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require "bundler/gem_tasks"
21 changes: 21 additions & 0 deletions capistrano-sidekiq.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'capistrano/sidekiq/version'

Gem::Specification.new do |spec|
spec.name = "capistrano-sidekiq"
spec.version = Capistrano::Sidekiq::VERSION
spec.authors = ["Abdelkader Boudih"]
spec.email = ["[email protected]"]
spec.summary = %q{Sidekiq integration for Capistrano}
spec.description = %q{Sidekiq integration for Capistrano}
spec.homepage = 'https://github.com/seuros/capistrano-sidekiq'
spec.license = "LGPL-3.0"

spec.files = `git ls-files`.split($/)
spec.require_paths = ['lib']

spec.add_dependency 'capistrano'
spec.add_dependency 'sidekiq'
end
Empty file added lib/capistrano-sidekiq.rb
Empty file.
5 changes: 5 additions & 0 deletions lib/capistrano/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if Gem::Version.new(Capistrano::VERSION).release >= Gem::Version.new('3.0.0')
load File.expand_path('../tasks/sidekiq.cap', __FILE__)
else
require_relative 'tasks/capistrano2'
end
5 changes: 5 additions & 0 deletions lib/capistrano/sidekiq/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Capistrano
module Sidekiq
VERSION = '0.0.1'
end
end
54 changes: 54 additions & 0 deletions lib/capistrano/tasks/capistrano2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Capistrano::Configuration.instance.load do

_cset(:sidekiq_default_hooks) { true }
_cset(:sidekiq_cmd) { "#{fetch(:bundle_cmd, "bundle")} exec sidekiq" }
_cset(:sidekiqctl_cmd) { "#{fetch(:bundle_cmd, "bundle")} exec sidekiqctl" }
_cset(:sidekiq_timeout) { 10 }
_cset(:sidekiq_role) { :app }
_cset(:sidekiq_pid) { "#{current_path}/tmp/pids/sidekiq.pid" }
_cset(:sidekiq_processes) { 1 }

if fetch(:sidekiq_default_hooks)
before 'deploy:update_code', 'sidekiq:quiet'
after 'deploy:stop', 'sidekiq:stop'
after 'deploy:start', 'sidekiq:start'
before 'deploy:restart', 'sidekiq:restart'
end

namespace :sidekiq do
def for_each_process(&block)
fetch(:sidekiq_processes).times do |idx|
yield((idx == 0 ? "#{fetch(:sidekiq_pid)}" : "#{fetch(:sidekiq_pid)}-#{idx}"), idx)
end
end

desc 'Quiet sidekiq (stop accepting new work)'
task :quiet, :roles => lambda { fetch(:sidekiq_role) }, :on_no_matching_servers => :continue do
for_each_process do |pid_file, idx|
run "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} quiet #{pid_file} ; else echo 'Sidekiq is not running'; fi"
end
end

desc 'Stop sidekiq'
task :stop, :roles => lambda { fetch(:sidekiq_role) }, :on_no_matching_servers => :continue do
for_each_process do |pid_file, idx|
run "if [ -d #{current_path} ] && [ -f #{pid_file} ] && kill -0 `cat #{pid_file}`> /dev/null 2>&1; then cd #{current_path} && #{fetch(:sidekiqctl_cmd)} stop #{pid_file} #{fetch :sidekiq_timeout} ; else echo 'Sidekiq is not running'; fi"
end
end

desc 'Start sidekiq'
task :start, :roles => lambda { fetch(:sidekiq_role) }, :on_no_matching_servers => :continue do
rails_env = fetch(:rails_env, 'production')
for_each_process do |pid_file, idx|
run "cd #{current_path} ; nohup #{fetch(:sidekiq_cmd)} -e #{rails_env} -i #{idx} -P #{pid_file} >> #{current_path}/log/sidekiq.log 2>&1 &", :pty => false
end
end

desc 'Restart sidekiq'
task :restart, :roles => lambda { fetch(:sidekiq_role) }, :on_no_matching_servers => :continue do
stop
start
end

end
end
92 changes: 92 additions & 0 deletions lib/capistrano/tasks/sidekiq.cap
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
namespace :load do
task :defaults do
set :sidekiq_default_hooks, -> { true }

set :sidekiq_pid, -> { File.join(shared_path, 'tmp', 'pids', 'sidekiq.pid') }
set :sidekiq_env, -> { fetch(:rack_env, fetch(:rails_env, fetch(:stage))) }
set :sidekiq_log, -> { File.join(shared_path, 'log', 'sidekiq.log') }

# "-d -i INT -P PATH" are added automatically.
set :sidekiq_options, -> { "-e #{fetch(:sidekiq_env)} -L #{fetch(:sidekiq_log)}" }

set :sidekiq_timeout, -> { 10 }
set :sidekiq_role, -> { :app }
set :sidekiq_processes, -> { 1 }
# Rbenv and RVM integration
set :rbenv_map_bins, fetch(:rbenv_map_bins).to_a.concat(%w{ sidekiq sidekiqctl })
set :rvm_map_bins, fetch(:rvm_map_bins).to_a.concat(%w{ sidekiq sidekiqctl })
end

end

namespace :deploy do
before :starting, :check_sidekiq_hooks do
invoke 'sidekiq:add_default_hooks' if fetch(:sidekiq_default_hooks)
end
end

namespace :sidekiq do
def for_each_process(&block)
fetch(:sidekiq_processes).times do |idx|
yield((idx.zero? ? "#{fetch(:sidekiq_pid)}" : "#{fetch(:sidekiq_pid).gsub('.pid', "-#{idx}.pid")}"), idx)
end
end

task :add_default_hooks do
after 'deploy:starting', 'sidekiq:quiet'
after 'deploy:updated', 'sidekiq:stop'
after 'deploy:reverted', 'sidekiq:stop'
after 'deploy:published', 'sidekiq:start'
end

desc 'Quiet sidekiq (stop processing new tasks)'
task :quiet do
on roles fetch(:sidekiq_role) do
for_each_process do |pid_file, idx|
if test("[ -f #{pid_file} ]") and test("kill -0 $( cat #{pid_file} )")
within current_path do
execute :bundle, :exec, :sidekiqctl, 'quiet', "#{pid_file}"
end
end
end
end
end

desc 'Stop sidekiq'
task :stop do
on roles fetch(:sidekiq_role) do
for_each_process do |pid_file, idx|
if test("[ -f #{pid_file} ]") and test("kill -0 $( cat #{pid_file} )")
within current_path do
execute :bundle, :exec, :sidekiqctl, 'stop', "#{pid_file}", fetch(:sidekiq_timeout)
end
end
end
end
end

desc 'Start sidekiq'
task :start do
on roles fetch(:sidekiq_role) do
within current_path do
for_each_process do |pid_file, idx|
command = "-i #{idx} -P #{pid_file} #{fetch(:sidekiq_options)}"
if defined?(JRUBY_VERSION)
command = "#{command} >/dev/null 2>&1 &"
warn 'Since JRuby doesn\'t support Process.daemon, Sidekiq will be running without the -d flag.'
else
command = "-d #{command}"
end
execute :bundle, :exec, :sidekiq, command
end
end
end
end

desc 'Restart sidekiq'
task :restart do
invoke 'sidekiq:stop'
invoke 'sidekiq:start'
end

end

0 comments on commit 07b9d97

Please sign in to comment.