Skip to content

Commit

Permalink
Merge pull request #40 from MineralsCloud:shouldrun
Browse files Browse the repository at this point in the history
Change `shouldrun`, add `skip_incomplete` in `ConditionalJob` and `ArgDependentJob` structs
  • Loading branch information
singularitti authored Jul 5, 2023
2 parents 4a42221 + 4125963 commit 538bbdf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
12 changes: 8 additions & 4 deletions EasyJobsBase/src/jobs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ mutable struct ConditionalJob <: DependentJob
parents::Set{AbstractJob}
"These jobs runs after the current job."
children::Set{AbstractJob}
function ConditionalJob(core::Think; name="", description="", username="")
skip_incomplete::Bool
function ConditionalJob(
core::Think, skip_incomplete=true; name="", description="", username=""
)
return new(
uuid1(),
core,
Expand All @@ -102,6 +105,7 @@ mutable struct ConditionalJob <: DependentJob
0,
Set(),
Set(),
skip_incomplete,
)
end
end
Expand All @@ -122,9 +126,9 @@ mutable struct ArgDependentJob <: DependentJob
parents::Set{AbstractJob}
"These jobs runs after the current job."
children::Set{AbstractJob}
succeededonly::Bool
skip_incomplete::Bool
function ArgDependentJob(
core::Think, succeededonly=true; name="", description="", username=""
core::Think, skip_incomplete=true; name="", description="", username=""
)
return new(
uuid1(),
Expand All @@ -139,7 +143,7 @@ mutable struct ArgDependentJob <: DependentJob
0,
Set(),
Set(),
succeededonly,
skip_incomplete,
)
end
end
32 changes: 15 additions & 17 deletions EasyJobsBase/src/run.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Thinkers: TimeoutException, ErrorInfo, reify!, setargs!, haserred, _kill

export run!, execute!, kill!
export shouldrun, run!, execute!, kill!

# See https://github.com/MineralsCloud/SimpleWorkflows.jl/issues/137
"""
Expand Down Expand Up @@ -123,16 +123,14 @@ prepare!(::AbstractJob) = nothing # No op
function prepare!(job::ArgDependentJob)
# Use previous results as arguments
args = if countparents(job) == 1
result = getresult(only(eachparent(job)))
if isnothing(result) # Parent job is pending or still running
# This means `job.succeededonly` must be `true` based on the logic
# Keep the arguments unchanged
@warn "the parent job is pending or still running! No arguments will be set!"
else # Parent job has succeeded or failed
something(result)
parent = only(eachparent(job))
if issucceeded(parent)
something(getresult(parent))
else
error("the parent job has failed!")
end
else # > 1
parents = if job.succeededonly
parents = if job.skip_incomplete
Iterators.filter(issucceeded, eachparent(job))
else
eachparent(job)
Expand All @@ -143,15 +141,15 @@ function prepare!(job::ArgDependentJob)
return nothing
end

shouldrun(::AbstractJob) = true
shouldrun(job::ConditionalJob) =
countparents(job) >= 1 && all(issucceeded(parent) for parent in eachparent(job))
function shouldrun(job::ArgDependentJob)
if job.succeededonly
return countparents(job) >= 1
shouldrun(::IndependentJob) = true
function shouldrun(job::Union{ConditionalJob,ArgDependentJob})
if countparents(job) < 1
return false
end
if job.skip_incomplete
return all(issucceeded(parent) for parent in eachparent(job))
else
return countparents(job) >= 1 &&
all(issucceeded(parent) for parent in eachparent(job))
return all(isexited(parent) for parent in eachparent(job))
end
end

Expand Down
17 changes: 10 additions & 7 deletions EasyJobsBase/test/run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,17 @@ end
j = ArgDependentJob(Thunk(f₂, 3), false; username="he", name="j")
k = ArgDependentJob(Thunk(f₃, 6), false; username="she", name="k")
i j k
@test_throws AssertionError run!(j)
@test !shouldrun(j)
@test !shouldrun(k)
exec = run!(i)
wait(exec)
@test getresult(i) == Some(25)
@test_throws AssertionError run!(k)
@test shouldrun(j)
@test !shouldrun(k)
exec = run!(j)
wait(exec)
@test getresult(j) == Some(26)
@test shouldrun(k)
exec = run!(k)
wait(exec)
@test getresult(k) == Some(13.0)
Expand All @@ -134,28 +137,28 @@ end
k = Job(Thunk(f₃, 6); username="she", name="k")
l = ArgDependentJob(Thunk(f₄, ()); username="she", name="me")
(i, j, k) .→ l
exec = run!(l)
wait(exec)
@test isfailed(l)
@test !shouldrun(l)
execs = map((i, j, k)) do job
run!(job)
end
for exec in execs
wait(exec)
end
@test shouldrun(l)
l.core = Thunk(l.core)
exec = run!(l)
wait(exec)
@test getresult(i) == Some(25)
@test getresult(j) == Some(4)
@test getresult(k) == Some(3.0)
@test getresult(l) == Some(32.0)
@testset "Change `succeededonly` to `false`" begin
@testset "Change `skip_incomplete` to `false`" begin
i = Job(Thunk(f₁, 5); username="me", name="i")
j = Job(Thunk(f₂, 3); username="he", name="j")
k = Job(Thunk(f₃, 6); username="she", name="k")
l = ArgDependentJob(Thunk(f₄, ()), false; username="she", name="me")
(i, j, k) .→ l
@test !shouldrun(l)
@test_throws AssertionError run!(l)
end
@test_throws AssertionError run!(l)
end

0 comments on commit 538bbdf

Please sign in to comment.