Skip to content

Instantly share code, notes, and snippets.

@leopd
Created October 25, 2012 17:12
Show Gist options
  • Save leopd/3954088 to your computer and use it in GitHub Desktop.
Save leopd/3954088 to your computer and use it in GitHub Desktop.
Chef recipe to run gunicorn application and gearman workers. Posted for discussion about notification problems. This is probably not a good example to follow. :)
#
# Cookbook Name:: imgmuck
# Recipe:: default
#
# Copyright 2012, Scaled Recognition, all rights reserved
#
include_recipe "git"
include_recipe "python"
include_recipe "application"
include_recipe "supervisor"
directory "/srv/imgmuck/shared" do
mode '0755'
owner "nobody"
group "nogroup"
recursive true
end
supervisor_service "srapiworker" do
action :enable
autostart true
user "nobody"
process_name "srapiworker%(process_num)s"
directory "/srv/imgmuck/current"
command "python sr/pipeline/worker.py"
numprocs node['apiserver']['numworkers']
environment ({
"PYTHONPATH" => "/srv/imgmuck/current"
})
end
supervisor_service "imgmuck" do
# Create a stub service so I can notify it.
action :nothing
end
template "/srv/imgmuck/shared/local_serverconfig.py" do
#TODO: Get address_list from a chef server search instead of data bag.
address_list = data_bag_item('server_locations','gearmand_address')[node.chef_environment]
if !address_list
raise NameError, "No gearmand address list found in environment #{node.chef_environment} in data bag #{data_bag_item('server_locations','gearmand_address')}."
end
source "local_serverconfig.py.erb"
mode 0644
owner "nobody"
group "nogroup"
variables({
:gearmand_address => address_list
})
# Notify the running jobs that they need to restart with new config
notifies :restart, "supervisor_service[srapiworker]", :immediate
notifies :restart, "supervisor_service[imgmuck]", :immediate
end
application "imgmuck" do
repository node['apiserver']['gitrepos']['imgmuck']
revision node['apiserver']['gitbranches']['imgmuck']
deploy_key data_bag_item('deploykeys','imgmuck')['deploy_key']
path "/srv/imgmuck"
owner "nobody"
group "nogroup"
gunicorn do
app_module node['apiserver']['gunicorn']['app_module']
port node['apiserver']['gunicorn']['port']
workers node['apiserver']['gunicorn']['workers']
timeout node['apiserver']['gunicorn']['timeout']
end
symlinks ({
"local_serverconfig.py" => "local_serverconfig.py"
})
# Below are several unsuccessful attempts to notify the worker jobs to restart
# on code pushes. None of them seem to do anything. Go figure.
#
#notifies :restart, "supervisor_service[srapiworker]"
#
#before_restart do
# notifies :restart, "supervisor_service[srapiworker]"
#end
#
#after_restart do
# notifies :restart, "supervisor_service[srapiworker]"
#end
end
supervisor_service "srapiworker" do
action :nothing
subscribes :restart, "deploy_revision[imgmuck]", :immediate
# Note the :immediate is needed or else the action just gets swallowed. Bizarre.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment