CI / Build and test (push) Successful in 7m0s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 6s
Release / Build, test and publish (push) Successful in 13m28s
CI / PostgreSQL 16 integration (push) Successful in 5m52s
CI / PostgreSQL 17 integration (push) Successful in 4m50s
CI / Docker image (push) Successful in 1m4s
Release / Server image (push) Successful in 1m35s
- Jobs > Scheduled on the desktop and the admin panel. Every timer that queued work is now a visible system schedule: archive scan, upload clean-up, index backup, job-history and audit clean-ups, desktop housekeeping. New actions: verify, thumbnails, proxies, analysis, maintenance, backup, clean-ups, reports. - The scheduler: migration 0007, in-house recurrence, compare-and-set claim, catch-up, scan at start. - Log settings: levels, retention, size parts, total limit, daily or hourly rotation, audit retention, diagnostics share; the server writes Debug/Trace to its files again; logging never fails a job. Upgrade: CHRONOS_LOG_LEVEL set in an existing container locks both levels; migration 0007 is one-way. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
90 lines
4.5 KiB
C#
90 lines
4.5 KiB
C#
using System.Globalization;
|
|
using System.Text.Json;
|
|
using Chronos.Core.Model;
|
|
using Chronos.Data.Jobs;
|
|
using Chronos.Protocol;
|
|
|
|
namespace Chronos.Data.Scheduling;
|
|
|
|
/// <summary>
|
|
/// Schedules between the model and API v1 (<see cref="ScheduleDto"/>, <see cref="RecurrenceDto"/>), and the stored form of
|
|
/// their parameters (<see cref="ScheduleParametersDto"/> as camelCase JSON, normalised to the fields the action reads).
|
|
/// </summary>
|
|
public static class ScheduleMapping
|
|
{
|
|
/// <summary>Wire names of <see cref="RecurrenceKind"/>: <c>every</c>, <c>daily</c>, <c>weekly</c>, <c>monthly</c>.</summary>
|
|
public static string Kind(RecurrenceKind kind) => kind.ToString().ToLowerInvariant();
|
|
|
|
/// <summary>Wire names of <see cref="RecurrenceUnit"/>: <c>minutes</c>, <c>hours</c>, <c>days</c>.</summary>
|
|
public static string Unit(RecurrenceUnit unit) => unit.ToString().ToLowerInvariant();
|
|
|
|
public static ScheduleDto ToDto(Schedule schedule)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(schedule);
|
|
var recurrence = ScheduleRecurrence.TryParse(schedule.Recurrence, out var parsed) ? ToDto(parsed) : new RecurrenceDto(schedule.Recurrence);
|
|
return new ScheduleDto(schedule.Id, schedule.SystemKey, schedule.Name, schedule.Action, schedule.Target, Parameters(schedule), recurrence,
|
|
schedule.CatchUp, JobWire.Priority(schedule.Priority), schedule.Enabled, schedule.NextRun, schedule.LastRun, schedule.LastJobId,
|
|
schedule.LastOutcome, schedule.CreatedBy, schedule.Created, schedule.Updated);
|
|
}
|
|
|
|
public static RecurrenceDto ToDto(ScheduleRecurrence recurrence)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(recurrence);
|
|
var time = recurrence.Time.ToString("HH:mm", CultureInfo.InvariantCulture);
|
|
return recurrence.Kind switch
|
|
{
|
|
RecurrenceKind.Every => new RecurrenceDto(Kind(recurrence.Kind), Every: recurrence.Every, Unit: Unit(recurrence.Unit)),
|
|
RecurrenceKind.Daily => new RecurrenceDto(Kind(recurrence.Kind), Time: time),
|
|
RecurrenceKind.Weekly => new RecurrenceDto(Kind(recurrence.Kind), Time: time, Days: [.. recurrence.Days.Select(ScheduleRecurrence.DayName)]),
|
|
_ => new RecurrenceDto(Kind(recurrence.Kind), Time: time, DayOfMonth: recurrence.DayOfMonth),
|
|
};
|
|
}
|
|
|
|
/// <summary>The recurrence <paramref name="dto"/> describes; null when it is missing or invalid (out of range, unknown names).</summary>
|
|
public static ScheduleRecurrence? FromDto(RecurrenceDto? dto)
|
|
{
|
|
if (dto is null) return null;
|
|
var text = dto.Kind?.ToLowerInvariant() switch
|
|
{
|
|
"every" when dto.Every is { } every && Enum.TryParse<RecurrenceUnit>(dto.Unit, ignoreCase: true, out var unit) && Enum.IsDefined(unit)
|
|
=> string.Create(CultureInfo.InvariantCulture, $"every:{every}{Unit(unit)[0]}"),
|
|
"daily" => "daily:" + dto.Time,
|
|
"weekly" when dto.Days is { Count: > 0 } days => $"weekly:{string.Join(',', days)}@{dto.Time}",
|
|
"monthly" when dto.DayOfMonth is { } day => string.Create(CultureInfo.InvariantCulture, $"monthly:{day}@{dto.Time}"),
|
|
_ => null,
|
|
};
|
|
return ScheduleRecurrence.TryParse(text, out var recurrence) ? recurrence : null;
|
|
}
|
|
|
|
public static ScheduleActionDto ToDto(ScheduleActionInfo info)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(info);
|
|
return new ScheduleActionDto(info.Name, info.Available, info.Reason);
|
|
}
|
|
|
|
/// <summary>The schedule's parameters, normalised for its action (stored JSON that cannot be read counts as none).</summary>
|
|
public static ScheduleParametersDto Parameters(Schedule schedule)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(schedule);
|
|
ScheduleParametersDto? stored = null;
|
|
if (schedule.ParametersJson is { Length: > 0 } json)
|
|
{
|
|
try
|
|
{
|
|
stored = JsonSerializer.Deserialize(json, ProtocolJson.Default.ScheduleParametersDto);
|
|
}
|
|
catch (JsonException)
|
|
{
|
|
}
|
|
}
|
|
return ScheduleActions.Normalize(schedule.Action, stored);
|
|
}
|
|
|
|
/// <summary>The stored form of <paramref name="parameters"/> for <paramref name="action"/>; null for an action without parameters.</summary>
|
|
public static string? ParametersJson(string action, ScheduleParametersDto? parameters)
|
|
{
|
|
var normalized = ScheduleActions.Normalize(action, parameters);
|
|
return normalized == new ScheduleParametersDto() ? null : JsonSerializer.Serialize(normalized, ProtocolJson.Default.ScheduleParametersDto);
|
|
}
|
|
}
|