Skip to content

Commit

Permalink
Refactored template_sidekiq method
Browse files Browse the repository at this point in the history
  • Loading branch information
rstrobl committed Jun 25, 2015
1 parent 94e08ab commit 8f8d04b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ If you need change some config in redactor, you can
- [andreygerasimchuk] (https://github.com/andreygerasimchuk)
- [Saicheg] (https://github.com/Saicheg)
- [Alex Yakubenko] (https://github.com/alexyakubenko)
- [Robert Strobl] (https://github.com/rstrobl)

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion lib/capistrano/tasks/monit.cap
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace :sidekiq do
task :config do
on roles(fetch(:sidekiq_role)) do |role|
@role = role
template_sidekiq 'sidekiq_monit', "#{fetch(:tmp_dir)}/monit.conf", @role
upload_sidekiq_template 'sidekiq_monit', "#{fetch(:tmp_dir)}/monit.conf", @role

mv_command = "mv #{fetch(:tmp_dir)}/monit.conf #{fetch(:sidekiq_monit_conf_dir)}/#{sidekiq_service_name}.conf"

Expand Down
43 changes: 23 additions & 20 deletions lib/capistrano/tasks/sidekiq.cap
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace :load do
end
end


namespace :deploy do
before :starting, :check_sidekiq_hooks do
invoke 'sidekiq:add_default_hooks' if fetch(:sidekiq_default_hooks)
Expand Down Expand Up @@ -204,25 +203,29 @@ namespace :sidekiq do
end
end

def template_sidekiq(from, to, role)
[
"#{fetch(:sidekiq_monit_templates_path)}/#{from}.erb",
File.join('lib', 'capistrano', 'templates', "#{from}-#{role.hostname}-#{fetch(:stage)}.conf.rb"),
File.join('lib', 'capistrano', 'templates', "#{from}-#{role.hostname}-#{fetch(:stage)}.conf.rb"),
File.join('lib', 'capistrano', 'templates', "#{from}-#{role.hostname}.conf.rb"),
File.join('lib', 'capistrano', 'templates', "#{from}-#{fetch(:stage)}.conf.rb"),
File.join('lib', 'capistrano', 'templates', "#{from}.conf.rb.erb"),
File.join('lib', 'capistrano', 'templates', "#{from}.conf.rb"),
File.join('lib', 'capistrano', 'templates', "#{from}.conf.erb"),
File.expand_path("../../../generators/capistrano/sidekiq/monit/templates/#{from}.conf.rb.erb", __FILE__),
File.expand_path("../../../generators/capistrano/sidekiq/monit/templates/#{from}.conf.erb", __FILE__)
].each do |path|
if File.file?(path)
erb = File.read(path)
upload! StringIO.new(ERB.new(erb).result(binding)), to
break
end
end
def upload_sidekiq_template(from, to, role)
template = sidekiq_template(from, role)
upload!(StringIO.new(ERB.new(template).result(binding)), to)
end

def sidekiq_template(name, role)
local_template_directory = fetch(:sidekiq_monit_templates_path)

search_paths = [
"#{name}-#{role.hostname}-#{fetch(:stage)}.erb",
"#{name}-#{role.hostname}.erb",
"#{name}-#{fetch(:stage)}.erb",
"#{name}.erb"
].map { |filename| File.join(local_template_directory, filename) }

global_search_path = File.expand_path(
File.join(*%w[.. .. .. generators capistrano sidekiq monit templates], "#{name}.conf.erb"),
__FILE__
)

search_paths << global_search_path

template_path = search_paths.detect { |path| File.file?(path) }
File.read(template_path)
end
end

0 comments on commit 8f8d04b

Please sign in to comment.