Skip to content

Commit

Permalink
versioning updates
Browse files Browse the repository at this point in the history
  • Loading branch information
louisponet committed Jun 21, 2021
1 parent 2abb83b commit e986c6e
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 36 deletions.
24 changes: 22 additions & 2 deletions src/job.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ function sanitize_pseudos!(job::DFJob)
all_pseudos = pseudo.(atoms(job))
uni_dirs = unique(map(x->x.dir, all_pseudos))
uni_pseudos = unique(all_pseudos)
@assert all(ispath.(path.(uni_pseudos))) "Some Pseudos could not be found, please set them to existing ones."
if !all(ispath.(path.(uni_pseudos)))
@warn "Some Pseudos could not be located on disk."
end
pseudo_dir = length(uni_dirs) == 1 ? uni_dirs[1] : job.local_dir
if length(uni_dirs) > 1
@info "Found pseudos in multiple directories, copying them to job directory"
Expand Down Expand Up @@ -182,6 +184,9 @@ for (f, strs) in zip((:cp, :mv), (("copy", "Copies"), ("move", "Moves")))
The `kwargs...` are passed to `Base.$($f)`.
"""
function Base.$f(job::DFJob, dest::String; all=false, temp=false, kwargs...)
if !ispath(dest)
mkpath(dest)
end
for file in readdir(job.local_dir)
if !all
if file == VERSION_DIR_NAME
Expand Down Expand Up @@ -265,7 +270,22 @@ function cleanup(job::DFJob)
end
end

save_metadata(job) = jldsave(joinpath(job, ".metadata.jld2"); metadata=job.metadata)
save_metadata(job) = jldsave(joinpath(job, ".metadata.jld2");
metadata=job.metadata,
version=job.version)

timestamp(job) = job.metadata[:timestamp]
timestamp!(job, time) = job.metadata[:timestamp] = time
has_timestamp(job) = haskey(job.metadata, :timestamp)


function clean_local_dir!(job::DFJob)
for f in readdir(job.local_dir)
if f == TEMP_CALC_DIR || f == VERSION_DIR_NAME || splitext(f)[end] == ".jl"
continue
end
rm(joinpath(job, f), recursive=true)
end
end


6 changes: 4 additions & 2 deletions src/jobAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ function save(job::DFJob, local_dir=job.local_dir; kwargs...)
job.name = "noname"
end
maybe_register_job(job)
maybe_increment_version(job)

job.version = last_version(job) + 1
maybe_cp_prev_version(job)
sanitize_cutoffs!(job)
sanitize_pseudos!(job)
sanitize_magnetization!(job)
sanitize_projections!(job)
sanitize_flags!(job)
timestamp(job) = now()
timestamp!(job,now())
save_metadata(job)
return writejobfiles(job; kwargs...)
end
Expand Down
15 changes: 10 additions & 5 deletions src/serverAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@ Returns whether the job is running.
function slurm_isrunning(job::DFJob)
runslocal_assert(job)
id = slurm_jobid(job)
if id != -1
line = getfirst(x -> occursin("JobState", x), slurm_process_command(`scontrol show job $id`))
return split(split(line)[1], "=")[2] ∈ ("RUNNING", "PENDING", "CONFIGURING")
else
@warn "No jobid found. Was your job submitted through slurm?"
try
if id != -1
result = slurm_process_command(`scontrol show job $id`)
line = getfirst(x -> occursin("JobState", x), result)
return split(split(line)[1], "=")[2] ∈ ("RUNNING", "PENDING", "CONFIGURING")
else
@warn "No jobid found. Was your job submitted through slurm?"
return false
end
catch
return false
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ The `kwargs...` will be passed to the [`DFJob`](@ref) constructor.
if isempty(metadata)
mpath = joinpath(local_dir, ".metadata.jld2")
if ispath(mpath)
metadata = load(mpath)["metadata"]
stored_data = load(mpath)
metadata = haskey(stored_data, "metadata") ? stored_data["metadata"] : metadata
version = haskey(stored_data, "version") ? stored_data["version"] : version
end
end
out = new(name, structure, calculations, local_dir, header, metadata, version, copy_temp_folders, server, server_dir)
Expand Down
77 changes: 51 additions & 26 deletions src/versioning.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const VERSION_DIR_NAME = ".versions"
function job_versions(dir::String)
verdir = joinpath(dir, VERSION_DIR_NAME)
if ispath(verdir)
return parse.(Int, readdir(joinpath(dir, VERSION_DIR_NAME)))
return sort(parse.(Int, readdir(joinpath(dir, VERSION_DIR_NAME))))
else
return Int[]
end
Expand All @@ -28,24 +28,28 @@ version_path(job::DFJob) = version_path(job.local_dir, job.version)
version_path(job::DFJob, version::Int) = version_path(job.local_dir, version)

exists_version(dir::String, version::Int) = version ∈ job_versions(dir)
exists_version(job::DFJob, version::Int) = exists_version(job.local_dir, version)

function maybe_increment_version(job::DFJob)
"""
maybe_cp_prev_version(job::DFJob)

