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

Dashboard: show more details about jobs #575

Merged
merged 21 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions engine/app/controllers/good_job/jobs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def index
end

def show
@job = ActiveJobJob.find(params[:id])
@executions = GoodJob::Execution.active_job_id(params[:id])
.order(Arel.sql("COALESCE(scheduled_at, created_at) DESC"))
redirect_to root_path, alert: "Executions for Active Job #{params[:id]} not found" if @executions.empty?
Expand Down
6 changes: 3 additions & 3 deletions engine/app/helpers/good_job/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true
module GoodJob
module ApplicationHelper
def relative_time(timestamp)
text = timestamp.future? ? "in #{time_ago_in_words(timestamp)}" : "#{time_ago_in_words(timestamp)} ago"
def relative_time(timestamp, **args)
text = timestamp.future? ? "in #{time_ago_in_words(timestamp, **args)}" : "#{time_ago_in_words(timestamp, **args)} ago"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FIXME: not new to this PR, but "in …" and "… ago" need translations

tag.time(text, datetime: timestamp, title: timestamp)
end

Expand All @@ -18,7 +18,7 @@ def status_badge(status)
"badge rounded-pill bg-danger"
end

content_tag :span, status.to_s, class: classes
content_tag :span, status.to_s.titleize, class: classes
end
end
end
87 changes: 86 additions & 1 deletion engine/app/views/good_job/jobs/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,88 @@
<h1 class="mb-3">ActiveJob ID: <code><%= @executions.first.id %></code></h1>
<div class="break-out bg-light border-bottom py-2 mb-3">
<div class="container-fluid pt-2">
<div class="d-flex align-items-center">
<div class="flex-fill">
<nav aria-label="breadcrumb">
<ol class="breadcrumb small mb-0">
<li class="breadcrumb-item"><%= link_to "Jobs", jobs_path %></li>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FIXME: translation for "Jobs"

<li class="breadcrumb-item active" aria-current="page">ActiveJob ID: <code><%= @job.id %></code></li>
</ol>
</nav>
<h2 class="mb-1"><code><%= @job.job_class %></code></h2>
<div>
<%= status_badge @job.status %>
<span class="small text-muted">
<%= relative_time @job.finished_at || @job.performed_at || @job.scheduled_at || @job.created_at, include_seconds: true %>

<% if @job.runtime %>
• <%= @job.runtime.round(2) %>s runtime
<% end %>

<% if @job.executions_count > 1 %>
• <%= pluralize @job.executions_count, 'attempt' %>
<% end %>
</span>
</div>
</div>
<div>
<% job_reschedulable = @job.status.in? [:scheduled, :retried, :queued] %>
<%= button_to reschedule_job_path(@job.id), method: :put,
class: "btn btn-sm #{job_reschedulable ? 'btn-outline-primary' : 'btn-outline-secondary'}",
form_class: "d-inline-block",
disabled: !job_reschedulable,
aria: { label: "Reschedule job" },
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FIXME: translation for "Reschedule job"

title: "Reschedule job",
data: { confirm: "Confirm reschedule" } do %>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FIXME: translation for "Confirm reschedule"

<%= render "good_job/shared/icons/skip_forward" %>
Reschedule
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FIXME: translation for "Reschedule"

<% end %>

<% job_discardable = @job.status.in? [:scheduled, :retried, :queued] %>
<%= button_to discard_job_path(@job.id), method: :put, class: "btn btn-sm #{job_discardable ? 'btn-outline-primary' : 'btn-outline-secondary'}", form_class: "d-inline-block", disabled: !job_discardable, aria: { label: "Discard job" }, title: "Discard job", data: { confirm: "Confirm discard" } do %>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FIXME: translation for "Discard job" and "Confirm discard"

<%= render "good_job/shared/icons/stop" %>
Discard
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FIXME: translation for "Discard"

<% end %>

<%= button_to retry_job_path(@job.id), method: :put, class: "btn btn-sm #{@job.status == :discarded ? 'btn-outline-primary' : 'btn-outline-secondary'}", form_class: "d-inline-block", disabled: @job.status != :discarded, aria: { label: "Retry job" }, title: "Retry job", data: { confirm: "Confirm retry" } do %>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FIXME: translation for "Retry job" and "Confirm retry"

<%= render "good_job/shared/icons/arrow_clockwise" %>
Retry
Copy link
Contributor Author

Choose a reason for hiding this comment

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

FIXME: translation for "Retry"

<% end %>
</div>
</div>
</div>
</div>

<table class="table">
<tr>
<th class="col-1">Queue</th>
<td>
<%= @job.queue_name %>
<% if @job.latency %>
<span class="text-muted small">(<%= @job.latency.round(2) %>s latency)</span>
<% end %>
</td>
</tr>
<tr>
<th>Priority</th>
<td><%= @job.priority %></td>
</tr>

<tr>
<th>Arguments</th>
<td><code><%= @job.serialized_params["arguments"].map(&:inspect).join(', ') %></code></td>
</tr>

<% if @job.recent_error %>
<tr>
<th>Error</th>
<td><pre class="text-danger my-0"><%= @job.recent_error %></pre></td>
</tr>
<% end %>

<tr>
<th>Params</th>
<td><%= tag.pre JSON.pretty_generate(@job.serialized_params) %></td>
</tr>
</table>

<%= render 'good_job/executions/table', executions: @executions %>
14 changes: 14 additions & 0 deletions lib/good_job/active_job_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ def status
end
end

# Time between when this job was expected to run and when it started running
def latency
now = Time.zone.now
expected_start = scheduled_at || created_at
actual_start = performed_at || now

actual_start - expected_start unless expected_start >= now
end

# Time between when this job started and finished
def runtime
(finished_at || Time.zone.now) - performed_at if performed_at
end

# This job's most recent {Execution}
# @param reload [Booelan] whether to reload executions
# @return [Execution]
Expand Down
41 changes: 41 additions & 0 deletions spec/lib/good_job/active_job_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,45 @@ def perform(feline = nil, canine: nil)
end
end
end

describe '#latency' do
it 'is nil for future scheduled job' do
job.scheduled_at = 1.minute.from_now
expect(job.latency).to be(nil)
end

it 'is distance between scheduled_at and now for past scheduled job' do
job.scheduled_at = 1.minute.ago
expect(job.latency).to be_within(0.01).of(Time.zone.now - job.scheduled_at)
end

it 'is distance between created_at and now for queued job' do
job.scheduled_at = nil
expect(job.latency).to be_within(0.01).of(Time.zone.now - job.created_at)
end

it 'is distance between created_at and performed_at for started job' do
job.scheduled_at = nil
job.performed_at = 10.seconds.ago
expect(job.latency).to eq(job.performed_at - job.created_at)
end
end

describe "#runtime" do
it 'is nil for queued job' do
expect(job.runtime).to be(nil)
end

it 'is distance between performed_at and now for started job' do
job.performed_at = 10.seconds.ago
job.finished_at = nil
expect(job.runtime).to be_within(0.01).of(Time.zone.now - job.performed_at)
end

it 'is distance between performed_at and finished_at' do
job.performed_at = 5.seconds.ago
job.finished_at = 1.second.ago
expect(job.runtime).to eq(job.finished_at - job.performed_at)
end
end
end