Search
Back to all

Create a Schedule Task in v4.4

1. Create the Task Method

namespace Nop.Plugin.Misc.Name.Services
{
    /// <summary>
    /// Represents task for myprogram
    /// </summary>
    public partial class ProgramTask : IScheduleTask
    {
        #region Fields

        private readonly ICustomerService _customerService;
        private readonly IStoreContext _storeContext;

        #endregion

        #region Ctor

        public ProgramTask(ICustomerService customerService,
            IStoreContext storeContext)
        {
            _customerService = customerService;
            _storeContext = storeContext;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Executes a task
        /// </summary>
        public async System.Threading.Tasks.Task ExecuteAsync()
        {
            var store = await _storeContext.GetCurrentStoreAsync();

            // Do the task
        }

        #endregion
    }

 

2. Create the Task

            private const string PROGRAM_TASK_TYPE = "Nop.Plugin.Misc.Name.Services.ProgramTask, Nop.Plugin.Misc.Name";

            //task for process
            if (await _scheduleTaskService.GetTaskByTypeAsync(PROGRAM_TASK_TYPE) == null)
            {
                await _scheduleTaskService.InsertTaskAsync(new ScheduleTask
                {
                    Enabled = true,
                    Name = "My rewards points program task",
                    Seconds = 3600,
                    Type = PROGRAM_TASK_TYPE
                });
            }

Comments
Leave your comment Close
Thank you for the response. I did a hack and used this approach in the ScheduleTaskController Run Task. I used a Try statement to do the InsertTaskAsync without error, however when I try and run the Task through the Administrator I get the same error.

Checking code, I can't find the InsertTaskAsync anywhere else in the code base, except for the SendInBlue plugin.

i've tried to follow/figure out the DeleteGuestTask without success.