简体   繁体   中英

NopCommerce Task Plugin not starting task

Using NopCommerce 3.8.

I have created a new Misc plugin. The purpose is to run a task that restfully calls my web api for data.

The following is my code:

Plugin class

public class MereSyncPlugin : BasePlugin, IMiscPlugin
{
    private MereSyncObjectContext _context;
    private IScheduleTaskService _schedualTaskService;

    public MereSyncPlugin(MereSyncObjectContext context, IScheduleTaskService schedualTaskService)
    {
        _context = context;
        _schedualTaskService = schedualTaskService;
    }

    public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
    {
        actionName = "";
        controllerName = "";
        routeValues = new RouteValueDictionary()
        {
            { "Namespaces", "Nop.plugin.Misc.MereSync.Controllers" },
            { "area", null }
        };   
    }

    public override void Install()
    {
        //TODO: add more
        _schedualTaskService.InsertTask(new Nop.Core.Domain.Tasks.ScheduleTask()
        {
            Enabled = true,
            Name = "Product Sync",
            Seconds = 3600,
            StopOnError = false,
            Type = "Nop.Plugin.Misc.MereSync.ProductSyncTask, Nop.Plugin.Misc.MereSync"
        });

        //_context.Install();
        base.Install();
    }

    public override void Uninstall()
    {
        Nop.Core.Domain.Tasks.ScheduleTask task = _schedualTaskService.GetTaskByType("Nop.Plugin.Misc.MereSync.ProductSyncTask, Nop.Plugin.Misc.MereSync");

        if (task != null)
            _schedualTaskService.DeleteTask(task);

        //_context.Uninstall();
        base.Uninstall();
    }
}

Object context class (although not set up at this point so commented out some parts)

public class MereSyncObjectContext : DbContext, IDbContext
{
    public bool ProxyCreationEnabled
    {
        get
        {
            throw new NotImplementedException();
        }

        set
        {
            throw new NotImplementedException();
        }
    }

    public bool AutoDetectChangesEnabled
    {
        get
        {
            throw new NotImplementedException();
        }

        set
        {
            throw new NotImplementedException();
        }
    }

    public MereSyncObjectContext(string nameOrConnectionString) : base(nameOrConnectionString)
    {

    }

    /*protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //TODO: insert ALL model MAPPINGS!!!!
        //modelBuilder.Configurations.Add(new insertnewmap());
        base.OnModelCreating(modelBuilder);
    }*/

    public string CreateDatabaseInstallationScript()
    {
        return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
    }

    public void Install()
    {
        Database.SetInitializer<MereSyncObjectContext>(null);
        Database.ExecuteSqlCommand(CreateDatabaseInstallationScript());
        SaveChanges();
    }

    public void Uninstall()
    {
        //this.DropPluginTable("MereSys_TableName");
    }

    IDbSet<TEntity> IDbContext.Set<TEntity>()
    {
        return base.Set<TEntity>();
    }

    public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : BaseEntity, new()
    {
        throw new NotImplementedException();
    }

    public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
    {
        throw new NotImplementedException();
    }

    public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = default(int?), params object[] parameters)
    {
        throw new NotImplementedException();
    }

    public void Detach(object entity)
    {
        throw new NotImplementedException();
    }
}

Task class

public class ProductSyncTask : ITask
{
    private readonly ILogger _logger;
    public ProductSyncTask(ILogger logger)
    {
        this._logger = logger;
    }
    public async void Execute()
    {
        //TODO: Call My method, USE BEST PRACTICE, IoC DI
        //throw new NotImplementedException();
        _logger.InsertLog(Nop.Core.Domain.Logging.LogLevel.Information, "Start", "Product sync has started");
        ProductSyncService test = new ProductSyncService();
        await test.GetProductsAsync();
    }
}

Question

When i Navigate in the Nopcommerce backend to the log file

admin area > system > log

I expected to see my log statment there, but nothing shows.

So i also check the schedual status of the task, it never started. Breakpoints are also set up on my task class in debug mode and is never hit.

So, Am i missing something here? Is my implimentation correct?

Notes:

I have removed the async call but it did nothing (as its never reaching the class level). Cleaned and rebuilt, set copy local false where applicable. The plugin is installed. Event viewer on machine doesnt show any warnings or errors relating to nopcommerce. "Run Now" button when pressed refreshs page but no indication task run, nor my class breakpoints hit in debug mode.

Source file Source

plugin contains my code so far, most is redundant not used ie rest client etc as im not ever getting to that stage of the flow.

First parameter of Type property is full path of the class, which inherits the ITask interface. And the second parameter is namespace of the plugin.

So, you need to just change here:

_schedualTaskService.InsertTask(new Nop.Core.Domain.Tasks.ScheduleTask()
{
    Enabled = true,
    Name = "Product Sync",
    Seconds = 3600,
    StopOnError = false,
    Type = "Nop.Plugin.Misc.MereSync.ProductSyncTask, Nop.Plugin.Misc.MereSync"
});

To

Type = "Nop.Plugin.Misc.MereSync.ProductSyncTask.MereSyncPlugin, Nop.Plugin.Misc.MereSync"

Hope this helps!

OK Div was on the correct track but couldnt see my file system, I forgot 1 folder.

Lesson to learn here? Check Strings, I forgot i added a nice extra folder called...... "Tasks".

full path for install?

"Nop.Plugin.Misc.MereSync.Tasks.ProductSyncTask, Nop.Plugin.Misc.MereSync.MereSyncPlugin"

Task in nop really is that simple. Dont waste a day like i just did!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM