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

Refactors workflows and tasks #185

Merged
merged 5 commits into from Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/api/tasks_controller.rb
Expand Up @@ -3,7 +3,7 @@ def resource_class
Task
end

Task.aasm.events.map(&:name).each do |action|
Task.aasm.events.map(&:name).reject{|x| [:retry].include? x}.each do |action|
nubis marked this conversation as resolved.
Show resolved Hide resolved
define_method(action) do
task = Task.find(params[:id])
begin
Expand Down
6 changes: 5 additions & 1 deletion app/models/task.rb
Expand Up @@ -38,7 +38,7 @@ class Task < ApplicationRecord
end

def failure!
if may_retry?
if can_retry?
retry!
else
fail!
Expand All @@ -53,6 +53,10 @@ def can_retry?
current_retries < max_retries
end

def can_execute?
state == 'new' || (state == 'retried' && can_retry?)
end

def has_an_output?
!output.blank?
end
Expand Down
4 changes: 2 additions & 2 deletions app/models/workflow.rb
Expand Up @@ -68,15 +68,15 @@ def name
def state
aasm_state
end

def all_task_in_final_state?
tasks.all? {|task| task.performed? || task.failed?}
end

def all_tasks_performed?
tasks.all? {|task| task.performed?}
end

def any_task_failed?
tasks.any? {|task| task.failed? && !task.can_retry?}
end
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/task_serializer.rb
Expand Up @@ -2,7 +2,7 @@ class TaskSerializer
include FastJsonapiCandy::Serializer
set_type 'tasks'

attributes *%i(max_retries current_retries output state task_type)
attributes *%i(max_retries current_retries output state task_type can_execute? performed? failed?)
build_timestamps

build_belongs_to :workflow
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/workflow_serializer.rb
Expand Up @@ -6,5 +6,5 @@ class WorkflowSerializer
build_belongs_to :issue
build_has_many :tasks

attributes *%i(state scope workflow_type)
attributes *%i(state scope workflow_type all_task_in_final_state? started?)
end
14 changes: 13 additions & 1 deletion config/routes.rb
Expand Up @@ -73,7 +73,6 @@

%i(
workflows
tasks
).each do |entities|
resources entities, except: [:new, :edit] do
member do
Expand All @@ -85,6 +84,19 @@
end
end

%i(
tasks
).each do |entities|
resources entities, except: [:new, :edit] do
member do
entities.to_s.classify.constantize
.aasm.events.map(&:name).reject{|x| [:retry].include? x}.each do |action|
post action
end
end
end
end

resources :tasks, except: [:new, :edit] do
member do
post :failure
Expand Down
4 changes: 3 additions & 1 deletion spec/models/task_spec.rb
Expand Up @@ -41,6 +41,7 @@
end

it "goes from retried to performed on finish" do
expect(basic_task.can_execute?).to be true
nubis marked this conversation as resolved.
Show resolved Hide resolved
basic_task.start!
basic_task.failure!
expect do
Expand All @@ -64,12 +65,13 @@
expect(basic_task.reload.current_retries).to eq 1

2.times do |i|
expect(basic_task.can_execute?).to be true
basic_task.failure!
expect(basic_task.reload.current_retries).to eq i + 2
end

basic_task.failure!

expect(basic_task.can_execute?).to be false
expect(basic_task.state).to eq("failed")
end
end
Expand Down