diff --git a/app/models/kuroko2/job_instance.rb b/app/models/kuroko2/job_instance.rb index ef6c4442..18fec6be 100644 --- a/app/models/kuroko2/job_instance.rb +++ b/app/models/kuroko2/job_instance.rb @@ -34,10 +34,6 @@ def add(level, message) scope :working, -> { where(finished_at: nil, canceled_at: nil) } scope :finished, -> { where.not(finished_at: nil) } - def notify_back_to_normal? - job_definition.notify_back_to_normal? && retrying? - end - def error? working? && error_at? end diff --git a/db/migrate/030_add_notify_back_to_normal.rb b/db/migrate/030_add_notify_back_to_normal.rb index e5c1185a..0e285e66 100644 --- a/db/migrate/030_add_notify_back_to_normal.rb +++ b/db/migrate/030_add_notify_back_to_normal.rb @@ -1,6 +1,5 @@ class AddNotifyBackToNormal < ActiveRecord::Migration[5.1] def change add_column :job_definitions, :notify_back_to_normal, :boolean, default: false, null: false, after: :notify_cancellation - add_column :job_instances, :retrying, :boolean end end diff --git a/lib/autoload/kuroko2/workflow/engine.rb b/lib/autoload/kuroko2/workflow/engine.rb index 1ab074aa..854913e5 100644 --- a/lib/autoload/kuroko2/workflow/engine.rb +++ b/lib/autoload/kuroko2/workflow/engine.rb @@ -25,10 +25,11 @@ def retry(token) node = extract_node(token) message = "(token #{token.uuid}) Retry current node: '#{node.type}: #{node.option}'" - token.job_instance.update_columns(error_at: nil, retrying: true) + token.job_instance.update_column(:error_at, nil) token.job_instance.logs.info(message) token.mark_as_working + token.context['RETRYING'] = true token.save! Kuroko2.logger.info(message) @@ -119,6 +120,7 @@ def process_next(node, token) token.mark_as_finished unless token.parent token.job_instance.touch(:finished_at) + Notifier.notify(:back_to_normal, token.job_instance) if token.context['RETRYING'] Notifier.notify(:finished, token.job_instance) token.destroy! end diff --git a/lib/autoload/kuroko2/workflow/notifier/concerns/chat_message_builder.rb b/lib/autoload/kuroko2/workflow/notifier/concerns/chat_message_builder.rb index 0702f807..493d8852 100644 --- a/lib/autoload/kuroko2/workflow/notifier/concerns/chat_message_builder.rb +++ b/lib/autoload/kuroko2/workflow/notifier/concerns/chat_message_builder.rb @@ -20,6 +20,10 @@ def launched_text "Launched '#{@definition.name}'" end + def back_to_normal_text + "Back to normal the current task in '#{@definition.name}'" + end + def retrying_text "Retrying the current task in '#{@definition.name}'" end diff --git a/lib/autoload/kuroko2/workflow/notifier/hipchat.rb b/lib/autoload/kuroko2/workflow/notifier/hipchat.rb index 1ff3f810..e689f613 100644 --- a/lib/autoload/kuroko2/workflow/notifier/hipchat.rb +++ b/lib/autoload/kuroko2/workflow/notifier/hipchat.rb @@ -73,12 +73,19 @@ def notify_critical end def notify_finished - if @definition.hipchat_notify_finished? || @instance.notify_back_to_normal? + if @definition.hipchat_notify_finished? message = build_message(level: 'SUCCESS', text: message_builder.finished_text) send_to_hipchat(message) end end + def notify_back_to_normal + if @definition.notify_back_to_normal? + message = build_message(level: 'SUCCESS', text: message_builder.back_to_normal_text) + send_to_hipchat(message) + end + end + def notify_long_elapsed_time message = build_message(level: 'WARNING', text: message_builder.long_elapsed_time_text) send_to_hipchat(message, color: 'red') diff --git a/lib/autoload/kuroko2/workflow/notifier/mail.rb b/lib/autoload/kuroko2/workflow/notifier/mail.rb index d08a013c..a8ad46a8 100644 --- a/lib/autoload/kuroko2/workflow/notifier/mail.rb +++ b/lib/autoload/kuroko2/workflow/notifier/mail.rb @@ -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 diff --git a/lib/autoload/kuroko2/workflow/notifier/slack.rb b/lib/autoload/kuroko2/workflow/notifier/slack.rb index 9b116cb7..2e3e527c 100644 --- a/lib/autoload/kuroko2/workflow/notifier/slack.rb +++ b/lib/autoload/kuroko2/workflow/notifier/slack.rb @@ -80,7 +80,7 @@ def notify_critical end def notify_finished - if @definition.hipchat_notify_finished? || @instance.notify_back_to_normal? + if @definition.hipchat_notify_finished? send_attachment_message_to_slack( level: 'SUCCESS', text: message_builder.finished_text, @@ -88,6 +88,15 @@ def notify_finished end end + def notify_back_to_normal + if @definition.notify_back_to_normal? + send_attachment_message_to_slack( + level: 'SUCCESS', + text: message_builder.back_to_normal_text, + ) + end + end + def notify_long_elapsed_time send_attachment_message_to_slack( level: 'WARNING', diff --git a/lib/autoload/kuroko2/workflow/notifier/webhook.rb b/lib/autoload/kuroko2/workflow/notifier/webhook.rb index 03621fe4..066db038 100644 --- a/lib/autoload/kuroko2/workflow/notifier/webhook.rb +++ b/lib/autoload/kuroko2/workflow/notifier/webhook.rb @@ -89,7 +89,7 @@ def notify_critical end def notify_finished - if @definition.hipchat_notify_finished? || @instance.notify_back_to_normal? + if @definition.hipchat_notify_finished? request( build_payload( action: 'notify_finished', @@ -100,6 +100,18 @@ def notify_finished end end + def notify_back_to_normal + if @definition.notify_back_to_normal? + request( + build_payload( + action: 'notify_back_to_normal', + level: 'SUCCESS', + subject: message_builder.back_to_normal_text, + ) + ) + end + end + def notify_long_elapsed_time request( build_payload( diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 5774b6de..3629c853 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -80,7 +80,6 @@ t.datetime "error_at" t.datetime "created_at" t.datetime "updated_at" - t.boolean "retrying" t.index ["finished_at", "canceled_at", "job_definition_id"], name: "job_instance_idx" t.index ["job_definition_id"], name: "index_kuroko2_job_instances_on_job_definition_id" end diff --git a/spec/workflow/engine_spec.rb b/spec/workflow/engine_spec.rb index 553ce224..e76d2826 100644 --- a/spec/workflow/engine_spec.rb +++ b/spec/workflow/engine_spec.rb @@ -298,19 +298,17 @@ module Kuroko2::Workflow end specify do - expect(token.job_instance.notify_back_to_normal?).to be_falsey - subject.process(token) subject.process(token) shell.execute subject.process(token) expect(token.status_name).to eq 'failure' - expect(token.job_instance.retrying?).to be_falsey FileUtils.touch(tmpfile) + expect(token.context['RETRYING']).to be_falsy subject.retry(token) - expect(token.job_instance.retrying?).to be_truthy + expect(token.context['RETRYING']).to be_truthy subject.process(token) shell.execute @@ -319,36 +317,6 @@ module Kuroko2::Workflow expect(token.status_name).to eq 'finished' expect(Kuroko2::Token.all.count).to eq 0 - expect(token.job_instance.notify_back_to_normal?).to be_falsey - end - - context 'notify_back_to_normal' do - before do - definition.update_column(:notify_back_to_normal, true) - end - - specify do - expect(token.job_instance.notify_back_to_normal?).to be_falsey - - subject.process(token) - subject.process(token) - shell.execute - subject.process(token) - expect(token.status_name).to eq 'failure' - - FileUtils.touch(tmpfile) - - subject.retry(token) - - subject.process(token) - shell.execute - subject.process(token) - subject.process(token) - - expect(token.status_name).to eq 'finished' - expect(Kuroko2::Token.all.count).to eq 0 - expect(token.job_instance.notify_back_to_normal?).to be_truthy - end end end diff --git a/spec/workflow/notifier/hipchat_spec.rb b/spec/workflow/notifier/hipchat_spec.rb index a17fc6f8..e67ace5d 100644 --- a/spec/workflow/notifier/hipchat_spec.rb +++ b/spec/workflow/notifier/hipchat_spec.rb @@ -125,6 +125,35 @@ module Kuroko2::Workflow end end + describe '#notify_back_to_normal' do + context 'with notify_back_to_normal' do + before do + instance.job_definition.notify_back_to_normal = true + instance.save! + end + + 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 + context 'without notify_back_to_normal' do + before do + instance.job_definition.notify_back_to_normal = false + instance.save! + end + + it 'sends back_to_normal message' do + expect(hipchat_room_object).not_to receive(:send) + notifier.notify_back_to_normal + end + end + end + describe '#notify_retrying' do context 'with notify_finished' do before do diff --git a/spec/workflow/notifier/mail_spec.rb b/spec/workflow/notifier/mail_spec.rb index d8d7d4fa..7696e972 100644 --- a/spec/workflow/notifier/mail_spec.rb +++ b/spec/workflow/notifier/mail_spec.rb @@ -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 { diff --git a/spec/workflow/notifier/slack_spec.rb b/spec/workflow/notifier/slack_spec.rb index 9a9e67e5..916b3bb4 100644 --- a/spec/workflow/notifier/slack_spec.rb +++ b/spec/workflow/notifier/slack_spec.rb @@ -90,6 +90,34 @@ module Kuroko2::Workflow end end + describe '#notify_back_to_normal' do + context 'with notify_back_to_normal' do + before do + instance.job_definition.notify_back_to_normal = true + instance.save! + end + + 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 + + context 'without notify_back_to_normal' do + before do + instance.job_definition.notify_back_to_normal = false + instance.save! + end + + it 'sends back_to_normal mesasge' do + expect(notifier).not_to receive(:send_to_slack) + notifier.notify_back_to_normal + end + end + end + describe '#notify_retrying' do context 'with notify_finished' do before do diff --git a/spec/workflow/notifier/webhook_spec.rb b/spec/workflow/notifier/webhook_spec.rb index e303e190..338252fc 100644 --- a/spec/workflow/notifier/webhook_spec.rb +++ b/spec/workflow/notifier/webhook_spec.rb @@ -72,6 +72,36 @@ module Kuroko2::Workflow end end + describe '#notify_back_to_normal' do + context 'with notify_back_to_normal' do + before do + instance.job_definition.notify_back_to_normal = true + instance.save! + end + + 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 + + context 'without notify_back_to_normal' do + before do + instance.job_definition.notify_back_to_normal = false + instance.save! + end + + it 'sends back_to_normal message' do + notifier.notify_back_to_normal + expect(stub).not_to have_been_requested + end + end + end + describe '#notify_retrying' do context 'with notify_finished' do before do