Migrating to Ratermania.Automation
This commit is contained in:
@@ -1,21 +1,32 @@
|
||||
using PartSource.Automation.Jobs.Interfaces;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using PartSource.Automation.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class TestJob : IAutomationJob
|
||||
{
|
||||
public async Task<AutomationJobResult> Run()
|
||||
{
|
||||
return new AutomationJobResult
|
||||
{
|
||||
Message = "Test job ran successfully from the new server",
|
||||
IsSuccess = true
|
||||
};
|
||||
}
|
||||
}
|
||||
public class TestJob : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<TestJob> _logger;
|
||||
|
||||
public TestJob(ILogger<TestJob> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
#pragma warning disable CS1998, CA1303
|
||||
public async Task Run()
|
||||
{
|
||||
await Task.Delay(5000);
|
||||
|
||||
_logger.LogInformation("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc scelerisque congue euismod. Curabitur enim eros, sollicitudin ac purus eget, dignissim mattis augue. In quam sapien, tincidunt et elementum vitae, interdum vitae sem.");
|
||||
_logger.LogWarning("Praesent feugiat sapien non suscipit faucibus. Mauris fermentum ut augue a feugiat. Integer felis sem, laoreet et augue at, finibus maximus ex. Fusce sit amet erat non tortor porta condimentum condimentum quis ipsum.");
|
||||
_logger.LogError("Sed fringilla placerat turpis, sed tristique mi malesuada quis. Sed a justo erat. In iaculis, orci pulvinar tempor accumsan, mi leo rutrum lorem, ut egestas arcu ligula sodales dolor.");
|
||||
_logger.LogCritical("Donec pulvinar vehicula massa. Praesent non erat tortor. Duis posuere tortor sed odio iaculis, sit amet eleifend est tincidunt. Suspendisse rhoncus eros id purus aliquet, ut porttitor lectus eleifend.");
|
||||
}
|
||||
#pragma warning restore CS1998, CA1303
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using PartSource.Automation.Jobs.Interfaces;
|
||||
using Ratermania.Automation.Interfaces;
|
||||
using PartSource.Automation.Models;
|
||||
using PartSource.Data;
|
||||
using PartSource.Data.Models;
|
||||
@@ -11,22 +11,24 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace PartSource.Automation.Jobs
|
||||
{
|
||||
public class UpdatePricing : IAutomationJob
|
||||
{
|
||||
private readonly ILogger<UpdatePricing> _logger;
|
||||
private readonly PartSourceContext _partSourceContext;
|
||||
private readonly ShopifyClient _shopifyClient;
|
||||
|
||||
public UpdatePricing(PartSourceContext partSourceContext, ShopifyClient shopifyClient)
|
||||
public UpdatePricing(ILogger<UpdatePricing> logger, PartSourceContext partSourceContext, ShopifyClient shopifyClient)
|
||||
{
|
||||
_logger = logger;
|
||||
_partSourceContext = partSourceContext;
|
||||
_shopifyClient = shopifyClient;
|
||||
|
||||
}
|
||||
|
||||
public async Task<AutomationJobResult> Run()
|
||||
public async Task Run()
|
||||
{
|
||||
IEnumerable<Product> products = null;
|
||||
IEnumerable<PartPrice> prices = null;
|
||||
@@ -40,12 +42,8 @@ namespace PartSource.Automation.Jobs
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
// TODO: Logging
|
||||
return new AutomationJobResult
|
||||
{
|
||||
Message = "Failed to get products from Shopify",
|
||||
IsSuccess = false
|
||||
};
|
||||
_logger.LogError(ex, "Failed to get the initial set of products from Shopify.");
|
||||
return;
|
||||
}
|
||||
|
||||
while (products != null && products.Any())
|
||||
@@ -54,7 +52,6 @@ namespace PartSource.Automation.Jobs
|
||||
{
|
||||
if (product.Variants.Length > 0)
|
||||
{
|
||||
|
||||
Variant variant = product.Variants[0];
|
||||
PartPrice partPrice = prices.Where(p => p.SKU == variant.Sku).FirstOrDefault();
|
||||
|
||||
@@ -63,74 +60,54 @@ namespace PartSource.Automation.Jobs
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
|
||||
if (product.Variants[0].Price != partPrice.Your_Price.Value || product.Variants[0].CompareAtPrice != partPrice.Compare_Price.Value)
|
||||
{
|
||||
if (product.Variants[0].Price != partPrice.Your_Price.Value || product.Variants[0].CompareAtPrice != partPrice.Compare_Price.Value)
|
||||
product.Variants[0].Price = partPrice.Your_Price.Value;
|
||||
product.Variants[0].CompareAtPrice = partPrice.Compare_Price.Value;
|
||||
|
||||
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
|
||||
product.PublishedScope = PublishedScope.Global;
|
||||
|
||||
Metafield metafield = new Metafield
|
||||
{
|
||||
product.Variants[0].Price = partPrice.Your_Price.Value;
|
||||
product.Variants[0].CompareAtPrice = partPrice.Compare_Price.Value;
|
||||
Namespace = "Pricing",
|
||||
Key = "CorePrice",
|
||||
Value = partPrice.Core_Price.HasValue ? partPrice.Core_Price.Value.ToString() : "0.00",
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
product.PublishedAt = partPrice.Active.Trim().ToUpperInvariant() == "Y" ? (DateTime?)DateTime.Now : null;
|
||||
product.PublishedScope = PublishedScope.Global;
|
||||
try
|
||||
{
|
||||
await _shopifyClient.Metafields.Add(metafield);
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
Metafield metafield = new Metafield
|
||||
{
|
||||
Namespace = "Pricing",
|
||||
Key = "CorePrice",
|
||||
Value = partPrice.Core_Price.HasValue ? partPrice.Core_Price.Value.ToString() : "0.00",
|
||||
ValueType = "string",
|
||||
OwnerResource = "product",
|
||||
OwnerId = product.Id
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
await _shopifyClient.Metafields.Add(metafield);
|
||||
await _shopifyClient.Products.Update(product);
|
||||
|
||||
updateCount++;
|
||||
|
||||
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("bad update");
|
||||
}
|
||||
updateCount++;
|
||||
}
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("failed getting parts");
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"Failed to update pricing for SKU {variant.Sku}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// _partSourceContext.SaveChanges();
|
||||
|
||||
try
|
||||
{
|
||||
//await _shopifyClient.Products.SaveChanges();
|
||||
|
||||
products = await _shopifyClient.Products.GetNext();
|
||||
|
||||
Console.SetCursorPosition(0, 2);
|
||||
Console.Clear();
|
||||
Console.Write($"Updated: {updateCount} ");
|
||||
_logger.LogInformation($"Total updated: {updateCount}");
|
||||
}
|
||||
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to get the next set of products. Retrying");
|
||||
products = await _shopifyClient.Products.GetPrevious();
|
||||
}
|
||||
}
|
||||
|
||||
return new AutomationJobResult
|
||||
{
|
||||
Message = $"The nightly pricing update has completed. {updateCount} products were updated.",
|
||||
IsSuccess = true
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user