Skip to content

Commit

Permalink
Remove TaskData#last_run method
Browse files Browse the repository at this point in the history
Co-authored-by: Paulo Barros <[email protected]>
  • Loading branch information
2 people authored and promulo committed Jul 21, 2022
1 parent b9c154f commit b3e1df1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
22 changes: 6 additions & 16 deletions app/models/maintenance_tasks/task_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,22 @@ def available_tasks
end
end

# Initializes a Task Data with a name and optionally a last_run.
# Initializes a Task Data with a name and optionally a related run.
#
# @param name [String] the name of the Task subclass.
# @param last_run [MaintenanceTasks::Run] optionally, a Run record to
# @param related_run [MaintenanceTasks::Run] optionally, a Run record to
# set for the Task.
def initialize(name, last_run = :none_passed)
def initialize(name, related_run = nil)
@name = name
@last_run = last_run unless last_run == :none_passed
@related_run = related_run
end

# @return [String] the name of the Task.
attr_reader :name
attr_reader :related_run

alias_method :to_s, :name
alias_method :last_run, :related_run

# The Task's source code.
#
Expand All @@ -97,18 +99,6 @@ def code
File.read(file)
end

# Retrieves the latest Run associated with the Task.
#
# @return [MaintenanceTasks::Run] the Run record.
# @return [nil] if there are no Runs associated with the Task.
def last_run
return @last_run if defined?(@last_run)

@last_run = [active_runs.first, completed_runs.first].max_by do |r|
r.nil? ? Time.new(0) : r.created_at
end
end

# Returns the set of currently active Run records associated with the Task.
#
# @return [ActiveRecord::Relation<MaintenanceTasks::Run>] the relation of
Expand Down
1 change: 0 additions & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2022_07_13_131925) do

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
Expand Down
29 changes: 10 additions & 19 deletions test/models/maintenance_tasks/task_data_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,6 @@ class TaskDataTest < ActiveSupport::TestCase
assert_nil task_data.code
end

test "#last_run returns the last Run associated with the Task" do
Run.create!(
task_name: "Maintenance::UpdatePostsTask",
status: :succeeded
)
latest = Run.create!(task_name: "Maintenance::UpdatePostsTask")
task_data = TaskData.new("Maintenance::UpdatePostsTask")

assert_equal latest, task_data.last_run
end

test "#to_s returns the name of the Task" do
task_data = TaskData.new("Maintenance::UpdatePostsTask")

Expand Down Expand Up @@ -118,20 +107,22 @@ class TaskDataTest < ActiveSupport::TestCase
end

test "#status is new when Task does not have any Runs" do
Run.destroy_all
task_data = TaskData.find("Maintenance::UpdatePostsTask")
task_data = TaskData.new("Maintenance::UpdatePostsTask")
assert_equal "new", task_data.status
end

test "#status is the latest Run status" do
Run.create!(task_name: "Maintenance::UpdatePostsTask", status: :paused)
task_data = TaskData.find("Maintenance::UpdatePostsTask")
run = Run.create!(
task_name: "Maintenance::UpdatePostsTask",
status: :paused
)
task_data = TaskData.new("Maintenance::UpdatePostsTask", run)
assert_equal "paused", task_data.status
end

test "#category returns :active if the task is active" do
Run.create!(task_name: "Maintenance::UpdatePostsTask")
task_data = TaskData.new("Maintenance::UpdatePostsTask")
run = Run.create!(task_name: "Maintenance::UpdatePostsTask")
task_data = TaskData.new("Maintenance::UpdatePostsTask", run)
assert_equal :active, task_data.category
end

Expand All @@ -140,11 +131,11 @@ class TaskDataTest < ActiveSupport::TestCase
end

test "#category returns :completed if the task is completed" do
Run.create!(
run = Run.create!(
task_name: "Maintenance::UpdatePostsTask",
status: :succeeded
)
task_data = TaskData.new("Maintenance::UpdatePostsTask")
task_data = TaskData.new("Maintenance::UpdatePostsTask", run)
assert_equal :completed, task_data.category
end

Expand Down

0 comments on commit b3e1df1

Please sign in to comment.