Skip to content

Commit

Permalink
Fix for multiple tasks, and change message text
Browse files Browse the repository at this point in the history
  • Loading branch information
takonomura committed Jan 19, 2018
1 parent 3b0d971 commit c5246f2
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 44 deletions.
4 changes: 0 additions & 4 deletions app/models/kuroko2/job_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion db/migrate/030_add_notify_back_to_normal.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion lib/autoload/kuroko2/workflow/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
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
"Back to normal the current task in '#{@definition.name}'"
end

def retrying_text
"Retrying the current task in '#{@definition.name}'"
end
Expand Down
9 changes: 8 additions & 1 deletion lib/autoload/kuroko2/workflow/notifier/hipchat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
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
11 changes: 10 additions & 1 deletion lib/autoload/kuroko2/workflow/notifier/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,23 @@ 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,
)
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',
Expand Down
14 changes: 13 additions & 1 deletion lib/autoload/kuroko2/workflow/notifier/webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(
Expand Down
1 change: 0 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 2 additions & 34 deletions spec/workflow/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
29 changes: 29 additions & 0 deletions spec/workflow/notifier/hipchat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
28 changes: 28 additions & 0 deletions spec/workflow/notifier/slack_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions spec/workflow/notifier/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c5246f2

Please sign in to comment.