-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from Etherna/improve/BHM-133-cleanup-hangfire
Add task to clean up old failed Hangfire tasks
- Loading branch information
Showing
6 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2021-present Etherna SA | ||
// This file is part of Beehive. | ||
// | ||
// Beehive is free software: you can redistribute it and/or modify it under the terms of the | ||
// GNU Affero General Public License as published by the Free Software Foundation, | ||
// either version 3 of the License, or (at your option) any later version. | ||
// | ||
// Beehive is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// See the GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License along with Beehive. | ||
// If not, see <https://www.gnu.org/licenses/>. | ||
|
||
using Etherna.Beehive.Services.Extensions; | ||
using Hangfire; | ||
using Hangfire.Storage.Monitoring; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Etherna.Beehive.Services.Tasks | ||
{ | ||
public class CleanupOldFailedTasksTask(ILogger<CleanupOldFailedTasksTask> logger) | ||
: ICleanupOldFailedTasksTask | ||
{ | ||
// Consts. | ||
public const string TaskId = "cleanupOldFailedTasksTask"; | ||
|
||
// Fields. | ||
private readonly TimeSpan retentionTime = new(30, 0, 0, 0); | ||
|
||
// Methods. | ||
public Task RunAsync() | ||
{ | ||
var api = JobStorage.Current.GetMonitoringApi(); | ||
var tasksToSkip = 0; | ||
var deletedTasks = 0L; | ||
JobList<FailedJobDto> failedJobs; | ||
|
||
// Log begin. | ||
logger.CleanupOldFailedTasksTaskStarted(); | ||
|
||
// Run clean up. | ||
do | ||
{ | ||
failedJobs = api.FailedJobs(tasksToSkip, 1000 /* limit */); | ||
|
||
foreach (var job in failedJobs) | ||
{ | ||
if (job.Value.FailedAt is not null && | ||
DateTime.UtcNow - job.Value.FailedAt > retentionTime) | ||
{ | ||
BackgroundJob.Delete(job.Key); | ||
deletedTasks++; | ||
} | ||
else | ||
tasksToSkip++; | ||
} | ||
} while (failedJobs.Count > 0); | ||
|
||
// Log result. | ||
logger.CleanupOldFailedTasksTaskCompleted(deletedTasks); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2021-present Etherna SA | ||
// This file is part of Beehive. | ||
// | ||
// Beehive is free software: you can redistribute it and/or modify it under the terms of the | ||
// GNU Affero General Public License as published by the Free Software Foundation, | ||
// either version 3 of the License, or (at your option) any later version. | ||
// | ||
// Beehive is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
// See the GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License along with Beehive. | ||
// If not, see <https://www.gnu.org/licenses/>. | ||
|
||
using Hangfire; | ||
using System.Threading.Tasks; | ||
|
||
namespace Etherna.Beehive.Services.Tasks | ||
{ | ||
/// <summary> | ||
/// Delete failed tasks older than a month from Hangfire | ||
/// </summary> | ||
public interface ICleanupOldFailedTasksTask | ||
{ | ||
[Queue(Queues.DB_MAINTENANCE)] | ||
Task RunAsync(); | ||
} | ||
} |
This file contains 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
This file contains 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