无法将我的 nopcommerce 任务发送到 运行
Having trouble getting my nopcommerce task to run
好的,我已经创建了一个我想每小时 运行 执行的任务:
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 的供稿。但问题是任务不会 运行.
根据我 googled 的内容,一切看起来都是正确的,不确定我缺少什么。
还有,这是 1 分钟 new System.Timers.Timer(60000);
:)
好的,所以我错了。
我从 froogleservice install()
方法中删除了任务创建。
现在在我的 CreateAmazonInventoryUpdateTask
class 我需要使用:
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;
}
数据库里面还有一个tabledbo.ScheduleTask
我需要在这里以及代码中手动添加我的任务。
现在有效:)
好的,我已经创建了一个我想每小时 运行 执行的任务:
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 的供稿。但问题是任务不会 运行.
根据我 googled 的内容,一切看起来都是正确的,不确定我缺少什么。
还有,这是 1 分钟 new System.Timers.Timer(60000);
:)
好的,所以我错了。
我从 froogleservice install()
方法中删除了任务创建。
现在在我的 CreateAmazonInventoryUpdateTask
class 我需要使用:
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;
}
数据库里面还有一个tabledbo.ScheduleTask 我需要在这里以及代码中手动添加我的任务。
现在有效:)