Class ServerBandwidthRollupService
- Namespace
- FishMMO.ControlPanel.Services
- Assembly
- FishMMO-ControlPanel.dll
Rolls the servers' bandwidth minutes into hours and enforces the retention of both tables, on a timer, so the Bandwidth page's 30-day figures and a year of history exist whether or not anybody has the page open.
public sealed class ServerBandwidthRollupService : BackgroundService, IHostedService, IDisposable
- Inheritance
-
ServerBandwidthRollupService
- Implements
- Inherited Members
Remarks
Why here. The game servers write their own minutes and nothing else; a rollup there would run once per server process and compete with the tick. Nothing in the repository prunes on a schedule, and PostgreSQL has no scheduler without an extension, so the panel — the one long-running host that reads these tables — owns it, as it owns the maintenance advance (MaintenanceAdvanceService, which this is modelled on).
Safe to run twice, and safe in two panels at once. Every hour row is recomputed from its minutes and SET, never added to, so repeating a pass changes nothing. Each pass takes a transaction-level advisory lock first and the second panel skips; the lock is there to keep two panels' multi-row upserts from deadlocking each other and their retention batches from fighting over the same rows, not for correctness of the numbers. See IServerBandwidthReportService.
The first pass after this panel starts is a catch-up: it rolls every hour that still has minutes, covering however long no panel was running. Later passes roll the last RollupWindowHours hours, which reaches past the latest a server's retried minute can arrive. Retention folds each hour into its row before deleting its minutes, so a pass that deletes can never lose traffic the rollup had not reached.
A failed pass is logged and the timer continues, for the reason the maintenance service gives: a transient database fault must not become a panel outage.
Constructors
ServerBandwidthRollupService(IServiceScopeFactory, ILogger<ServerBandwidthRollupService>)
Creates the service.
public ServerBandwidthRollupService(IServiceScopeFactory scopeFactory, ILogger<ServerBandwidthRollupService> log)
Parameters
scopeFactoryIServiceScopeFactoryFactory for the scope each pass resolves its service from.
logILogger<ServerBandwidthRollupService>The log to report to.
Fields
MaxHourBatchesPerPass
Batches of expired hour rows one pass may delete.
public const int MaxHourBatchesPerPass = 10
Field Value
MaxMinuteHoursPerPass
Whole hours of minutes one pass may prune. A day per pass at five-minute passes clears any backlog — a panel that was down for weeks — within the hour, while keeping each pass short. Each hour is its own transaction.
public const int MaxMinuteHoursPerPass = 24
Field Value
Methods
ExecuteAsync(CancellationToken)
This method is called when the IHostedService starts. The implementation should return a task that represents the lifetime of the long running operation(s) being performed.
protected override Task ExecuteAsync(CancellationToken stoppingToken)
Parameters
stoppingTokenCancellationTokenTriggered when StopAsync(CancellationToken) is called.
Returns
Remarks
See Worker Services in .NET for implementation guidelines.