Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add description as comment in crontab #776

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ every 3.hours do
end
```

### Adding comments to crontab

A description can be added to a job that will be included in the crontab as a comment above the cron entry.

Example: A single line description:

```ruby
every 1.hours, desc: "My job description" do
command "/usr/bin/my_great_command"
end
```

Example: A multi line description:

```ruby
every 1.hours, desc: "My job description\nhas multiple lines" do
command "/usr/bin/my_great_command"
end
```

### Capistrano integration

Use the built-in Capistrano recipe for easy crontab updates with deploys. For Capistrano V3, see the next section.
Expand Down
3 changes: 2 additions & 1 deletion lib/whenever/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Whenever
class Job
attr_reader :at, :roles, :mailto
attr_reader :at, :roles, :mailto, :desc

def initialize(options = {})
@options = options
Expand All @@ -11,6 +11,7 @@ def initialize(options = {})
@mailto = options.fetch(:mailto, :default_mailto)
@job_template = options.delete(:job_template) || ":job"
@roles = Array(options.delete(:roles))
@desc = options.delete(:desc)
@options[:output] = options.has_key?(:output) ? Whenever::Output::Redirection.new(options[:output]).to_s : ''
@options[:environment_variable] ||= "RAILS_ENV"
@options[:environment] ||= :production
Expand Down
1 change: 1 addition & 0 deletions lib/whenever/job_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def cron_jobs_of_time(time, jobs)
end
Whenever::Output::Cron.output(time, job, :chronic_options => @chronic_options) do |cron|
cron << "\n\n"
cron = (job.desc.strip + "\n").gsub(/^(.*)$/, '# \1') + cron unless job.desc.to_s.empty?

if cron[0,1] == "@"
shortcut_jobs << cron
Expand Down
25 changes: 25 additions & 0 deletions test/functional/output_desc_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'test_helper'

class OutputDescTest < Whenever::TestCase
test "single line description" do
output = Whenever.cron \
<<-file
every "weekday", :desc => "A description" do
command "blahblah"
end
file

assert_match "# A description\n0 0 * * 1-5 /bin/bash -l -c 'blahblah'\n\n", output
end

test "multi line description" do
output = Whenever.cron \
<<-file
every "weekday", :desc => "A description\nhas mulitple lines" do
command "blahblah"
end
file

assert_match "# A description\n# has mulitple lines\n0 0 * * 1-5 /bin/bash -l -c 'blahblah'\n\n", output
end
end