Skip to content

Commit

Permalink
implemented running_jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
louisponet committed Jun 28, 2021
1 parent 3ec1e3c commit 0fed708
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 27 deletions.
1 change: 1 addition & 0 deletions docs/src/guide/jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Loading all [`DFJobs`](@ref DFJob) that contain a given `fuzzy` can be done thro
```@docs
registered_jobs
load_jobs
running_jobs
```
### Versioning

Expand Down
4 changes: 2 additions & 2 deletions src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export gencalc_scf, gencalc_vcrelax, gencalc_nscf, gencalc_bands, gencalc_projwf
include("jobAPI.jl")
#Basic Job Control Functionality
export save, submit, abort, set_flow!, set_headerword!, isrunning, last_running_calculation,
last_submission,
last_submission,
progressreport, set_serverdir!, set_localdir!, structure, scale_cell!, volume,
switch_version!, version, versions, registered_jobs, rm_version!, rm_versions!,
rm_tmp_dirs!, cleanup, load_jobs, last_version
rm_tmp_dirs!, cleanup, load_jobs, last_version, running_jobs

#Basic Interaction with DFCalculations inside DFJob
export set_cutoffs!
Expand Down
1 change: 0 additions & 1 deletion src/calculation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ end

rm_outfiles(calc::DFCalculation) = rm.(outfiles(calc))


include("qe/calculation.jl")
include("elk/calculation.jl")
include("wannier90/calculation.jl")
21 changes: 21 additions & 0 deletions src/execs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,24 @@ function exec(calculation::DFCalculation, exec::String)
return getfirst(x -> occursin(exec, x.exec), calculation.execs)
end
exec(job::DFJob, exec::String) = getfirst(x -> occursin(exec, x.exec), execs(job))


function Base.string(e::Exec)
direxec = joinpath(e.dir, e.exec)
str = "$direxec"
for flag in e.flags
str *= " $(join(fill('-', flag.minus_count)))$(flag.symbol)"
if !isa(flag.value, AbstractString)
for v in flag.value
str *= " $v"
end
else
str *= " $(flag.value)"
end
end
return str
end




17 changes: 2 additions & 15 deletions src/fileio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,8 @@ function writetojob(f, job, calculations::Vector{DFCalculation{Elk}}; kwargs...)
return calculations
end

function writeexec(f, exec::Exec)
direxec = joinpath(exec.dir, exec.exec)
write(f, "$direxec")
for flag in exec.flags
write(f, " $(join(fill('-', flag.minus_count)))$(flag.symbol)")
if !isa(flag.value, AbstractString)
for v in flag.value
write(f, " $v")
end
else
write(f, " $(flag.value)")
end
end
return write(f, " ")
end
writeexec(f, exec::Exec) =
write(f, string(exec) * " ")

function writetojob(f, job, calculation::DFCalculation; kwargs...)
filename = infilename(calculation)
Expand Down
26 changes: 21 additions & 5 deletions src/job.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,17 +220,33 @@ is_slurm_job(job::DFJob) = haskey(job.metadata, :slurmid)
"""
isrunning(job::DFJob; print=true)
If the job was submitted through a scheduler like `slurm`,
this will return whether the job is queued or running.
Returns whether a job is running or not. If the job was
submitted using `slurm`, a `QUEUED` status also counts as
running.
**Note:**
For now only `slurm` is supported as scheduler.
!!! note:
For now only `slurm` is supported as scheduler.
"""
function isrunning(job::DFJob; print = true)
n = now()
if is_slurm_job(job)
return slurm_isrunning(job)
else
u = username()
l = last_running_calculation(job)
l === nothing && return false
codeexec = execs(l)[end].exec
try
pids = parse.(Int, split(read(`pgrep $codeexec`, String)))
if isempty(pids)
return false
end
pwd = split(strip(read(`pwdx $(pids[end])`, String)))[end]
return abspath(pwd) == job.local_dir
catch
return false
end
end
print && @warn "Job scheduler unknown."
return false
end

Expand Down
4 changes: 4 additions & 0 deletions src/jobAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function submit(job::DFJob; server = job.server, server_dir = job.server_dir, kw
job.metadata[:slurmid] = sbatch(job)
save_metadata(job)
catch
pop!(job.metadata, :slurmid, nothing)
run(job)
end
end
Expand Down Expand Up @@ -171,6 +172,9 @@ function set_localdir!(job::DFJob, dir::AbstractString; copy = false)
if !isabspath(dir)
dir = abspath(dir)
end
if dir[end] == '/'
dir = dir[1:end-1]
end
if copy
verify_or_create(dir)
cp(job, dir; temp = true)
Expand Down
17 changes: 13 additions & 4 deletions src/registry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ end
maybe_register_job(job::DFJob) = maybe_register_job(job.local_dir)

"""
registered_jobs(fuzzy::AbstractString)
registered_jobs(fuzzy::AbstractString = "")
Lists all the known [`DFJob](@ref) directories that contain `fuzzy`.
Lists all the known [`DFJobs`](@ref DFJob) directories that contain `fuzzy`.
Intended to be used as:
```julia
job_dirs = registered_jobs("NiO")
job = DFJob(job_dirs[1])
```
"""
registered_jobs(fuzzy::AbstractString) = filter(x -> occursin(fuzzy, x), JOB_REGISTRY)
registered_jobs(fuzzy::AbstractString = "") = (cleanup_job_registry(; print=false); filter(x -> occursin(fuzzy, x), JOB_REGISTRY))

"""
load_jobs(fuzzy::AbstractString)
Expand All @@ -55,7 +55,6 @@ Loads all the known [`DFJobs`](@ref DFJob) whose `local_dir` contains `fuzzy`.
load_jobs(fuzzy::AbstractString) = DFJob.(registered_jobs(fuzzy))

function request_job(job_dir::String)
cleanup_job_registry(; print = false)
function timestamp(jobdir)
if ispath(joinpath(jobdir, ".metadata.jld2"))
md = load(joinpath(jobdir, ".metadata.jld2"))["metadata"]
Expand Down Expand Up @@ -88,3 +87,13 @@ function request_job(job_dir::String)
@error err_msg
end
end

"""
running_jobs(fuzzy::AbstractString = "")
Loads all [`DFJobs`](@ref DFJob) that are currently running.
"""
function running_jobs(fuzzy::AbstractString = "")
jobs = load_jobs(fuzzy)
return filter(isrunning, jobs)
end
2 changes: 2 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,5 @@ end
yesterday() = today() - Day(1)
lastweek() = today() - Week(1)
lastmonth() = today() - Month(1)

username() = read(`whoami`, String)

0 comments on commit 0fed708

Please sign in to comment.