无法让我的nopcommerce任务运行

时间:2017-11-13 22:55:04

标签: c# model-view-controller plugins nopcommerce

好的,所以我创建了一个我想每小时运行的任务:

using Nop.Core.Domain.Stores;
using Nop.Core.Plugins;
using Nop.Services.Common;
using Nop.Services.Configuration;
using Nop.Services.Customers;
using Nop.Services.Logging;
using Nop.Services.Stores;
using Nop.Services.Tasks;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Timers;

namespace Nop.Plugin.Feed.Froogle.Tasks
{
    public partial class CreateAmazonInventoryUpdateTask : ITask
    {
        private readonly ISettingService _settingService;
        private readonly IScheduleTaskService _scheduleTaskService;
        private readonly ILogger _logger;
        private readonly IPluginFinder _pluginFinder;
        private readonly IStoreService _storeService;
        private readonly FroogleSettings _froogleSettings;
        private readonly ICustomerService _customerService;
        private readonly IGenericAttributeService _genericAttributeService;

        //Constructor
        public CreateAmazonInventoryUpdateTask(
            ISettingService settingService,
            IScheduleTaskService scheduleTaskService,
            ILogger logger,
            IPluginFinder pluginFinder,
            IStoreService storeService,
            FroogleSettings froogleSettings,
            ICustomerService customerService,
            IGenericAttributeService genericAttributeService)
        {
            _settingService = settingService;
            _scheduleTaskService = scheduleTaskService;
            _logger = logger;
            _pluginFinder = pluginFinder;
            _storeService = storeService;
            _froogleSettings = froogleSettings;
            _customerService = customerService;
            _genericAttributeService = genericAttributeService;
        }

        /// <summary>
        /// Executes a task
        /// </summary>
        public void Execute()
        {
            Debug.WriteLine("CreateAmazonInventoryUpdateTask started");
            // Only runs on first server, or test server, or local
            var runHour = new System.Timers.Timer(60000); //60 * 60 * 1000 one hour in milliseconds
            runHour.Elapsed += new ElapsedEventHandler(CreateAmazonInventoryUpdateFeed);
        }

        public void CreateAmazonInventoryUpdateFeed(object src, ElapsedEventArgs e)
        {
            Debug.WriteLine("CreateAmazonInventoryUpdateFeed task starting");
            _logger.Information("CreateAmazonInventoryUpdateFeed task starting");

            var stopwatch = new Stopwatch();
            stopwatch.Start();

            try
            {
                var pluginDescriptor = _pluginFinder.GetPluginDescriptorBySystemName("PromotionFeed.Froogle");
                if (pluginDescriptor == null)
                    throw new Exception("Cannot load the plugin");

                //plugin
                var plugin = pluginDescriptor.Instance() as FroogleService;
                if (plugin == null)
                    throw new Exception("Cannot load the plugin");

                var stores = new List<Store>();
                var storeById = _storeService.GetStoreById(_froogleSettings.StoreId);
                if (storeById != null)
                    stores.Add(storeById);
                else
                    stores.AddRange(_storeService.GetAllStores());

                foreach (var store in stores.Where(x => x.Id != 2)) // Added store 2 is admin
                {
                    _logger.Information($"CreateAmazonInventoryUpdateFeed, storeId = {store.Id}, machine name = {Environment.MachineName}");
                    plugin.AmazonProductHourlyInventoryUpdateTask(store);

                }
            }
            catch (Exception exc)
            {
                _logger.Error(exc.Message, exc);
            }

            var time = string.Format("{0} mins {1} seconds", Math.Floor(stopwatch.Elapsed.TotalMinutes), (int)stopwatch.Elapsed.TotalSeconds % 60);
            _logger.Information("CreateAmazonInventoryUpdateFeed scheduled task completed in " + time);
        }
    }
}

现在在froogle服务中,我在安装插件时添加了任务:

