Skip to content

Commit

Permalink
Merge pull request #67 from eagletmt/execution-id
Browse files Browse the repository at this point in the history
Add execution_id to process_signals table and use it
  • Loading branch information
eagletmt authored Jun 27, 2017
2 parents c216165 + 18376fd commit 24ed9c0
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 311 deletions.
7 changes: 4 additions & 3 deletions app/controllers/kuroko2/executions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ def index
end

def destroy
if @execution.try(:pid)
hostname = Kuroko2::Worker.executing(@execution.id).try(:hostname)
Kuroko2::ProcessSignal.create!(pid: @execution.pid, hostname: hostname) if hostname
if @execution.try!(:pid)
hostname = Kuroko2::Worker.executing(@execution.id).try!(:hostname) || ''
# XXX: Store pid and hostname for compatibility
Kuroko2::ProcessSignal.create!(pid: @execution.pid, hostname: hostname, execution_id: @execution.id)
end

redirect_to job_definition_job_instance_path(job_definition_id: execution_params[:job_definition_id], id: execution_params[:job_instance_id])
Expand Down
4 changes: 3 additions & 1 deletion app/models/kuroko2/process_signal.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class Kuroko2::ProcessSignal < Kuroko2::ApplicationRecord
include Kuroko2::TableNameCustomizable

belongs_to :execution

scope :unstarted, -> { where(started_at: nil) }
scope :on, ->(hostname) { where(hostname: hostname) }
scope :on, ->(hostname) { joins(execution: :worker).merge(Kuroko2::Worker.on(hostname)) }

def self.poll(hostname)
self.transaction do
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/029_add_execution_id_to_process_signals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddExecutionIdToProcessSignals < ActiveRecord::Migration[5.0]
def change
add_reference :process_signals, :execution, foreign_key: false
end
end
17 changes: 6 additions & 11 deletions lib/autoload/kuroko2/workflow/task/execute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,12 @@ def process_timeout_if_needed(execution)
timeout = token.context['TIMEOUT'].to_i

if timeout > 0 && ((execution.created_at + timeout.minutes) < Time.current) && execution.pid
hostname = Worker.executing(execution.id).try(:hostname)
if hostname
ProcessSignal.create!(pid: execution.pid, hostname: hostname)
message = "(token #{token.uuid}) Timeout occurred after #{timeout} minutes."
token.job_instance.logs.info(message)
Kuroko2.logger.info(message)
else
message = "(token #{token.uuid}) The timeout task is not working. Hostname not found on execution_id #{execution.id}"
token.job_instance.logs.error(message)
Kuroko2.logger.error(message)
end
hostname = Worker.executing(execution.id).try!(:hostname)
# XXX: Store pid and hostname for compatibility
ProcessSignal.create!(pid: execution.pid, hostname: hostname, execution_id: execution.id)
message = "(token #{token.uuid}) Timeout occurred after #{timeout} minutes."
token.job_instance.logs.info(message)
Kuroko2.logger.info(message)
end
end
end
Expand Down
16 changes: 11 additions & 5 deletions spec/command/kill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ module Kuroko2::Command
describe Kill do

describe '#execute' do
subject { Kill.new('test', '1').execute }
subject { Kill.new(hostname, '1').execute }

before { Process.detach(pid) }
before do
execution.pid = Process.spawn('sleep 10')
Process.detach(execution.pid)
worker.update!(execution_id: execution.id)
end

let!(:signal) { create(:process_signal, pid: pid, hostname: 'test') }
let(:pid) { Process.spawn('sleep 10') }
let!(:signal) { create(:process_signal, pid: execution.pid, hostname: 'test', execution_id: execution.id) }
let(:execution) { create(:execution) }
let(:hostname) { 'test' }
let(:worker) { create(:worker, hostname: hostname) }

it 'terminates spawned process' do
is_expected.to eq signal
expect { Process.kill(0, pid) }.to raise_error(Errno::ESRCH)
expect { Process.kill(0, execution.pid) }.to raise_error(Errno::ESRCH)
end
end
end
Expand Down
Loading

0 comments on commit 24ed9c0

Please sign in to comment.