Owner review 2, R10: nothing may run out of sight, so every timer the server ran becomes a system schedule the admin panel shows and manages, and administrators create their own. - SchedulerHost runs the shared Scheduler on the job engine (started after JobEngineHost, stopped before it); ServerScheduleHost gives it the container's time zone (UTC with a warning when TZ is unknown), the archive refreshed from archive.json at every run (what ArchivePolling did), the clean-up steps and the system schedules: archive-scan every 10 minutes (no catch-up, also run once at start like the polling), upload-cleanup every 30 minutes, index-backup at 03:00 (SQLite only), housekeeping (job history) at 04:00 and audit-cleanup at 04:30. - ArchivePolling and DatabaseBackupService are gone; UploadMaintenance only recovers commits at start, its sweep is the "uploads" clean-up step. - /api/v1/schedules (ManageArchive, audited): list with actions and time zone, create, PUT/PATCH, delete (409 schedule_system), run now (409 schedule_pending), reset (409 schedule_no_defaults), runs paged by cursor; invalid fields are 400 invalid_schedule naming the field. - CHRONOS_AUDIT_RETENTION_DAYS (AuditRetentionDays, default 365) seeds the audit clean-up; set by the environment it is applied at every start and locked. Job history leaves Settings: it is the housekeeping schedule's age limit, and the setting is what a reset restores. - Admin panel: Jobs has Queue and Scheduled tabs; schedules with when (in the panel's language and the server's zone), next run, last result, a switch, Run now, Edit, Delete or Reset, their runs linking to the jobs, and a side panel for the action, recurrence and parameters; job rows a schedule queued carry a Scheduled pill. Strings in en and it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 808ce94ae29e3bf4522cb4d40dfd76d4f8529c64)
129 lines
6.2 KiB
C#
129 lines
6.2 KiB
C#
using System.Net;
|
|
using System.Net.Http.Json;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data;
|
|
using Chronos.Data.Jobs;
|
|
using Chronos.Protocol;
|
|
using Chronos.Server.Configuration;
|
|
using Chronos.Server.Tests.Jobs;
|
|
using Chronos.Testing;
|
|
|
|
namespace Chronos.Server.Tests.Postgres;
|
|
|
|
/// <summary>The server on PostgreSQL (<c>CHRONOS_TEST_PG</c>), each test in a private schema.</summary>
|
|
[Category("server")]
|
|
[RequiresEnvironment("CHRONOS_TEST_PG")]
|
|
public sealed class PostgresServerTests(TestContext context)
|
|
{
|
|
[Test]
|
|
public async Task SignInUploadSyncAndJobsWorkOnPostgres()
|
|
{
|
|
await using var schema = await Schema.CreateAsync(context);
|
|
var handler = new BlockingJobHandler(JobKind.Thumbs);
|
|
await using var server = await TestServer.StartAsync(context, o =>
|
|
{
|
|
o.Database = DatabaseKind.Postgres;
|
|
o.DbConnectionString = schema.ConnectionString;
|
|
o.ChunkSizeBytes = 64 * 1024;
|
|
}, s => s.AddSingleton<IJobHandler>(handler));
|
|
Assert.Equal(Dialect.Postgres, server.Database.Dialect);
|
|
|
|
var (editor, _, _) = await server.UserAsync("alberto", "editor", quotaBytes: 10_000_000);
|
|
var data = Uploads.Photo(150_000, 71);
|
|
var frame = await Uploads.UploadAsync(editor, data, sessionKey: "pg");
|
|
using (var manifest = await editor.GetAsync($"/api/v1/sequences/{frame.SequenceId}/manifest"))
|
|
Assert.Equal(Uploads.Sha(data), (await manifest.ReadAsync(ProtocolJson.Default.SequenceManifest)).Frames.Single().Sha256);
|
|
using (var me = await editor.GetAsync("/api/v1/auth/me")) Assert.Equal(data.Length, (await me.ReadAsync(ProtocolJson.Default.UserDto)).UsedBytes);
|
|
|
|
using (var stale = await editor.PatchAsJsonAsync($"/api/v1/sequences/{frame.SequenceId}", new UpdateSequenceRequest(null, ["pg"], null, null, "{}", 99),
|
|
ProtocolJson.Default.UpdateSequenceRequest))
|
|
await stale.ExpectErrorAsync(HttpStatusCode.Conflict, "settings_conflict");
|
|
|
|
// Uploading queued a background thumbnail job for the sequence; it runs on the server's engine.
|
|
using var queued = await editor.GetAsync("/api/v1/jobs?kind=thumbs");
|
|
var job = (await queued.ReadAsync(ProtocolJson.Default.PageJobDto)).Items.Single();
|
|
await JobApiTests.WaitForAsync(editor, job.Id, "running");
|
|
handler.Release();
|
|
await JobApiTests.WaitForAsync(editor, job.Id, "done");
|
|
|
|
var admin = await server.AdminAsync();
|
|
using var dashboard = await admin.GetAsync("/api/v1/server/dashboard");
|
|
Assert.True((await dashboard.ReadAsync(ProtocolJson.Default.DashboardDto)).Database.StartsWith("PostgreSQL ", StringComparison.Ordinal));
|
|
using var health = await server.Client.GetAsync("/health");
|
|
Assert.Equal("ok", (await health.ReadAsync(ProtocolJson.Default.HealthStatus)).Checks["database"]);
|
|
}
|
|
|
|
[Test]
|
|
public async Task TheManifestListsTheFramesOfOneFramesVersionOnPostgres()
|
|
{
|
|
await using var schema = await Schema.CreateAsync(context);
|
|
await using var server = await TestServer.StartAsync(context, o =>
|
|
{
|
|
o.Database = DatabaseKind.Postgres;
|
|
o.DbConnectionString = schema.ConnectionString;
|
|
});
|
|
Assert.Equal(Dialect.Postgres, server.Database.Dialect);
|
|
await Api.SequenceTests.FramesOfOneVersionAsync(server);
|
|
}
|
|
|
|
[Test]
|
|
public async Task SchedulesRunOnPostgresWithoutAnIndexBackup()
|
|
{
|
|
await using var schema = await Schema.CreateAsync(context);
|
|
await using var server = await TestServer.StartAsync(context, o =>
|
|
{
|
|
o.Database = DatabaseKind.Postgres;
|
|
o.DbConnectionString = schema.ConnectionString;
|
|
}, Chronos.Server.Storage.ArchiveModule.Register);
|
|
var admin = await server.AdminAsync();
|
|
using var response = await admin.GetAsync("/api/v1/schedules");
|
|
var list = await response.ReadAsync(ProtocolJson.Default.SchedulesDto);
|
|
|
|
// PostgreSQL is backed up with its own tools (owner review 2, R10): no index-backup schedule, and the action says why.
|
|
Assert.SequenceEqual(["archive-scan", "upload-cleanup", "housekeeping", "audit-cleanup"], list.Items.Select(s => s.SystemKey));
|
|
var backup = list.Actions.Single(a => a.Name == "backup");
|
|
Assert.Equal((false, "postgres"), (backup.Available, backup.Reason));
|
|
|
|
// A run is claimed, queued and recorded on PostgreSQL as on SQLite.
|
|
var history = list.Items.Single(s => s.SystemKey == "housekeeping");
|
|
using var run = await admin.PostAsync($"/api/v1/schedules/{history.Id}/run", null);
|
|
var ran = await run.ReadAsync(ProtocolJson.Default.ScheduleRunDto);
|
|
Assert.Equal(("queued", ran.JobId), (ran.Outcome, ran.Schedule.LastJobId));
|
|
await JobApiTests.WaitForAsync(admin, ran.JobId!.Value, "done");
|
|
}
|
|
|
|
/// <summary>A throw-away schema on the test server, selected through <c>Search Path</c>.</summary>
|
|
private sealed class Schema : IAsyncDisposable
|
|
{
|
|
private readonly IDatabase _admin;
|
|
private readonly string _name;
|
|
|
|
private Schema(IDatabase admin, string name, string connectionString)
|
|
{
|
|
_admin = admin;
|
|
_name = name;
|
|
ConnectionString = connectionString;
|
|
}
|
|
|
|
public string ConnectionString { get; }
|
|
|
|
public static async Task<Schema> CreateAsync(TestContext context)
|
|
{
|
|
var baseConnection = Environment.GetEnvironmentVariable("CHRONOS_TEST_PG")!;
|
|
var admin = DatabaseFactory.Open(DatabaseOptions.Postgres(baseConnection));
|
|
var name = "chronos_s_" + Guid.NewGuid().ToString("N")[..16];
|
|
await using (var session = await admin.OpenAsync(context.Cancellation))
|
|
await session.ExecuteScriptAsync($"CREATE SCHEMA {name}", context.Cancellation);
|
|
return new Schema(admin, name, baseConnection.TrimEnd(';', ' ') + $";Search Path={name}");
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await using (var session = await _admin.OpenAsync(CancellationToken.None))
|
|
await session.ExecuteScriptAsync($"DROP SCHEMA IF EXISTS {_name} CASCADE", CancellationToken.None);
|
|
await _admin.DisposeAsync();
|
|
}
|
|
}
|
|
}
|