Skip to content

Commit b1cba56

Browse files
authored
Automatically call Pkg.gc() after up, pin, free and rm (#2567)
* Automatically call `Pkg.gc()` after `up`, `pin`, `free` and `rm` We want to call `Pkg.gc()` every now and then, so let's automatically call it after operations that can themselves result in orphaned package versions (e.g. `rm` or `up`). We wait the default GC collect interval, using `mtime(orphanage_path)` as our marker of the last time we have collected. After each of the above operations, we query the current time as compared to the last known GC time, hitting the filesystem only when there is a possibility that we might need to GC. Also, we now always write out an orphanage file (even if it's empty) to ensure that the timestamp is always available.
1 parent 6c98f54 commit b1cba56

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

src/API.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ for f in (:develop, :add, :rm, :up, :pin, :free, :test, :build, :status)
148148
foreach(pkg -> handle_package_input!(pkg), pkgs)
149149
ret = $f(ctx, pkgs; kwargs...)
150150
$(f in (:add, :up, :pin, :free, :build)) && Pkg._auto_precompile(ctx)
151+
$(f in (:up, :pin, :free, :rm)) && Pkg._auto_gc(ctx)
151152
return ret
152153
end
153154
$f(ctx::Context; kwargs...) = $f(ctx, PackageSpec[]; kwargs...)
@@ -847,11 +848,9 @@ function gc(ctx::Context=Context(); collect_delay::Period=Day(7), verbose=false,
847848
merge_orphanages!(new_orphanage, depot_orphaned_scratchspaces, spaces_to_delete, old_orphanage)
848849

849850
# Write out the `new_orphanage` for this depot
850-
if !isempty(new_orphanage) || isfile(orphanage_file)
851-
mkpath(dirname(orphanage_file))
852-
open(orphanage_file, "w") do io
853-
TOML.print(io, new_orphanage, sorted=true)
854-
end
851+
mkpath(dirname(orphanage_file))
852+
open(orphanage_file, "w") do io
853+
TOML.print(io, new_orphanage, sorted=true)
855854
end
856855
end
857856

src/Pkg.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Pkg
55
import Random
66
import REPL
77
import TOML
8+
using Dates
89

910
export @pkg_str
1011
export PackageSpec
@@ -607,6 +608,41 @@ function dir(pkg::String, paths::AbstractString...)
607608
return abspath(path, "..", "..", paths...)
608609
end
609610

611+
###########
612+
# AUTO GC #
613+
###########
614+
615+
const DEPOT_ORPHANAGE_TIMESTAMPS = Dict{String,Float64}()
616+
const _auto_gc_enabled = Ref{Bool}(true)
617+
function _auto_gc(ctx::Types.Context; collect_delay::Period = Day(7))
618+
if !_auto_gc_enabled[]
619+
return
620+
end
621+
622+
# If we don't know the last time this depot was GC'ed (because this is the
623+
# first time we've looked this session), or it looks like we might want to
624+
# collect; let's go ahead and hit the filesystem to find the mtime of the
625+
# `orphaned.toml` file, which should tell us how long since the last time
626+
# we GC'ed.
627+
orphanage_path = joinpath(logdir(depots1()), "orphaned.toml")
628+
delay_secs = Second(collect_delay).value
629+
curr_time = time()
630+
if curr_time - get(DEPOT_ORPHANAGE_TIMESTAMPS, depots1(), 0.0) >= delay_secs
631+
DEPOT_ORPHANAGE_TIMESTAMPS[depots1()] = mtime(orphanage_path)
632+
end
633+
634+
if curr_time - DEPOT_ORPHANAGE_TIMESTAMPS[depots1()] > delay_secs
635+
@info("We haven't cleaned this depot up for a bit, running Pkg.gc()...")
636+
try
637+
Pkg.gc(ctx; collect_delay)
638+
DEPOT_ORPHANAGE_TIMESTAMPS[depots1()] = curr_time
639+
catch ex
640+
@error("GC failed", exception=ex)
641+
end
642+
end
643+
end
644+
645+
610646
##################
611647
# Precompilation #
612648
##################

test/new.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ unicode_uuid = UUID("4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5")
1616
unregistered_uuid = UUID("dcb67f36-efa0-11e8-0cef-2fc465ed98ae")
1717
simple_package_uuid = UUID("fc6b7c0f-8a2f-4256-bbf4-8c72c30df5be")
1818

19+
# Disable auto-gc for these tests
20+
Pkg._auto_gc_enabled[] = false
1921

2022
#
2123
# # Depot Changes

0 commit comments

Comments
 (0)