Skip to content

Commit

Permalink
Auto-update index periodically (#1447)
Browse files Browse the repository at this point in the history
* Auto-refresh indexes after some time (default 24h)

* ask user on first autoupdate

* User changes

* Test behavior on network failure
  • Loading branch information
mosteo authored Oct 4, 2023
1 parent ffc5659 commit aab567a
Show file tree
Hide file tree
Showing 27 changed files with 458 additions and 81 deletions.
12 changes: 12 additions & 0 deletions doc/user-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ stay on top of `alr` new features.

## Release `2.0-dev`

### Automatic index updates

PR [#1447](https://github.com/alire-project/alire/pull/1447)

A new configuration option, `index.auto_update`, allows setting the refresh
period of indexes. It defaults to 24 hours and the user will be asked the first
time to allow automatic updates. Setting this option to 0 will also disable
automatic updates.

When enabled, updates may happen before executing commands that rely on
indexes: `get`, `search`, `with`, etc.

### Deprecation of `dependencies.dir` in favor of `dependencies.shared`

PR [#1419](https://github.com/alire-project/alire/pull/1419)
Expand Down
20 changes: 20 additions & 0 deletions src/alire/alire-config-builtins.ads
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ package Alire.Config.Builtins is
"When unset or true, the community index will be added " &
"automatically when required if no other index is configured.");

Index_Auto_Update : constant Builtin := New_Builtin
(Key => "index.auto_update",
Kind => Cfg_Int,
Def => "24", -- hours
Help =>
"Hours between automatic index refresh. Set to 0 to disable.");

Index_Auto_Update_Asked : constant Builtin := New_Builtin
(Key => "index.auto_update_asked",
Def => False,
Public => False,
Help => "First time we must autoupdate, we ask the user to approve");

Index_Last_Update : constant Builtin := New_Builtin
(Key => "index.last_update",
Public => False,
Kind => Cfg_Int,
Def => "0", -- seconds since epoch
Help => "Timestamp of last index auto-refresh (seconds)");

Index_Host : constant Builtin := New_Builtin
(Key => "index.host",
Kind => Cfg_String,
Expand Down
20 changes: 20 additions & 0 deletions src/alire/alire-config.adb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ package body Alire.Config is
function Get (This : Builtin_Option) return Boolean
is (DB.Get (+This.Key, Boolean'Value (+This.Def)));

---------
-- Get --
---------

function Get (This : Builtin_Option) return Config_Int
is (Config_Int'Value
(DB.Get_As_String (+This.Key, +This.Def)));

-----------------
-- Set_Locally --
-----------------
Expand Down Expand Up @@ -58,6 +66,18 @@ package body Alire.Config is
Edit.Set_Boolean (Level, +This.Key, Value);
end Set;

---------
-- Set --
---------

procedure Set (This : Builtin_Option;
Level : Config.Level;
Value : Config_Int)
is
begin
Edit.Set (Level, +This.Key, Value'Image, This.Check);
end Set;

-----------
-- Unset --
-----------
Expand Down
7 changes: 7 additions & 0 deletions src/alire/alire-config.ads
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package Alire.Config is
-- Ordering is important, as Globals are loaded first and overridden by any
-- Local definition loaded later.

subtype Config_Int is Long_Long_Integer;

---------------
-- Built-ins --
---------------
Expand All @@ -41,6 +43,7 @@ package Alire.Config is

function Get (This : Builtin_Option) return String;
function Get (This : Builtin_Option) return Boolean;
function Get (This : Builtin_Option) return Config_Int;

procedure Set_Locally (This : Builtin_Option; Value : String);

Expand All @@ -54,6 +57,10 @@ package Alire.Config is
Level : Config.Level;
Value : Boolean);

procedure Set (This : Builtin_Option;
Level : Config.Level;
Value : Config_Int);

procedure Unset (This : Builtin_Option;
Level : Config.Level);

Expand Down
84 changes: 35 additions & 49 deletions src/alire/alire-index_on_disk-loading.adb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ with Alire.Config.Builtins;
with Alire.Config.Edit;
with Alire.Containers;
with Alire.Index;
with Alire.Index_On_Disk.Updates;
with Alire.Platforms.Current;
with Alire.Provides;
with Alire.TOML_Adapters;
Expand Down Expand Up @@ -46,10 +47,6 @@ package body Alire.Index_On_Disk.Loading is
-- metadata file doesn't exist, a full index load will be triggered and
-- the file will be rebuilt.

procedure Invalidate_Providers (Indexes_Dir : Any_Path);
-- Whenever an index is added or updated, we must invalidate the cache on
-- disk containing crate virtual providers.

procedure Save_Providers (Indexes_Dir : Any_Path);
-- Write to disk the providers info already in memory (generated after a
-- full load).
Expand Down Expand Up @@ -196,6 +193,22 @@ package body Alire.Index_On_Disk.Loading is
Result,
Cached => False);
use Sets;

------------------
-- Actually_Add --
------------------

function Actually_Add (Before : String := "") return Outcome is
begin
Updates.Reset_Update_Time;

return Add (Origin => Alire.Index.Community_Repo &
"#" & Alire.Index.Community_Branch,
Name => Alire.Index.Community_Name,
Under => Config.Edit.Indexes_Directory,
Before => Before);
end Actually_Add;

begin
if not Config.Builtins.Index_Auto_Community.Get then
Warnings.Warn_Once
Expand All @@ -212,13 +225,9 @@ package body Alire.Index_On_Disk.Loading is
Trace.Debug ("Index was already set, deleting and re-adding...");
Assert (Indexes (I).Delete);

return Add (Origin => Alire.Index.Community_Repo &
"#" & Alire.Index.Community_Branch,
Name => Alire.Index.Community_Name,
Under => Config.Edit.Indexes_Directory,
Before => (if Has_Element (Next (I))
then Indexes (Next (I)).Name
else ""));
return Actually_Add (Before => (if Has_Element (Next (I))
then Indexes (Next (I)).Name
else ""));
end if;
end loop;

Expand All @@ -228,10 +237,7 @@ package body Alire.Index_On_Disk.Loading is
-- Reset cache so next detection finds the new index

Trace.Debug ("Index was not set, adding it...");
return Add (Origin => Alire.Index.Community_Repo &
"#" & Alire.Index.Community_Branch,
Name => Alire.Index.Community_Name,
Under => Config.Edit.Indexes_Directory);
return Actually_Add;
exception
when E : Checked_Error =>
return Outcome_From_Exception (E);
Expand Down Expand Up @@ -372,6 +378,20 @@ package body Alire.Index_On_Disk.Loading is
return Indexes;
end Find_All;

---------------------
-- Index_Available --
---------------------

function Index_Available (Under : Absolute_Path := Default_Path)
return Boolean
is
Result : Outcome;
Index : constant Set := Find_All (Under, Result);
begin
Result.Assert;
return not Index.Is_Empty;
end Index_Available;

----------
-- Load --
----------
Expand Down Expand Up @@ -628,38 +648,4 @@ package body Alire.Index_On_Disk.Loading is
end if;
end Save_Providers;

----------------
-- Update_All --
----------------

function Update_All (Under : Absolute_Path) return Outcome is
Result : Outcome;
Indexes : constant Set := Find_All (Under, Result);
begin
if not Result.Success then
return Result;
end if;

-- First, invalidate providers metadata as this may change with the
-- update.

Invalidate_Providers (Under);

-- Now update normally

for Index of Indexes loop
declare
Result : constant Outcome := Index.Update;
begin
if Result.Success then
Trace.Detail ("Updated successfully: " & Index.Origin);
else
return Result;
end if;
end;
end loop;

return Outcome_Success;
end Update_All;

end Alire.Index_On_Disk.Loading;
11 changes: 8 additions & 3 deletions src/alire/alire-index_on_disk-loading.ads
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ package Alire.Index_On_Disk.Loading is
-- instead of proceeding with default behaviors, such as getting the
-- community index.

function Index_Available (Under : Absolute_Path := Default_Path)
return Boolean;
-- Says if there's any available index configured

procedure Setup (From : Absolute_Path := Default_Path);
-- If no index is configured, set up the default community index

Expand All @@ -51,9 +55,6 @@ package Alire.Index_On_Disk.Loading is
-- If no path for detection is given, default one is used.
-- May raise Checked_Error. Loading twice the same crate is idempotent.

function Update_All (Under : Absolute_Path) return Outcome;
-- Find and update all indexes at given location

function Add (Origin : URL;
Name : String;
Under : Absolute_Path;
Expand All @@ -70,6 +71,10 @@ package Alire.Index_On_Disk.Loading is
procedure Drop_Index_Cache;
-- Force detection of indexes on disk on next call to Find_All

procedure Invalidate_Providers (Indexes_Dir : Any_Path);
-- Whenever an index is added or updated, we must invalidate the cache on
-- disk containing crate virtual providers.

private

function Default return Set is (Sets.Empty_Set with null record);
Expand Down
Loading

0 comments on commit aab567a

Please sign in to comment.