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

Add execution history for worker #98

Merged
merged 5 commits into from
Mar 7, 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
73 changes: 73 additions & 0 deletions app/controllers/kuroko2/execution_histories_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class Kuroko2::ExecutionHistoriesController < Kuroko2::ApplicationController
def index
@histories = histories.page(params[:page])
end

def timeline
end

def dataset
set_period
@histories = histories.where('started_at < ?', @end_at).where('finished_at > ?', @start_at)
end

private

def query_params
params.permit(:queue, :hostname)
end

def histories
histories = Kuroko2::ExecutionHistory.ordered.includes(:job_definition, :job_instance)

queue = query_params[:queue]
histories = histories.where(queue: queue) if queue.present?

hostname = query_params[:hostname]
histories = histories.where(hostname: hostname) if hostname.present?

histories
end

def period_params
params.permit(:period, :end_at, :start_at)
end

def end_at
if period_params[:end_at].present?
begin
return period_params[:end_at].to_datetime
rescue ArgumentError
# do nothing
end
end
Time.current
end

def start_at
if period_params[:start_at].present?
begin
return period_params[:start_at].to_datetime
rescue ArgumentError
# do nothing
end
end
case period_params[:period]
when /\A(\d+)m\z/
$1.to_i.minutes.ago(@end_at)
when /\A(\d+)h\z/
$1.to_i.hours.ago(@end_at)
when /\A(\d+)d\z/
$1.to_i.days.ago(@end_at)
when /\A(\d+)w\z/
$1.to_i.weeks.ago(@end_at)
else
1.hour.ago(@end_at)
end
end

def set_period
@end_at = end_at
@start_at = start_at
end
end
8 changes: 8 additions & 0 deletions app/models/kuroko2/execution_history.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Kuroko2::ExecutionHistory < Kuroko2::ApplicationRecord
include Kuroko2::TableNameCustomizable

belongs_to :job_definition
belongs_to :job_instance

scope :ordered, -> { order(started_at: :desc) }
end
10 changes: 10 additions & 0 deletions app/views/kuroko2/execution_histories/dataset.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
json.start @start_at.strftime('%Y-%m-%d %H:%M:%S')
json.end @end_at.strftime('%Y-%m-%d %H:%M:%S')
json.data do
json.array! @histories do |history|
json.id history.id
json.content "<a href='#{job_definition_job_instance_path(history.job_definition, history.job_instance)}'>##{history.job_definition.id} #{h(history.job_definition.name)}</a>"
json.start history.started_at.strftime('%Y-%m-%d %H:%M:%S')
json.end history.finished_at.strftime('%Y-%m-%d %H:%M:%S')
end
end
39 changes: 39 additions & 0 deletions app/views/kuroko2/execution_histories/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
- content_for :title, 'Execution Histories'
- content_for :content_title do
<i class="fa fa-history"></i> Execution Histories
- @scope = @histories

.box#execution-histories
.box-header
.row
.col-md-9
h3.box-title Execution Histories
.col-md-3.right-button
= link_to raw('<i class="fa fa-clock-o"></i> Show Timeline'), timeline_execution_histories_path(params.permit(:queue, :hostname)), class: 'btn btn-default btn-small btn-block js-to-timeline'
- if @histories.empty?
.box-body
.text-muted.well.well-sm.no-shadow There are no execution histories yet.
- else
.box-body.table-responsive
table.table.table-hover
thead
tr
th.col-md-2 Hostname
th.col-md-1 WID
th.col-md-2 Queue
th.col-md-2 Job
th.col-md-2 Command
th.col-md-2 Started at
th.col-md-1 Elapsed Time
tbody
- for history in @histories
tr
td= history.hostname
td= history.worker_id
td= history.queue
td.no-decorate= link_to "##{history.job_definition.id} #{history.job_definition.name}", job_definition_job_instance_path(history.job_definition, history.job_instance)
td= history.shell
td= l(history.started_at, format: :short)
td= distance_of_time(history.started_at, history.finished_at)
.box-footer#pagination
= paginate @histories, theme: 'list'
33 changes: 33 additions & 0 deletions app/views/kuroko2/execution_histories/timeline.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
- content_for :title, "Execution Timeline"
- content_for :content_title do
<i class="fa fa-clock-o"></i> Execution Timeline

