Skip to main content

General

Örnek: Zamanlanmış Görevler

Wall-Clock hizalı scheduler ile tam zamanlı görev çalıştırma.

Task.Delay veya Thread.Sleep kullanmayın. Mesaj döngüsünü bloklamadan periyodik işler için ExecuteScheduledTask metodunu kullanın.

using Proxus.SDK.BaseFunctions;
using Proxus.Common.Messages;
using Proxus.Common;
using System;
using System.Threading;

namespace MyProject.Functions
{
    public class ShiftReset : FunctionBase
    {
        private CancellationTokenSource _scheduler;

        public ShiftReset(object sys, object log, object config) : base(sys, log, config) { }

        protected override void OnStarted()
        {
            // 1. Saatlik görev
            // align: true -> xx:00:00 zamanına hizalar
            _scheduler = ExecuteScheduledTask(
                TimeSpan.FromHours(1), 
                OnShiftChange, 
                align: true
            );

            base.OnStarted();
        }

        private async void OnShiftChange()
        {
            LogInformation($"Shift Sequence çalışıyor: {DateTime.Now}");

            // 2. Kontrol paketi oluştur
            var syncPacket = new TransportDataBuilder()
                .WithTopic("Global/Sync")
                .WithPayload("Command", "ResetCounters")
                .WithAttribute("Source", this.GatewayId)
                .Build();

            // 3. Cluster'daki tüm Gateway'lere yayınla
            await PublishToEdgeGateways(syncPacket);
        }

        protected override void OnStopping()
        {
            _scheduler?.Cancel();
            base.OnStopping();
        }
    }
}