Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Adriano Fabri 469 posts 1633 karma points
    May 31, 2021 @ 09:34
    Adriano Fabri
    0

    How to extend Scheduler with BackgroundTaskRunner to start at specific time

    Hi to all,

    I added my custom scheduled task to update Members based on Scheduling Reference

    The task function properly but I saw that the recurrent time (in milliseconds), starts from the end of last process time.

    This is not good because I would that my process starts every nights at 2.00AM

    This is my code, there is a way to start process at specific time?

    Thanks A.

    using AF.Members.Tools.For.UmbracoV8.Controllers;
    using System;
    using System.Collections.Generic;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Logging;
    using Umbraco.Core.Services;
    using Umbraco.Web.Scheduling;
    
    namespace AF.Members.Tools.For.UmbracoV8.Component
    {
        // We start by setting up a composer and component so our task runner gets registered on application startup
        public class TaskSchedulerComposer : ComponentComposer<TaskSchedulerComponent> { }
    
        public class TaskSchedulerComponent : IComponent
        {
            private IProfilingLogger _logger;
            private IRuntimeState _runtime;
            private IContentService _contentService;
            private BackgroundTaskRunner<IBackgroundTask> _updateMembersDetails;
    
            public TaskSchedulerComponent(IProfilingLogger logger, IRuntimeState runtime, IContentService contentService)
            {
                _logger = logger;
                _runtime = runtime;
                _contentService = contentService;
                _updateMembersDetails = new BackgroundTaskRunner<IBackgroundTask>("UpdateMembersDetails", _logger);
            }
    
            public void Initialize()
            {
                //DateTime StartProcessDate = DateTime.Now;
                DateTime StartProcessDate = DateTime.Now.AddDays(1);
    
                DateTime StartProcessDateTime = new DateTime(StartProcessDate.Year, StartProcessDate.Month, StartProcessDate.Day, 2, 30, 0);
    
                TimeSpan ts = StartProcessDateTime.Subtract(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second));
    
                int delayBeforeUpdateProcessStart = int.Parse(ts.TotalMilliseconds.ToString()); // Delay (in ms) Before Update Process Start
                int howOftenWeRepeat = 86400000; //86400000ms = 24h
    
                var task = new UpdateMemberesDetailsTaskBase(_updateMembersDetails, delayBeforeUpdateProcessStart, howOftenWeRepeat, _runtime, _logger, _contentService);
    
                //As soon as we add our task to the runner it will start to run (after its delay period)
                _updateMembersDetails.TryAdd(task);
            }
    
            public void Terminate()
            {
            }
        }
    
        // Now we get to define the recurring task
        public class UpdateMemberesDetailsTaskBase : RecurringTaskBase
        {
            private IRuntimeState _runtime;
            private IProfilingLogger _logger;
            private IContentService _contentService;
    
            public UpdateMemberesDetailsTaskBase(IBackgroundTaskRunner<RecurringTaskBase> runner, int delayBeforeWeStart, int howOftenWeRepeat, IRuntimeState runtime, IProfilingLogger logger, IContentService contentService)
                : base(runner, delayBeforeWeStart, howOftenWeRepeat)
            {
                _runtime = runtime;
                _logger = logger;
                _contentService = contentService;
            }
    
            public override bool PerformRun()
            {
                Dictionary<string, Exception> UpdateResult = new Dictionary<string, Exception>();
    
                try
                {
                    // Update Members
                    using (_logger.TraceDuration<UpdateMemberesDetailsTaskBase>("AFUpdateMembersDetailsTask - Start Update Members Process"))
                    {
                        //Lancio la procedura di aggiornamento dei dettagli degli utenti
                        UpdateResult = MembersController.UpdateAllMembersDetails();
                    }
    
                    if (UpdateResult["UpdateResult"] == null)
                    {
                        _logger.Info<UpdateMemberesDetailsTaskBase>("AFUpdateMembersDetailsTask - End Update Members Process - Result: Successfully completed");
                    }
                    else
                    {
                        _logger.Error(UpdateResult["UpdateResult"].GetType(), UpdateResult["UpdateResult"].Message);
                        _logger.Error(UpdateResult["UpdateResult"].GetType(), UpdateResult["UpdateResult"].StackTrace);
                        _logger.Info<UpdateMemberesDetailsTaskBase>("AFUpdateMembersDetailsTask - End Update Members Process - Result: Completed with Error. See Exception details");
                    }
    
                    return true;
                }
                catch (Exception ex)
                {
                    _logger.Error(ex.GetType(), ex.Message);
                    _logger.Error(ex.GetType(), ex.StackTrace);
                    _logger.Info<UpdateMemberesDetailsTaskBase>("AFUpdateMembersDetailsTask - End Update Members Process - Result: An error occurred during update. See Exception details");
    
                    return false;
                }
            }
    
            public override bool IsAsync => false;
        }
    }
    
  • Adriano Fabri 469 posts 1633 karma points
    Jun 01, 2021 @ 11:25
    Adriano Fabri
    0

    Any help?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies