Ok so I have created a task I would like to run every hour:
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);
}
}
}
Now inside the froogle service I have added the task when the plugin is installed:
/// <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");
}
The task will generate a feed to submit to google. But the problem is the task won't run.
From what I googled everything looks right, not sure what I'm missing.
Also yes this is 1 min new System.Timers.Timer(60000);
:)