Created
September 4, 2018 15:35
-
-
Save kfimchenko/f0d564cc017a0e3a5a31db70797a6f4b to your computer and use it in GitHub Desktop.
recurrentjob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void UpdateProduct() | |
{ | |
var productToUpdate = _context.FollowProducts | |
.FirstOrDefault(fp => fp.LastUpdate < DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Unspecified)); | |
if (productToUpdate == null) | |
return; | |
var updatedProduct = _shops.GetProduct(productToUpdate.ShopType, productToUpdate.URL); | |
if (updatedProduct.Price < productToUpdate.Money) | |
{ | |
productToUpdate.Money = updatedProduct.Price; | |
} | |
productToUpdate.LastUpdate = DateTime.Now; | |
_context.SaveChanges(); | |
} | |
// Startup.cs | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddSingleton<IChannel>(new Channel(Configuration)); | |
services.AddSingleton<IShopContainer>(new ShopContainer()); | |
services.AddHangfire(o => o.UsePostgreSqlStorage("User ID=postgres;Password=rootpass;Server=localhost;Database=promo;Enlist=false;Pooling=false")); | |
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); | |
//services.AddEntityFrameworkNpgsql().AddDbContext<PromoContext>(o => o.UseNpgsql("Server=localhost;Database=promo")); | |
services.AddDbContext<PromoContext>(o => o.UseNpgsql("User ID=postgres;Password=rootpass;Server=localhost;Database=promo")); | |
services.AddScoped<IChecker, Checker>(); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
var options = new BackgroundJobServerOptions { WorkerCount = 3 }; | |
app.UseHangfireServer(options); | |
app.UseHangfireDashboard(); | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
else | |
{ | |
app.UseHsts(); | |
} | |
//app.UseHttpsRedirection(); | |
app.UseMvc(); | |
RecurringJob.AddOrUpdate((IChecker ch) => ch.UpdateProduct(), "0/1 * * * *"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment