-
Notifications
You must be signed in to change notification settings - Fork 723
Add support for sequential job definitions #854
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
Open
njakobsen
wants to merge
3
commits into
javan:main
Choose a base branch
from
combinaut:add-support-for-sequential-job-definitions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| require 'shellwords' | ||
|
|
||
| module Whenever | ||
| class JobSequence | ||
| attr_reader :at, :roles, :mailto | ||
|
|
||
| def initialize(jobs, options = {}) | ||
| validate!(jobs) | ||
|
|
||
| @jobs = jobs | ||
| @options = options | ||
| @at = options.fetch(:at, primary_job.at) | ||
| @mailto = options.fetch(:mailto, primary_job.mailto || :default_mailto) | ||
| @roles = Array(options.delete(:roles), *primary_job.roles) | ||
| end | ||
|
|
||
| def output | ||
| @jobs.map { |job| [job.output, job.halt_sequence_on_failure ? ' && ' : ' ; '] }.flatten[0..-2].join | ||
| end | ||
|
|
||
| def has_role?(role) | ||
| roles.empty? || roles.include?(role) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def primary_job | ||
| @jobs.first | ||
| end | ||
|
|
||
| def validate!(jobs) | ||
| raise ArgumentError, "Jobs in a sequence don't support different `at` values" if jobs.map(&:at).uniq.count > 1 | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| require 'test_helper' | ||
|
|
||
| class OutputJobsWithSequenceTest < Whenever::TestCase | ||
| test "defined jobs with a sequence argument specified per-job" do | ||
| output = Whenever.cron \ | ||
| <<-file | ||
| every 2.hours do | ||
| command "blahblah", sequence: 'backups' | ||
| command "foofoo", sequence: 'backups' | ||
| command "barbar" | ||
| end | ||
| file | ||
|
|
||
| output_without_empty_line = lines_without_empty_line(output.lines) | ||
| assert_equal two_hours + " /bin/bash -l -c 'blahblah' ; /bin/bash -l -c 'foofoo'", output_without_empty_line.shift | ||
| assert_equal two_hours + " /bin/bash -l -c 'barbar'", output_without_empty_line.shift | ||
| end | ||
|
|
||
| test "defined jobs with a sequence argument specified on the group" do | ||
| output = Whenever.cron \ | ||
| <<-file | ||
| every 2.hours, sequence: 'backups' do | ||
| command "blahblah" | ||
| command "foofoo" | ||
| end | ||
| file | ||
|
|
||
| output_without_empty_line = lines_without_empty_line(output.lines) | ||
| assert_equal two_hours + " /bin/bash -l -c 'blahblah' ; /bin/bash -l -c 'foofoo'", output_without_empty_line.shift | ||
| end | ||
|
|
||
| test "defined jobs with a sequence argument and a job that halts the sequence on failure" do | ||
| output = Whenever.cron \ | ||
| <<-file | ||
| every 2.hours, sequence: 'backups' do | ||
| command "blahblah", halt_sequence_on_failure: true | ||
| command "foofoo" | ||
| end | ||
| file | ||
|
|
||
| output_without_empty_line = lines_without_empty_line(output.lines) | ||
| assert_equal two_hours + " /bin/bash -l -c 'blahblah' && /bin/bash -l -c 'foofoo'", output_without_empty_line.shift | ||
| end | ||
|
|
||
| test "defined jobs with a sequences specified on the group and jobs" do | ||
| output = Whenever.cron \ | ||
| <<-file | ||
| every 2.hours, sequence: 'backups' do | ||
| command "blahblah" | ||
| command "barbar", sequence: nil | ||
| command "foofoo" | ||
| command "bazbaz", sequence: 'bees' | ||
| command "buzzbuzz", sequence: 'bees' | ||
| end | ||
| file | ||
|
|
||
| output_without_empty_line = lines_without_empty_line(output.lines) | ||
| assert_equal two_hours + " /bin/bash -l -c 'blahblah' ; /bin/bash -l -c 'foofoo'", output_without_empty_line.shift | ||
| assert_equal two_hours + " /bin/bash -l -c 'barbar'", output_without_empty_line.shift | ||
| assert_equal two_hours + " /bin/bash -l -c 'bazbaz' ; /bin/bash -l -c 'buzzbuzz'", output_without_empty_line.shift | ||
| end | ||
|
|
||
| test "defined jobs with a multiple groups with sequences specified on the group and jobs" do | ||
| output = Whenever.cron \ | ||
| <<-file | ||
| every 2.hours, sequence: 'backups' do | ||
| command "blahblah" | ||
| command "barbar", sequence: nil | ||
| command "bazbaz", sequence: 'bees' | ||
| end | ||
|
|
||
| every 2.hours, sequence: 'backups' do | ||
| command "foofoo" | ||
| command "buzzbuzz", sequence: 'bees' | ||
| end | ||
| file | ||
|
|
||
| output_without_empty_line = lines_without_empty_line(output.lines) | ||
| assert_equal two_hours + " /bin/bash -l -c 'blahblah' ; /bin/bash -l -c 'foofoo'", output_without_empty_line.shift | ||
| assert_equal two_hours + " /bin/bash -l -c 'barbar'", output_without_empty_line.shift | ||
| assert_equal two_hours + " /bin/bash -l -c 'bazbaz' ; /bin/bash -l -c 'buzzbuzz'", output_without_empty_line.shift | ||
| end | ||
|
|
||
| test "defined jobs with a multiple groups with sequences specified on the group and jobs" do | ||
| output = Whenever.cron \ | ||
| <<-file | ||
| every 2.hours, sequence: 'backups' do | ||
| command "blahblah" | ||
| command "barbar", sequence: nil | ||
| command "bazbaz", sequence: 'bees' | ||
| end | ||
|
|
||
| every 3.hours, sequence: 'backups' do | ||
| command "foofoo" | ||
| command "buzzbuzz", sequence: 'bees' | ||
| end | ||
| file | ||
|
|
||
| output_without_empty_line = lines_without_empty_line(output.lines) | ||
| assert_equal two_hours + " /bin/bash -l -c 'blahblah'", output_without_empty_line.shift | ||
| assert_equal two_hours + " /bin/bash -l -c 'barbar'", output_without_empty_line.shift | ||
| assert_equal two_hours + " /bin/bash -l -c 'bazbaz'", output_without_empty_line.shift | ||
| assert_equal three_hours + " /bin/bash -l -c 'foofoo'", output_without_empty_line.shift | ||
| assert_equal three_hours + " /bin/bash -l -c 'buzzbuzz'", output_without_empty_line.shift | ||
| end | ||
|
|
||
| test "defined jobs with a multiple groups with sequences specified on the group and jobs" do | ||
| assert_raises ArgumentError do | ||
| Whenever.cron \ | ||
| <<-file | ||
| every 2.hours, sequence: 'backups' do | ||
| command "blahblah", at: 1 | ||
| command "barbar", at: 2 | ||
| end | ||
| file | ||
| end | ||
| end | ||
|
|
||
| def three_hours | ||
| "0 0,3,6,9,12,15,18,21 * * *" | ||
| end | ||
| end | ||
|
|
||
| class OutputJobsWithSequentialTest < Whenever::TestCase | ||
| test "defined jobs with a sequential argument" do | ||
| output = Whenever.cron \ | ||
| <<-file | ||
| every 2.hours, sequential: true do | ||
| command "blahblah" | ||
| command "foofoo" | ||
| end | ||
| file | ||
|
|
||
| output_without_empty_line = lines_without_empty_line(output.lines) | ||
| assert_equal two_hours + " /bin/bash -l -c 'blahblah' ; /bin/bash -l -c 'foofoo'", output_without_empty_line.shift | ||
| end | ||
|
|
||
| test "defined jobs with a sequential argument" do | ||
| output = Whenever.cron \ | ||
| <<-file | ||
| every 2.hours, sequential: true do | ||
| command "blahblah" | ||
| command "foofoo", sequence: false | ||
| end | ||
| file | ||
|
|
||
| output_without_empty_line = lines_without_empty_line(output.lines) | ||
| assert_equal two_hours + " /bin/bash -l -c 'blahblah'", output_without_empty_line.shift | ||
| assert_equal two_hours + " /bin/bash -l -c 'foofoo'", output_without_empty_line.shift | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be better to use a name like
chainorcombineinstead ofsequence, to better reflect the actual behavior of the generated crontab. What do you think?Here is the behavior I confirmed: