Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
newspaper backend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lap nguyen
newspaper backend
Commits
526bb6cf
Commit
526bb6cf
authored
Feb 06, 2023
by
lap nguyen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
change background tasks package
parent
59644c18
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
37 additions
and
18 deletions
+37
-18
News site.csproj
News site.csproj
+3
-0
Cronbot.cs
Services/Cronbot.cs
+7
-14
CronjobService.cs
Services/CronjobService.cs
+0
-1
ICronbot.cs
Services/ICronbot.cs
+1
-1
Startup.cs
Startup.cs
+22
-2
appsettings.json
appsettings.json
+4
-0
No files found.
News site.csproj
View file @
526bb6cf
...
...
@@ -10,6 +10,9 @@
</PropertyGroup>
<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.EntityFrameworkCore" Version="3.1.30" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.30">
...
...
Services/Cronbot.cs
View file @
526bb6cf
...
...
@@ -16,23 +16,16 @@ namespace News_site.Services
_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
;
_logger
.
LogInformation
(
$"Start crawling at:
{
startCrawlingTime
:
HH
:
mm
}
"
);
var
startCrawlingTime
=
DateTime
.
Now
;
_logger
.
LogInformation
(
$"Start crawling at:
{
startCrawlingTime
:
HH
:
mm
}
"
);
await
_dataProvider
.
CrawlCategories
();
await
_dataProvider
.
CrawlLatestNews
();
await
_dataProvider
.
CrawlCategories
();
await
_dataProvider
.
CrawlLatestNews
();
var
stopCrawlingTime
=
DateTime
.
Now
;
_logger
.
LogInformation
(
$"Stop task, crawling in
{(
stopCrawlingTime
-
startCrawlingTime
).
TotalSeconds
}
seconds"
);
await
Task
.
Delay
(
duration
);
}
var
stopCrawlingTime
=
DateTime
.
Now
;
_logger
.
LogInformation
(
$"Stop task, crawling in
{(
stopCrawlingTime
-
startCrawlingTime
).
TotalSeconds
}
seconds"
);
}
}
}
Services/CronjobService.cs
View file @
526bb6cf
...
...
@@ -17,7 +17,6 @@ namespace News_site.Services
protected
override
async
Task
ExecuteAsync
(
CancellationToken
stoppingToken
)
{
await
_cronbot
.
DoCrawlData
(
stoppingToken
);
}
}
}
Services/ICronbot.cs
View file @
526bb6cf
...
...
@@ -5,6 +5,6 @@ namespace News_site.Services
{
public
interface
ICronbot
{
Task
DoCrawlData
(
CancellationToken
cancellationToken
);
Task
DoCrawlData
();
}
}
\ No newline at end of file
Startup.cs
View file @
526bb6cf
...
...
@@ -8,6 +8,9 @@ using News_site.Data;
using
News_site.Services
;
using
Sieve.Models
;
using
Sieve.Services
;
using
Hangfire
;
using
Hangfire.PostgreSql
;
using
System
;
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
.
AddSingleton
<
ICronbot
,
Cronbot
>();
services
.
AddHostedService
<
CronjobService
>();
services
.
AddScoped
<
SieveProcessor
>();
services
.
AddDbContext
<
DataContext
>();
...
...
@@ -57,7 +67,13 @@ namespace News_site
}
// 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
();
...
...
@@ -71,6 +87,9 @@ namespace News_site
app
.
UseDeveloperExceptionPage
();
}
app
.
UseHangfireDashboard
();
recurringJobManager
.
AddOrUpdate
(
"Crawl Data every 30 minutes"
,
()
=>
serviceProvider
.
GetService
<
ICronbot
>().
DoCrawlData
(),
"*/30 * * * *"
,
TimeZoneInfo
.
Local
);
app
.
UseHttpsRedirection
();
app
.
UseRouting
();
...
...
@@ -82,6 +101,7 @@ namespace News_site
app
.
UseEndpoints
(
endpoints
=>
{
endpoints
.
MapControllers
();
endpoints
.
MapHangfireDashboard
();
});
}
}
...
...
appsettings.json
View file @
526bb6cf
{
"ConnectionStrings"
:
{
"HangfireConnection"
:
"{YOUR CONNECTION STRING}"
},
"Logging"
:
{
"LogLevel"
:
{
"Default"
:
"Information"
,
"Hangfire"
:
"Information"
,
"Microsoft"
:
"Warning"
,
"Microsoft.Hosting.Lifetime"
:
"Information"
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment