ModernUO/Projects/UOContent/Misc/AutoRestart.cs
Kamron Batman 3f6a87483b
feat: Adds auto archiving (#794)
## Adds Auto Archiving
Backups are archived once an hour, day, and month. Archives older than 60 days are pruned automatically.

_Note: Automatic pruning is off by default_

### Archive compression format
The following formats are supported:
* Zstd (The fastest with best compression ratio)
* GZip
* Zip
* None (Tar)

_Note: By default archives use [zstandard](http://facebook.github.io/zstd/) format.
The archive format can be changed in modernuo.json `autoArchive.compressionFormat`_

### Restoring world from archive
Move the archive to the Saves folder (tar.zst file). On startup the server will extract the file and restore the latest save. See pictures below.

### Manually extracting an archives
#### Windows
* Use the latest version of [7-zip w/ ZStandard](https://github.com/mcmilk/7-Zip-zstd/releases/latest)
  1. Extract the `.tar.zst` file.
  2. Extract the `.tar` file. (Yes you have to do it in two steps)
* On Windows 10 you can use the command line. `zstd.exe` is in the Assemblies folder after building ModernUO.
  1. `tar --use-compress-program "Distribution\Assemblies\zstd.exe -d" -xvf "Archives\Hourly\archivefile.tar.zst" -C "path to where you want to extract it"`
#### Mac
* Install [Keka](https://www.keka.io)
  1. Drop the .tar.zst onto the keka interface.
#### Linux
  1. Install zstd from a package manager
  2. Run `tar -I zstd -xvf "Archives\Hourly\archivefile.tar.zst" -C "path to where you want to extract it"`

### Other changes
* Changes Autosave to occur at the same time no matter when the server is booted.
* Adds `[SaveFrequency <delay> [warning]`command to set save frequency and warning frequency in-game.
* Adds support for time zones that are configurable. The system timezone can also be manually configured. Check `TimeZoneHandler.cs` for details.

<img width="257" alt="Screen Shot 2021-09-22 at 11 23 18 PM" src="https://user-images.githubusercontent.com/3953314/134464929-a5bf3cd8-2ef0-4476-a9ad-71d0816a19ac.png">
<img width="241" alt="Screen Shot 2021-09-22 at 11 23 39 PM" src="https://user-images.githubusercontent.com/3953314/134464945-5f6f96dc-3d1e-434d-8256-5c6b3705e786.png">
<img width="836" alt="Screen Shot 2021-09-22 at 11 34 39 PM" src="https://user-images.githubusercontent.com/3953314/134464957-625e57f2-ef47-4ca1-a0a7-cd40c9b6539c.png">
<img width="631" alt="Screen Shot 2021-09-22 at 11 34 50 PM" src="https://user-images.githubusercontent.com/3953314/134464969-b2d65c0d-f8f5-497a-a832-5d805e8e0b22.png">
2021-09-25 17:22:05 -07:00

113 lines
2.9 KiB
C#

using System;
using Server.Saves;
namespace Server.Misc
{
public class AutoRestart : Timer
{
private static Timer _autoRestart;
private static bool _enabled;
public static bool Enabled
{
get => _enabled;
set
{
_enabled = value;
if (value)
{
_autoRestart ??= new AutoRestart();
_autoRestart.Start();
}
else
{
_autoRestart?.Stop();
_autoRestart = null;
}
}
}
private static readonly TimeSpan RestartTime = TimeSpan.FromHours(2.0); // time of day at which to restart
// how long the server should remain active before restart (period of 'server wars')
private static readonly TimeSpan RestartDelay = TimeSpan.Zero;
// at what interval should the shutdown message be displayed?
private static readonly TimeSpan WarningDelay = TimeSpan.FromMinutes(1.0);
private static DateTime m_RestartTime;
public AutoRestart() : base(TimeSpan.FromSeconds(1.0), TimeSpan.FromSeconds(1.0))
{
m_RestartTime = Core.Now.Date + RestartTime;
if (m_RestartTime < Core.Now)
{
m_RestartTime += TimeSpan.FromDays(1.0);
}
}
public static bool Restarting { get; private set; }
public static void Configure()
{
Enabled = ServerConfiguration.GetOrUpdateSetting("world.enableAutoRestart", false);
}
public static void Initialize()
{
CommandSystem.Register("Restart", AccessLevel.Administrator, Restart_OnCommand);
}
public static void Restart_OnCommand(CommandEventArgs e)
{
if (Restarting)
{
e.Mobile.SendMessage("The server is already restarting.");
}
else
{
e.Mobile.SendMessage("You have initiated server shutdown.");
Enabled = true;
m_RestartTime = Core.Now;
}
}
private static void Warning_Callback()
{
World.Broadcast(0x22, true, "The server is going down shortly.");
}
private static void Restart_Callback()
{
Core.Kill(true);
}
protected override void OnTick()
{
if (Restarting || !Enabled)
{
return;
}
if (Core.Now < m_RestartTime)
{
return;
}
if (WarningDelay > TimeSpan.Zero)
{
Warning_Callback();
StartTimer(WarningDelay, WarningDelay, Warning_Callback);
}
AutoSave.Save();
Restarting = true;
StartTimer(RestartDelay, Restart_Callback);
}
}
}