Skip to content
This repository has been archived by the owner on Apr 23, 2019. It is now read-only.

Add cron recipe to allow per-app scheduled tasks. #160

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cheffile
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ cookbook "rails", path: "vendor/cookbooks/rails"
cookbook "ssh_deploy_keys", path: "vendor/cookbooks/ssh_deploy_keys"
cookbook "backups", path: "vendor/cookbooks/backups"
cookbook "sysadmins", path: "vendor/cookbooks/sysadmins"
cookbook "cron", path: "vendor/cookbooks/cron"
6 changes: 6 additions & 0 deletions Cheffile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ PATH
specs:
backups (0.1.0)

PATH
remote: vendor/cookbooks/cron
specs:
cron (0.0.1)

PATH
remote: vendor/cookbooks/packages
specs:
Expand Down Expand Up @@ -97,6 +102,7 @@ PATH
DEPENDENCIES
apt (~> 2.6.1)
backups (>= 0)
cron (>= 0)
mysql (~> 5.3.6)
nginx (~> 2.7.4)
packages (>= 0)
Expand Down
8 changes: 8 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
server_name www.localhost;
return 301 $scheme://localhost:8081$request_uri;
}"
},
"cron" => {
"sitemap" => {
minute: 0,
hour: 5,
command: "rake -s sitemap:refresh"
}
}
}
},
Expand All @@ -127,6 +134,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

chef.add_role "postgresql"
chef.add_role "rails_passenger"
chef.add_recipe "cron"

chef.log_level = :info

Expand Down
2 changes: 2 additions & 0 deletions vendor/cookbooks/cron/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
See https://github.com/intercity/chef-repo/blob/master/CHANGELOG.md for full
changelog
51 changes: 51 additions & 0 deletions vendor/cookbooks/cron/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Cron Cookbook
==================

Schedules tasks for apps. It simply adds entries to the root crontab
with commands that will be ran within the context of the app.

Attributes
----------

#### sysadmins::default
<table>
<tr>
<th>Key</th>
<th>Type</th>
<th>Description</th>
<th>Default</th>
</tr>
<tr>
<td><tt>['cron']></td>
<td>Hash</td>
<td>key: identifier</td>
<td><tt>empty, won't schedule tasks.</tt></td>
</tr>
</table>

Usage
-----

Add cron to your node configuration:

```@json
{
"cron": {
"sitemap": {
"minute": 0,
"hour": 5,
"command": "rake -s sitemap:refresh"
}
}
```
This runs the command `..... bundle exec rake -s sitemap:refresh` every
day at 5:00.

See the full list of available attributes at [the cron resource
documentation](http://docs.chef.io/resource_cron.html).

All attributes will be passed literarally from the node configuration
into the cron resource. With a few exceptions:

* Command will be prefixed with code so it runs within the app current dir.
* User defaults to the deploy\_user for the app ("deploy"). Be carefull when overriding.
Empty file.
7 changes: 7 additions & 0 deletions vendor/cookbooks/cron/metadata.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name "cron"
maintainer "Bèr `berkes` Kessels"
maintainer_email "[email protected]"
license "MIT"
description "Schedules tasks for apps"
long_description IO.read(File.join(File.dirname(__FILE__), "README.md"))
version "0.0.1"
44 changes: 44 additions & 0 deletions vendor/cookbooks/cron/recipes/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Cookbook Name:: cron
# Recipe:: default
#
# Copyright 2014, Bèr `berkes` Kessels
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

return unless node[:active_applications]

node[:active_applications].map do |app, app_info|
app_info["cron"].each do |id, crontab|
attributes = crontab.dup

unscoped_command = attributes.delete("command")
rails_env = app_info['rails_env'] || "production"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

user = attributes.delete("user") || app_info['deploy_user'] || "deploy"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

app_dir = "#{node[:rails][:applications_root]}/#{app}/current"

command = "cd #{app_dir} && ( PATH=/opt/rbenv/shims:$PATH RAILS_ENV=#{rails_env} bundle exec #{unscoped_command} )"

cron "#{app}_#{id}" do
command(command)
user(user)
attributes.each {|attribute, value| send(attribute, value) }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space between { and | missing.

end
end
end