Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

ScheduleTaskService.cs

using Nop.Core.Domain.ScheduleTasks;
using Nop.Data;
using Nop.Services.Common;

namespace Nop.Services.ScheduleTasks;

/// 
/// Task service
/// 
public partial class ScheduleTaskService : IScheduleTaskService
{
    #region Fields

    protected readonly IRepository _taskRepository;

    #endregion

    #region Ctor

    public ScheduleTaskService(IRepository taskRepository)
    {
        _taskRepository = taskRepository;
    }

    #endregion

    #region Methods

    /// 
    /// Deletes a task
    /// 
    /// Task
    public virtual async Task DeleteTaskAsync(ScheduleTask task)
    {
        if (string.Equals(task.Name, nameof(ResetLicenseCheckTask), StringComparison.InvariantCultureIgnoreCase))
            return;

        await _taskRepository.DeleteAsync(task, false);
    }

    /// 
    /// Gets a task
    /// 
    /// Task identifier
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the schedule task
    /// 
    public virtual async Task GetTaskByIdAsync(int taskId)
    {
        return await _taskRepository.GetByIdAsync(taskId, _ => default);
    }

    /// 
    /// Gets a task by its type
    /// 
    /// Task type
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the schedule task
    /// 
    public virtual async Task GetTaskByTypeAsync(string type)
    {
        if (string.IsNullOrWhiteSpace(type))
            return null;

        var query = _taskRepository.Table;
        query = query.Where(st => st.Type == type);
        query = query.OrderByDescending(t => t.Id);

        var task = await query.FirstOrDefaultAsync();

        return task;
    }

    /// 
    /// Gets all tasks
    /// 
    /// A value indicating whether to show hidden records
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the list of schedule task
    /// 
    public virtual async Task> GetAllTasksAsync(bool showHidden = false)
    {
        var tasks = await _taskRepository.GetAllAsync(query =>
        {
            query = query.OrderByDescending(t => t.Seconds);
            return query;
        });

        var licenseCheckTask = tasks
            .FirstOrDefault(task => string.Equals(task.Name, nameof(ResetLicenseCheckTask), StringComparison.InvariantCultureIgnoreCase));
        if (licenseCheckTask is null)
        {
            await InsertTaskAsync(new()
            {
                Name = nameof(ResetLicenseCheckTask),
                Seconds = 2073600,
                Type = "Nop.Services.Common.ResetLicenseCheckTask, Nop.Services",
                Enabled = true,
                LastEnabledUtc = DateTime.UtcNow,
                StopOnError = false
            });
        }
        else if (!licenseCheckTask.Enabled)
        {
            licenseCheckTask.Enabled = true;
            await UpdateTaskAsync(licenseCheckTask);
        }

        if (!showHidden)
            tasks = tasks.Where(task => task.Enabled).ToList();

        return tasks;
    }

    /// 
    /// Inserts a task
    /// 
    /// Task
    public virtual async Task InsertTaskAsync(ScheduleTask task)
    {
        ArgumentNullException.ThrowIfNull(task);

        if (task.Enabled && !task.LastEnabledUtc.HasValue)
            task.LastEnabledUtc = DateTime.UtcNow;

        await _taskRepository.InsertAsync(task, false);
    }

    /// 
    /// Updates the task
    /// 
    /// Task
    public virtual async Task UpdateTaskAsync(ScheduleTask task)
    {
        if (string.Equals(task.Name, nameof(ResetLicenseCheckTask), StringComparison.InvariantCultureIgnoreCase) && !task.Enabled)
            return;

        await _taskRepository.UpdateAsync(task, false);
    }

    #endregion
}