Commit 526bb6cf authored by lap nguyen's avatar lap nguyen

change background tasks package

parent 59644c18
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.33" />
<PackageReference Include="Hangfire.Core" Version="1.7.33" />
<PackageReference Include="Hangfire.PostgreSql" Version="1.8.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.32" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.32" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.30" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.30" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.30"> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.30">
......
...@@ -16,12 +16,8 @@ namespace News_site.Services ...@@ -16,12 +16,8 @@ namespace News_site.Services
_logger = logger; _logger = logger;
} }
public async Task DoCrawlData(CancellationToken cancellationToken) public async Task DoCrawlData()
{ {
while (!cancellationToken.IsCancellationRequested)
{
/* 30 minutes */
var duration = 1000 * 60 * 30;
var startCrawlingTime = DateTime.Now; var startCrawlingTime = DateTime.Now;
_logger.LogInformation($"Start crawling at: {startCrawlingTime:HH:mm}"); _logger.LogInformation($"Start crawling at: {startCrawlingTime:HH:mm}");
...@@ -30,9 +26,6 @@ namespace News_site.Services ...@@ -30,9 +26,6 @@ namespace News_site.Services
var stopCrawlingTime = DateTime.Now; var stopCrawlingTime = DateTime.Now;
_logger.LogInformation($"Stop task, crawling in {(stopCrawlingTime - startCrawlingTime).TotalSeconds} seconds"); _logger.LogInformation($"Stop task, crawling in {(stopCrawlingTime - startCrawlingTime).TotalSeconds} seconds");
await Task.Delay(duration);
}
} }
} }
} }
...@@ -17,7 +17,6 @@ namespace News_site.Services ...@@ -17,7 +17,6 @@ namespace News_site.Services
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{ {
await _cronbot.DoCrawlData(stoppingToken);
} }
} }
} }
...@@ -5,6 +5,6 @@ namespace News_site.Services ...@@ -5,6 +5,6 @@ namespace News_site.Services
{ {
public interface ICronbot public interface ICronbot
{ {
Task DoCrawlData(CancellationToken cancellationToken); Task DoCrawlData();
} }
} }
\ No newline at end of file
...@@ -8,6 +8,9 @@ using News_site.Data; ...@@ -8,6 +8,9 @@ using News_site.Data;
using News_site.Services; using News_site.Services;
using Sieve.Models; using Sieve.Models;
using Sieve.Services; using Sieve.Services;
using Hangfire;
using Hangfire.PostgreSql;
using System;
namespace News_site namespace News_site
{ {
...@@ -36,10 +39,17 @@ namespace News_site ...@@ -36,10 +39,17 @@ namespace News_site
}); });
}); });
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UsePostgreSqlStorage(Configuration.GetConnectionString("HangfireConnection"), new PostgreSqlStorageOptions()));
services.AddHangfireServer();
services.Configure<SieveOptions>(Configuration.GetSection("Sieve")); services.Configure<SieveOptions>(Configuration.GetSection("Sieve"));
services.AddSingleton<ICronbot, Cronbot>(); services.AddSingleton<ICronbot, Cronbot>();
services.AddHostedService<CronjobService>();
services.AddScoped<SieveProcessor>(); services.AddScoped<SieveProcessor>();
services.AddDbContext<DataContext>(); services.AddDbContext<DataContext>();
...@@ -57,7 +67,13 @@ namespace News_site ...@@ -57,7 +67,13 @@ namespace News_site
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public void Configure(
IApplicationBuilder app,
IBackgroundJobClient backgroundJobs,
IWebHostEnvironment env,
IRecurringJobManager recurringJobManager,
IServiceProvider serviceProvider
)
{ {
app.UseSwagger(); app.UseSwagger();
...@@ -71,6 +87,9 @@ namespace News_site ...@@ -71,6 +87,9 @@ namespace News_site
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
} }
app.UseHangfireDashboard();
recurringJobManager.AddOrUpdate("Crawl Data every 30 minutes", () => serviceProvider.GetService<ICronbot>().DoCrawlData(), "*/30 * * * *", TimeZoneInfo.Local);
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.UseRouting(); app.UseRouting();
...@@ -82,6 +101,7 @@ namespace News_site ...@@ -82,6 +101,7 @@ namespace News_site
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapControllers(); endpoints.MapControllers();
endpoints.MapHangfireDashboard();
}); });
} }
} }
......
{ {
"ConnectionStrings": {
"HangfireConnection": "{YOUR CONNECTION STRING}"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
"Hangfire": "Information",
"Microsoft": "Warning", "Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment