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

Notify when back to normal #93

Merged
merged 8 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions db/migrate/030_add_notify_back_to_normal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNotifyBackToNormal < ActiveRecord::Migration[5.0]
def change
add_column :job_instances, :retrying, :boolean, default: false, null: false
end
end
8 changes: 6 additions & 2 deletions lib/autoload/kuroko2/workflow/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def retry(token)
node = extract_node(token)

message = "(token #{token.uuid}) Retry current node: '#{node.type}: #{node.option}'"
token.job_instance.update_column(:error_at, nil)
token.job_instance.update_columns(error_at: nil, retrying: true)
Copy link
Member

Choose a reason for hiding this comment

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

If some tasks exists,

eg.

my_runner:  first_task
my_runner:  second_task
  1. a first task has an error.
  2. a first task retried and finished successfully.
  3. Notified back to normal
  4. a second task finished successfully
  5. Notified back to normal

The No.5 notification is not necessary.

Further, the fork task and parallel_fork task are more complicated. It will not work we want with this implementation.
https://github.com/cookpad/kuroko2/blob/master/docs/tasks.md#fork
https://github.com/cookpad/kuroko2/blob/master/docs/tasks.md#parallel_fork

I think that it is better to set retrying status in tokens.context.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I fixed it in c5246f2.

token.job_instance.logs.info(message)

token.mark_as_working
Expand Down Expand Up @@ -119,7 +119,11 @@ def process_next(node, token)
token.mark_as_finished
unless token.parent
token.job_instance.touch(:finished_at)
Notifier.notify(:finished, token.job_instance)
if token.job_instance.retrying?
Notifier.notify(:back_to_normal, token.job_instance)
else
Notifier.notify(:finished, token.job_instance)
end
token.destroy!
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def launched_text
"Launched '#{@definition.name}'"
end

def back_to_normal_text
"Backed to normal '#{@definition.name}'"
end

def retrying_text
"Retrying the current task in '#{@definition.name}'"
end
Expand Down
5 changes: 5 additions & 0 deletions lib/autoload/kuroko2/workflow/notifier/hipchat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ def notify_finished
end
end

def notify_back_to_normal
message = build_message(level: 'SUCCESS', text: message_builder.back_to_normal_text)
send_to_hipchat(message)
end

def notify_long_elapsed_time
message = build_message(level: 'WARNING', text: message_builder.long_elapsed_time_text)
send_to_hipchat(message, color: 'red')
Expand Down
4 changes: 4 additions & 0 deletions lib/autoload/kuroko2/workflow/notifier/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def notify_finished
# do nothing
end

def notify_back_to_normal
# do nothing
end

def notify_long_elapsed_time
Kuroko2::Notifications.notify_long_elapsed_time(@job_instance).deliver_now
end
Expand Down
7 changes: 7 additions & 0 deletions lib/autoload/kuroko2/workflow/notifier/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ def notify_finished
end
end

def notify_back_to_normal
send_attachment_message_to_slack(
level: 'SUCCESS',
text: message_builder.back_to_normal_text,
)
end

def notify_long_elapsed_time
send_attachment_message_to_slack(
level: 'WARNING',
Expand Down
10 changes: 10 additions & 0 deletions lib/autoload/kuroko2/workflow/notifier/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ def notify_finished
end
end

def notify_back_to_normal
request(
build_payload(
action: 'notify_back_to_normal',
level: 'SUCCESS',
subject: message_builder.back_to_normal_text,
)
)
end

def notify_long_elapsed_time
request(
build_payload(
Expand Down
265 changes: 133 additions & 132 deletions spec/dummy/db/schema.rb

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions spec/workflow/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ module Kuroko2::Workflow

FileUtils.touch(tmpfile)

expect(token.job_instance.retrying?).to be_falsy
subject.retry(token)
expect(token.job_instance.retrying?).to be_truthy

subject.process(token)
shell.execute
Expand Down
11 changes: 11 additions & 0 deletions spec/workflow/notifier/hipchat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ module Kuroko2::Workflow
end
end

describe '#notify_back_to_normal' do
it 'sends back_to_normal message' do
expect(hipchat_room_object).to receive(:send) do |_, message, option|
expect(message).to include('SUCCESS')
expect(option[:color]).to eq('green')
end

notifier.notify_back_to_normal
end
end

describe '#notify_retrying' do
context 'with notify_finished' do
before do
Expand Down
8 changes: 8 additions & 0 deletions spec/workflow/notifier/mail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ module Kuroko2::Workflow
end
end

describe '#notify_back_to_normal' do
it 'does not send mail' do
expect { notifier.notify_back_to_normal }.not_to change {
ActionMailer::Base.deliveries.size
}
end
end

describe '#notify_long_elapsed_time' do
it 'sends warning mesasge' do
expect { notifier.notify_long_elapsed_time }.to change {
Expand Down
9 changes: 9 additions & 0 deletions spec/workflow/notifier/slack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ module Kuroko2::Workflow
end
end

describe '#notify_back_to_normal' do
it 'sends back_to_normal mesasge' do
expect(notifier).to receive(:send_to_slack).
with(hash_including(channel: slack_channel)).and_call_original

notifier.notify_back_to_normal
end
end

describe '#notify_retrying' do
context 'with notify_finished' do
before do
Expand Down
11 changes: 11 additions & 0 deletions spec/workflow/notifier/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ module Kuroko2::Workflow
end
end

describe '#notify_back_to_normal' do
it 'sends back_to_normal message' do
stub.with { |req|
expect(JSON.parse(req.body)).to include("action"=>"notify_back_to_normal")
}

notifier.notify_back_to_normal
expect(stub).to have_been_requested
end
end

describe '#notify_retrying' do
context 'with notify_finished' do
before do
Expand Down