Skip to content

Commit 0fed708

Browse files
committed
implemented running_jobs
1 parent 3ec1e3c commit 0fed708

File tree

9 files changed

+66
-27
lines changed

9 files changed

+66
-27
lines changed

docs/src/guide/jobs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Loading all [`DFJobs`](@ref DFJob) that contain a given `fuzzy` can be done thro
6666
```@docs
6767
registered_jobs
6868
load_jobs
69+
running_jobs
6970
```
7071
### Versioning
7172

src/API.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export gencalc_scf, gencalc_vcrelax, gencalc_nscf, gencalc_bands, gencalc_projwf
1818
include("jobAPI.jl")
1919
#Basic Job Control Functionality
2020
export save, submit, abort, set_flow!, set_headerword!, isrunning, last_running_calculation,
21-
last_submission,
21+
last_submission,
2222
progressreport, set_serverdir!, set_localdir!, structure, scale_cell!, volume,
2323
switch_version!, version, versions, registered_jobs, rm_version!, rm_versions!,
24-
rm_tmp_dirs!, cleanup, load_jobs, last_version
24+
rm_tmp_dirs!, cleanup, load_jobs, last_version, running_jobs
2525

2626
#Basic Interaction with DFCalculations inside DFJob
2727
export set_cutoffs!

src/calculation.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ end
148148

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

151-
152151
include("qe/calculation.jl")
153152
include("elk/calculation.jl")
154153
include("wannier90/calculation.jl")

src/execs.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,24 @@ function exec(calculation::DFCalculation, exec::String)
228228
return getfirst(x -> occursin(exec, x.exec), calculation.execs)
229229
end
230230
exec(job::DFJob, exec::String) = getfirst(x -> occursin(exec, x.exec), execs(job))
231+
232+
233+
function Base.string(e::Exec)
234+
direxec = joinpath(e.dir, e.exec)
235+
str = "$direxec"
236+
for flag in e.flags
237+
str *= " $(join(fill('-', flag.minus_count)))$(flag.symbol)"
238+
if !isa(flag.value, AbstractString)
239+
for v in flag.value
240+
str *= " $v"
241+
end
242+
else
243+
str *= " $(flag.value)"
244+
end
245+
end
246+
return str
247+
end
248+
249+
250+
251+

src/fileio.jl

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -180,21 +180,8 @@ function writetojob(f, job, calculations::Vector{DFCalculation{Elk}}; kwargs...)
180180
return calculations
181181
end
182182

183-
function writeexec(f, exec::Exec)
184-
direxec = joinpath(exec.dir, exec.exec)
185-
write(f, "$direxec")
186-
for flag in exec.flags
187-
write(f, " $(join(fill('-', flag.minus_count)))$(flag.symbol)")
188-
if !isa(flag.value, AbstractString)
189-
for v in flag.value
190-
write(f, " $v")
191-
end
192-
else
193-
write(f, " $(flag.value)")
194-
end
195-
end
196-
return write(f, " ")
197-
end
183+
writeexec(f, exec::Exec) =
184+
write(f, string(exec) * " ")
198185

199186
function writetojob(f, job, calculation::DFCalculation; kwargs...)
200187
filename = infilename(calculation)

src/job.jl

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,33 @@ is_slurm_job(job::DFJob) = haskey(job.metadata, :slurmid)
220220
"""
221221
isrunning(job::DFJob; print=true)
222222
223-
If the job was submitted through a scheduler like `slurm`,
224-
this will return whether the job is queued or running.
223+
Returns whether a job is running or not. If the job was
224+
submitted using `slurm`, a `QUEUED` status also counts as
225+
running.
225226
226-
**Note:**
227-
For now only `slurm` is supported as scheduler.
227+
!!! note:
228+
For now only `slurm` is supported as scheduler.
228229
"""
229230
function isrunning(job::DFJob; print = true)
231+
n = now()
230232
if is_slurm_job(job)
231233
return slurm_isrunning(job)
234+
else
235+
u = username()
236+
l = last_running_calculation(job)
237+
l === nothing && return false
238+
codeexec = execs(l)[end].exec
239+
try
240+
pids = parse.(Int, split(read(`pgrep $codeexec`, String)))
241+
if isempty(pids)
242+
return false
243+
end
244+
pwd = split(strip(read(`pwdx $(pids[end])`, String)))[end]
245+
return abspath(pwd) == job.local_dir
246+
catch
247+
return false
248+
end
232249
end
233-
print && @warn "Job scheduler unknown."
234250
return false
235251
end
236252

src/jobAPI.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ function submit(job::DFJob; server = job.server, server_dir = job.server_dir, kw
6363
job.metadata[:slurmid] = sbatch(job)
6464
save_metadata(job)
6565
catch
66+
pop!(job.metadata, :slurmid, nothing)
6667
run(job)
6768
end
6869
end
@@ -171,6 +172,9 @@ function set_localdir!(job::DFJob, dir::AbstractString; copy = false)
171172
if !isabspath(dir)
172173
dir = abspath(dir)
173174
end
175+
if dir[end] == '/'
176+
dir = dir[1:end-1]
177+
end
174178
if copy
175179
verify_or_create(dir)
176180
cp(job, dir; temp = true)

src/registry.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ end
3636
maybe_register_job(job::DFJob) = maybe_register_job(job.local_dir)
3737

3838
"""
39-
registered_jobs(fuzzy::AbstractString)
39+
registered_jobs(fuzzy::AbstractString = "")
4040
41-
Lists all the known [`DFJob](@ref) directories that contain `fuzzy`.
41+
Lists all the known [`DFJobs`](@ref DFJob) directories that contain `fuzzy`.
4242
Intended to be used as:
4343
```julia
4444
job_dirs = registered_jobs("NiO")
4545
job = DFJob(job_dirs[1])
4646
```
4747
"""
48-
registered_jobs(fuzzy::AbstractString) = filter(x -> occursin(fuzzy, x), JOB_REGISTRY)
48+
registered_jobs(fuzzy::AbstractString = "") = (cleanup_job_registry(; print=false); filter(x -> occursin(fuzzy, x), JOB_REGISTRY))
4949

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

5757
function request_job(job_dir::String)
58-
cleanup_job_registry(; print = false)
5958
function timestamp(jobdir)
6059
if ispath(joinpath(jobdir, ".metadata.jld2"))
6160
md = load(joinpath(jobdir, ".metadata.jld2"))["metadata"]
@@ -88,3 +87,13 @@ function request_job(job_dir::String)
8887
@error err_msg
8988
end
9089
end
90+
91+
"""
92+
running_jobs(fuzzy::AbstractString = "")
93+
94+
Loads all [`DFJobs`](@ref DFJob) that are currently running.
95+
"""
96+
function running_jobs(fuzzy::AbstractString = "")
97+
jobs = load_jobs(fuzzy)
98+
return filter(isrunning, jobs)
99+
end

src/utils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,5 @@ end
203203
yesterday() = today() - Day(1)
204204
lastweek() = today() - Week(1)
205205
lastmonth() = today() - Month(1)
206+
207+
username() = read(`whoami`, String)

0 commit comments

Comments
 (0)