Looks in the `job.local_dir` for a previous version of the job, and copies it to the
respective directory in the `.versions`.
"""
function maybe_cp_prev_version(job::DFJob)
versions_path = joinpath(job, VERSION_DIR_NAME)
if !ispath(versions_path)
mkpath(versions_path)
job.version = 1
end
if ispath(joinpath(job, "job.tt"))
tjob = DFJob(job.local_dir, version = last_version(job) + 1)
tjob = DFJob(job.local_dir)
vpath = version_path(tjob)
mkpath(vpath)
cp(job, vpath)
job.version = last_version(job) + 1
cp(tjob, vpath, force=true)
end
end

"""
switch_version(job::DFJob, version::Int)
switch_version(job::DFJob[, version::Int])

Switches the version of `job` to one of the previously stored ones.
It will save also the current version for future reference.
Expand All @@ -55,38 +59,59 @@ function switch_version(job::DFJob, version::Int)
cur_version = job.version
if version != cur_version
verpath = version_path(job.local_dir, version)
if !ispath(verpath)
err_str = "Requested job version ($version) is invalid, please choose from:\n"
for jv in versions(job)
err_str *= "\t$jv\n"
end
@error err_str
else
maybe_increment_version(job)
out = DFJob(verpath)
curdir = job.local_dir
mv(out, job.local_dir, force=true)
rm(verpath, recursive=true)
for f in fieldnames(DFJob)
setfield!(job, f, getfield(out,f))
end
set_localdir!(job, curdir)
job.version = version
version_assert(job, version)
maybe_cp_prev_version(job)
clean_local_dir!(job)
out = DFJob(verpath)
curdir = job.local_dir
cp(out, job.local_dir, force=true)
for f in fieldnames(DFJob)
setfield!(job, f, getfield(out,f))
end
set_localdir!(job, curdir)
job.version = version
end
return job
end

function switch_version(job::DFJob)
vs = versions(job)
timestamps = []
for v in vs
mdatapath = joinpath(version_path(job, v), ".metadata.jld2")
if ispath(mdatapath) && haskey(load(mdatapath), "metadata") && haskey(load(mdatapath)["metadata"], :timestamp)
push!(timestamps, string(load(mdatapath)["metadata"][:timestamp]))
else
push!(timestamps, "")
end
end


menu = RadioMenu(join.(zip(string.(vs), timestamps), (" ",)))
choice = request("Please select which version to switch to:", menu)
if choice != -1
return switch_version(job, vs[choice])
end
end

version_assert(job, version) = @assert exists_version(job, version) "Version $version does not exist for job."

"""
rm_version!(job::DFJob, version::Int)
rm_versions!(job::DFJob, versions::Int...)

Removes the specified `versions` from the `job` if they exists.
Removes the specified `versions` from the `job` if they exist.
"""
function rm_version!(job::DFJob, version::Int)
version_assert(job, version)
if version == job.version
@warn "Job version is the same as the one to be removed, switching to last known version."
lv = last_version(job)
if lv == version
lv = versions(job)[end-1]
end
switch_version(job, lv)
end
rm(version_path(job, version), recursive=true)
end

Expand Down

0 comments on commit e986c6e

Please sign in to comment.