.row
.col-md-12
.box
.box-header
nav class="navbar navbar-default"
div class="container-fluid"
div class="navbar-header"
a class="navbar-brand" href="#" Period
div class="" id="period-nav"
ul class="nav navbar-nav"
li class="#{params[:period] == '30m' ? 'active' : ''}"
= link_to '30 minutes', timeline_execution_histories_path(params.permit(:queue, :hostname, :end_at).merge(period: '30m'))
li class="#{params[:period] == '1h' || !params[:period] ? 'active' : ''}"
= link_to '1 hour', timeline_execution_histories_path(params.permit(:queue, :hostname, :end_at).merge(period: '1h'))
li class="#{params[:period] == '3h' ? 'active' : ''}"
= link_to '3 hours', timeline_execution_histories_path(params.permit(:queue, :hostname, :end_at).merge(period: '3h'))
li class="#{params[:period] == '6h' ? 'active' : ''}"
= link_to '6 hours', timeline_execution_histories_path(params.permit(:queue, :hostname, :end_at).merge(period: '6h'))
li class="#{params[:period] == '12h' ? 'active' : ''}"
= link_to '12 hours', timeline_execution_histories_path(params.permit(:queue, :hostname, :end_at).merge(period: '12h'))
li class="#{params[:period] == '1d' ? 'active' : ''}"
= link_to '1 day', timeline_execution_histories_path(params.permit(:queue, :hostname, :end_at).merge(period: '1d'))
li class="#{params[:period] == '3d' ? 'active' : ''}"
= link_to '3 days', timeline_execution_histories_path(params.permit(:queue, :hostname, :end_at).merge(period: '3d'))
li class="#{params[:period] == '1w' ? 'active' : ''}"
= link_to '1 week', timeline_execution_histories_path(params.permit(:queue, :hostname, :end_at).merge(period: '1w'))

.box-body
div#timeline data-dataset-path="#{dataset_execution_histories_path(params.permit(:queue, :hostname, :start_at, :end_at, :period))}"
4 changes: 2 additions & 2 deletions app/views/kuroko2/workers/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
th.col-md-1 &nbsp;
- for worker in @workers
tr
td= worker.hostname
td.no-decorate= link_to worker.hostname, execution_histories_path(hostname: worker.hostname)
td= worker.worker_id
td= worker.queue
td.no-decorate= link_to worker.queue, execution_histories_path(queue: worker.queue)
td
- if worker.working
span.label.label-primary WORKING
Expand Down
9 changes: 9 additions & 0 deletions bin/cleanup_old_execution_histories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
old_histories = Kuroko2::ExecutionHistory.where('finished_at < ?', 2.weeks.ago)

count = old_histories.count

Kuroko2::ExecutionHistories.transaction do
old_histories.destroy_all
end

puts "Destroyed #{count} histories"
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
get :dataset, action: :dataset, on: :collection, defaults: { format: 'json' }
end

resources :execution_histories, only: :index do
get :timeline, action: :timeline, on: :collection
get 'timeline/dataset', action: :dataset, on: :collection, defaults: { format: 'json' }, as: 'dataset'
end

get '/sign_in', to: 'sessions#new', as: 'sign_in'
delete '/sign_out', to: 'sessions#destroy', as: 'sign_out'

Expand Down
19 changes: 19 additions & 0 deletions db/migrate/031_create_execution_histories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class CreateExecutionHistories < ActiveRecord::Migration[5.0]
def change
create_table :execution_histories do |t|
t.string :hostname, limit: 180
t.integer :worker_id, limit: 1
t.string :queue, limit: 180, default: "@default", null: false
t.integer :job_definition_id, null: false
t.integer :job_instance_id, null: false
t.text :shell, null: false
t.datetime :started_at, null: false
t.datetime :finished_at, null: false
end

add_column :executions, :hostname, :string, limit: 180
add_column :executions, :worker_id, :integer, limit: 1

add_index :execution_histories, [:worker_id, :started_at], using: :btree
end
end
1 change: 1 addition & 0 deletions lib/autoload/kuroko2/command/shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def execute
def do_execute(execution)
begin
@worker.update_column(:execution_id, execution.id)
execution.update(hostname: @hostname, worker_id: @worker_id)

invoke(execution)
rescue SystemCallError => e
Expand Down
11 changes: 11 additions & 0 deletions lib/autoload/kuroko2/workflow/task/execute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ def update_execution(execution)
instance.logs.error(message)
end

Kuroko2::ExecutionHistory.create(
hostname: execution.hostname,
worker_id: execution.worker_id,
queue: execution.queue,
job_definition: execution.job_definition,
job_instance: execution.job_instance,
shell: execution.shell,
started_at: execution.started_at,
finished_at: execution.finished_at,
)

execution.with_lock do
execution.destroy
execution.success? ? :next : :failure
Expand Down
Loading