Skip to content

Commit ab3ebf3

Browse files
dysontaketo1113
authored andcommitted
Add description as comment in crontab
Adds a desc to a job that will be output as a comment above the cron entry in the crontab. It supports single line and multiline descriptions.
1 parent 19daf02 commit ab3ebf3

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,26 @@ every 3.hours do
180180
end
181181
```
182182

183+
### Adding comments to crontab
184+
185+
A description can be added to a job that will be included in the crontab as a comment above the cron entry.
186+
187+
Example: A single line description:
188+
189+
```ruby
190+
every 1.hours, description: "My job description" do
191+
command "/usr/bin/my_great_command"
192+
end
193+
```
194+
195+
Example: A multi line description:
196+
197+
```ruby
198+
every 1.hours, description: "My job description\nhas multiple lines" do
199+
command "/usr/bin/my_great_command"
200+
end
201+
```
202+
183203
### Capistrano integration
184204

185205
Use the built-in Capistrano recipe for easy crontab updates with deploys. For Capistrano V3, see the next section.

lib/whenever/job.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Whenever
44
class Job
5-
attr_reader :at, :roles, :mailto
5+
attr_reader :at, :roles, :mailto, :description
66

77
def initialize(options = {})
88
@options = options
@@ -11,6 +11,7 @@ def initialize(options = {})
1111
@mailto = options.fetch(:mailto, :default_mailto)
1212
@job_template = options.delete(:job_template) || ":job"
1313
@roles = Array(options.delete(:roles))
14+
@description = options.delete(:description)
1415
@options[:output] = options.has_key?(:output) ? Whenever::Output::Redirection.new(options[:output]).to_s : ''
1516
@options[:environment_variable] ||= "RAILS_ENV"
1617
@options[:environment] ||= :production

lib/whenever/job_list.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def cron_jobs_of_time(time, jobs)
144144
end
145145
Whenever::Output::Cron.output(time, job, :chronic_options => @chronic_options) do |cron|
146146
cron << "\n\n"
147+
cron = (job.description.strip + "\n").gsub(/^(.*)$/, '# \1') + cron unless job.description.to_s.empty?
147148

148149
if cron[0,1] == "@"
149150
shortcut_jobs << cron
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'test_helper'
2+
3+
class OutputDescriptionTest < Whenever::TestCase
4+
test "single line description" do
5+
output = Whenever.cron \
6+
<<-file
7+
every "weekday", :description => "A description" do
8+
command "blahblah"
9+
end
10+
file
11+
12+
assert_match "# A description\n0 0 * * 1-5 /bin/bash -l -c 'blahblah'\n\n", output
13+
end
14+
15+
test "multi line description" do
16+
output = Whenever.cron \
17+
<<-file
18+
every "weekday", :description => "A description\nhas mulitple lines" do
19+
command "blahblah"
20+
end
21+
file
22+
23+
assert_match "# A description\n# has mulitple lines\n0 0 * * 1-5 /bin/bash -l -c 'blahblah'\n\n", output
24+
end
25+
end

0 commit comments

Comments
 (0)