Skip to content

Commit 94841b6

Browse files
convert write_env_usage to use a pidfile lock
1 parent 8ccdfdf commit 94841b6

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/Types.jl

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import ..Pkg, ..Registry
1515
import ..Pkg: GitTools, depots, depots1, logdir, set_readonly, safe_realpath, pkg_server, stdlib_dir, stdlib_path, isurl, stderr_f
1616
import Base.BinaryPlatforms: Platform
1717
using ..Pkg.Versions
18+
import ..Pkg.Pidfile
1819

1920
import Base: SHA1
2021
using SHA
@@ -477,16 +478,31 @@ function write_env_usage(source_file::AbstractString, usage_filepath::AbstractSt
477478
# Ensure that log dir exists
478479
!ispath(logdir()) && mkpath(logdir())
479480

480-
# Generate entire entry as a string first
481-
entry = sprint() do io
482-
TOML.print(io, Dict(source_file => [Dict("time" => now())]))
483-
end
484-
485-
# Append entry to log file in one chunk
486481
usage_file = joinpath(logdir(), usage_filepath)
487-
open(usage_file, append=true) do io
488-
write(io, entry)
482+
timestamp = now()
483+
484+
## Atomically write usage file using process id locking
485+
Pidfile.mkpidlock(usage_file * ".pidlock") do
486+
usage = if isfile(usage_file)
487+
TOML.parsefile(usage_file)
488+
else
489+
Dict{String, Any}()
490+
end
491+
492+
# record new usage
493+
usage[source_file] = [Dict("time" => timestamp)]
494+
495+
# keep only latest usage info
496+
for k in keys(usage)
497+
times = map(d -> Dates.DateTime(d["time"]), usage[k])
498+
usage[k] = [Dict("time" => maximum(times))]
499+
end
500+
501+
open(usage_file, "w") do io
502+
TOML.print(io, usage, sorted=true)
503+
end
489504
end
505+
return
490506
end
491507

492508
function read_package(path::String)

0 commit comments

Comments
 (0)