Create service to populate Imdb data collection within mongo
We can use this collection as an alternative source to lookup imdb ids, which would be executed before name_to_imdb is called in the consumer.
This commit is contained in:
10
src/metadata/Features/Jobs/BaseJob.cs
Normal file
10
src/metadata/Features/Jobs/BaseJob.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace Metadata.Features.Jobs;
|
||||
|
||||
public abstract class BaseJob : IMetadataJob
|
||||
{
|
||||
public abstract bool IsScheduelable { get; }
|
||||
|
||||
public abstract string JobName { get; }
|
||||
|
||||
public abstract Task Invoke();
|
||||
}
|
||||
7
src/metadata/Features/Jobs/IMetadataJob.cs
Normal file
7
src/metadata/Features/Jobs/IMetadataJob.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Metadata.Features.Jobs;
|
||||
|
||||
public interface IMetadataJob : IInvocable
|
||||
{
|
||||
bool IsScheduelable { get; }
|
||||
string JobName { get; }
|
||||
}
|
||||
34
src/metadata/Features/Jobs/JobScheduler.cs
Normal file
34
src/metadata/Features/Jobs/JobScheduler.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
namespace Metadata.Features.Jobs;
|
||||
|
||||
public class JobScheduler(IServiceProvider serviceProvider) : IHostedService
|
||||
{
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
using var scope = serviceProvider.CreateAsyncScope();
|
||||
|
||||
var mongoDbService = scope.ServiceProvider.GetRequiredService<ImdbMongoDbService>();
|
||||
|
||||
if (!mongoDbService.IsDatabaseInitialized())
|
||||
{
|
||||
throw new InvalidOperationException("MongoDb is not initialized");
|
||||
}
|
||||
|
||||
var jobConfigurations = scope.ServiceProvider.GetRequiredService<JobConfiguration>();
|
||||
var downloadJob = scope.ServiceProvider.GetRequiredService<DownloadImdbDataJob>();
|
||||
|
||||
if (!downloadJob.IsScheduelable)
|
||||
{
|
||||
return downloadJob.Invoke();
|
||||
}
|
||||
|
||||
var scheduler = scope.ServiceProvider.GetRequiredService<IScheduler>();
|
||||
|
||||
scheduler.Schedule<DownloadImdbDataJob>()
|
||||
.Cron(jobConfigurations.DownloadImdbCronSchedule)
|
||||
.PreventOverlapping(nameof(downloadJob.JobName));
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
Reference in New Issue
Block a user