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

Slight refactoring to CronEntry #1063

Merged
merged 1 commit into from
Sep 2, 2023
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
56 changes: 28 additions & 28 deletions app/models/good_job/cron_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,15 @@ def description

def next_at(previously_at: nil)
if cron_proc?
result = Rails.application.executor.wrap { cron.call(previously_at || last_at) }
return Fugit.parse(result).next_time.to_t if result.is_a?(String)

return result

result = Rails.application.executor.wrap { cron.call(previously_at || last_job_at) }
if result.is_a?(String)
Fugit.parse(result).next_time.to_t
else
result
end
else
fugit.next_time.to_t
end
fugit.next_time.to_t
end

def schedule
return "Custom schedule" if cron_proc?

fugit.original
end

def jobs
GoodJob::Job.where(cron_key: key)
end

def last_at
return if last_job.blank?

(last_job.cron_at || last_job.created_at).localtime
end

def enabled?
Expand Down Expand Up @@ -113,15 +99,11 @@ def enqueue(cron_at = nil)
false
end

def last_job
jobs.order("cron_at DESC NULLS LAST").first
end

def display_properties
{
key: key,
class: job_class,
cron: schedule,
cron: display_schedule,
set: display_property(set),
description: display_property(description),
}.tap do |properties|
Expand All @@ -130,6 +112,24 @@ def display_properties
end
end

def display_schedule
cron_proc? ? display_property(cron) : fugit.original
end

def jobs
GoodJob::Job.where(cron_key: key)
end

def last_job
jobs.order("cron_at DESC NULLS LAST").first
end

def last_job_at
return if last_job.blank?

(last_job.cron_at || last_job.created_at).localtime
end

private

def cron
Expand Down Expand Up @@ -163,7 +163,7 @@ def display_property(value)
case value
when NilClass
"None"
when Proc
when Callable
"Lambda/Callable"
else
value
Expand Down
4 changes: 2 additions & 2 deletions app/views/good_job/cron_entries/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<div class="col-12 col-lg-2 text-wrap"><%= tag.span tag.code(cron_entry.job_class), class: "fs-5 mb-0" %></div>
<div class="col-6 col-lg-2 text-wrap">
<div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.cron.schedule" %></div>
<span class="font-monospace fw-bold"><%= cron_entry.schedule %></span>
<span class="font-monospace fw-bold"><%= cron_entry.display_schedule %></span>
</div>
<div class="col-6 col-lg-2 text-wrap small">
<div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.cron.next_scheduled" %></div>
Expand All @@ -40,7 +40,7 @@
<div class="col-6 col-lg-2 text-wrap small">
<% if cron_entry.last_job.present? %>
<div class="d-lg-none small text-muted mt-1"><%= t "good_job.models.cron.last_run" %></div>
<%= link_to relative_time(cron_entry.last_at), cron_entry_path(cron_entry), title: "Job #{cron_entry.last_job.id}" %>
<%= link_to relative_time(cron_entry.last_job_at), cron_entry_path(cron_entry), title: "Job #{cron_entry.last_job.id}" %>
<% end %>
</div>
<div class="col d-flex gap-3 justify-content-end">
Expand Down
1 change: 1 addition & 0 deletions lib/good_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

require "good_job/assignable_connection"
require "good_job/bulk"
require "good_job/callable"
require "good_job/capsule"
require "good_job/cleanup_tracker"
require "good_job/cli"
Expand Down
12 changes: 12 additions & 0 deletions lib/good_job/callable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module GoodJob
# An object that has case-equality to a Proc or Lambda by responding to #call.
# This can be used to duck-type match in a case statement.
module Callable
# Whether the object responds to #call
def self.===(other)
other.respond_to?(:call)
end
end
end
8 changes: 4 additions & 4 deletions spec/app/models/good_job/cron_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ def perform(meaning, name:)
end
end

describe 'schedule' do
describe 'display_schedule' do
it 'returns the cron expression' do
expect(entry.schedule).to eq('* * * * *')
expect(entry.display_schedule).to eq('* * * * *')
end

it 'returns the cron expression for a schedule parsed using natual language' do
entry = described_class.new(cron: 'every weekday at five')
expect(entry.schedule).to eq('0 5 * * 1-5')
expect(entry.display_schedule).to eq('0 5 * * 1-5')
end

it 'generates a schedule provided via a block' do
entry = described_class.new(cron: ->(last_run) {})
expect(entry.schedule).to eq('Custom schedule')
expect(entry.display_schedule).to eq('Lambda/Callable')
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/system/cron_entries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
it 'can enqueue a cron_entry immediately' do
visit '/good_job/cron_entries'
expect(page).to have_content cron_entry.job_class
expect(cron_entry.last_at).to be_nil
expect(cron_entry.last_job_at).to be_nil

within "##{dom_id(cron_entry)}" do
accept_confirm { click_on "Enqueue cron entry now" }
Expand Down