/// <summary>
    /// Install plugin
    /// </summary>
    public override void Install()
    {
        //settings
        var settings = new FroogleSettings
        {
            PricesConsiderPromotions = false,
            ProductPictureSize = 125,
            PassShippingInfoWeight = false,
            PassShippingInfoDimensions = false,
            StaticFileName = string.Format("froogle_{0}.xml", CommonHelper.GenerateRandomDigitCode(10)),
            ExpirationNumberOfDays = 28
        };
        _settingService.SaveSetting(settings);

        //data
        _objectContext.Install();

        //locales
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Store", "Store");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Store.Hint", "Select the store that will be used to generate the feed.");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Currency", "Currency");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Currency.Hint", "Select the default currency that will be used to generate the feed.");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.DefaultGoogleCategory", "Default Google category");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.DefaultGoogleCategory.Hint", "The default Google category to use if one is not specified.");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.General", "General");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Generate", "Generate feed");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Override", "Override product settings");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.PassShippingInfoWeight", "Pass shipping info (weight)");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.PassShippingInfoWeight.Hint", "Check if you want to include shipping information (weight) in generated XML file.");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.PassShippingInfoDimensions", "Pass shipping info (dimensions)");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.PassShippingInfoDimensions.Hint", "Check if you want to include shipping information (dimensions) in generated XML file.");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.PricesConsiderPromotions", "Prices consider promotions");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.PricesConsiderPromotions.Hint", "Check if you want prices to be calculated with promotions (tier prices, discounts, special prices, tax, etc). But please note that it can significantly reduce time required to generate the feed file.");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.ProductPictureSize", "Product thumbnail image size");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.ProductPictureSize.Hint", "The default size (pixels) for product thumbnail images.");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Products.ProductName", "Product");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Products.GoogleCategory", "Google Category");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Products.Gender", "Gender");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Products.AgeGroup", "Age group");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Products.Color", "Color");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Products.Size", "Size");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.Products.CustomGoods", "Custom goods (no identifier exists)");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.SuccessResult", "Froogle feed has been successfully generated.");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.StaticFilePath", "Generated file path (static)");
        this.AddOrUpdatePluginLocaleResource("Plugins.Feed.Froogle.StaticFilePath.Hint", "A file path of the generated Froogle file. It's static for your store and can be shared with the Froogle service.");

        var task = FindScheduledTask();
        if (task == null)
        {
            DateTime now = DateTime.Now;
            task = new ScheduleTask
            {
                Name = "Amazon Inventory Update Sync",
                // Every hour
                Seconds = 60,
                Type = "Nop.Plugin.Feed.Froogle.Tasks.CreateAmazonInventoryUpdateTask, Nop.Plugin.Feed.Froogle",
                StopOnError = false,
                Enabled = true,
            };

            _scheduleTaskService.InsertTask(task);
        }

        base.Install();
    }

    private ScheduleTask FindScheduledTask()
    {
        return _scheduleTaskService.GetTaskByType("Nop.Plugin.Feed.Froogle.Tasks.CreateAmazonInventoryUpdateTask, Nop.Plugin.Feed.Froogle");
    }

该任务将生成要提交给Google的Feed。但问题是任务无法运行。

从我用Google搜索来看,一切看起来都是正确的,不确定我错过了什么。

同样是,这是1分钟new System.Timers.Timer(60000);:)

1 个答案:

答案 0 :(得分:0)

好的,这是错误的。 我从froogleservice install()方法中删除了任务创建。

现在我需要使用CreateAmazonInventoryUpdateTask课程:

public void InsertTask(Core.Domain.Tasks.ScheduleTask task)
    {
        task.Enabled = true;
        task.Name = "Amazon Inventory Update Sync";
        task.Type = "Nop.Plugin.Feed.Froogle.Tasks.CreateAmazonInventoryUpdateTask, Nop.Plugin.Feed.Froogle";
        task.Seconds = 60;
        task.StopOnError = false;
    }

在数据库中还有一个表dbo.ScheduleTask 我需要手动添加我的任务以及代码。

立即行动:)