Skip to content

Commit

Permalink
feat(back): disable generator by default
Browse files Browse the repository at this point in the history
  • Loading branch information
agjini committed Aug 17, 2023
1 parent daff017 commit 0290d4d
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 3 deletions.
14 changes: 13 additions & 1 deletion back/src/Liane/Liane.Api/Util/CronJobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@ public abstract class CronJobService : IHostedService, IDisposable
private readonly TimeZoneInfo timeZoneInfo;
private readonly bool runImmediately;
private readonly ILogger logger;
private readonly bool isEnabled;

protected CronJobService(ILogger logger, string cronExpression, bool runImmediately)
protected CronJobService(ILogger logger, string cronExpression, bool runImmediately, bool isEnabled = true)
{
this.logger = logger;
expression = CronExpression.Parse(cronExpression);
this.runImmediately = runImmediately;
timeZoneInfo = TimeZoneInfo.Local;
this.isEnabled = isEnabled;
}

public virtual async Task StartAsync(CancellationToken cancellationToken)
{
if (!isEnabled)
{
return;
}

try
{
await ScheduleJob(cancellationToken);
Expand Down Expand Up @@ -87,6 +94,11 @@ private async Task ScheduleJob(CancellationToken cancellationToken)

public virtual async Task StopAsync(CancellationToken cancellationToken)
{
if (!isEnabled)
{
return;
}

logger.LogInformation("{job} stopped", GetType().Name);
timer?.Stop();
await Task.CompletedTask;
Expand Down
3 changes: 3 additions & 0 deletions back/src/Liane/Liane.Mock/GeneratorSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Liane.Mock;

public sealed record GeneratorSettings(bool IsEnabled);
2 changes: 1 addition & 1 deletion back/src/Liane/Liane.Mock/LianeMockGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class LianeMockGenerator : CronJobService
private readonly IMockService mockService;
private readonly ILogger<LianeMockGenerator> logger;

public LianeMockGenerator(ILogger<LianeMockGenerator> logger, IMockService mockService) : base(logger, CronExpression, false)
public LianeMockGenerator(ILogger<LianeMockGenerator> logger, IMockService mockService, GeneratorSettings settings) : base(logger, CronExpression, false, settings.IsEnabled)
{
this.logger = logger;
this.mockService = mockService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using MongoDB.Driver.Core.Operations;

namespace Liane.Web.Internal.Debug;

Expand Down
1 change: 1 addition & 0 deletions back/src/Liane/Liane.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private static void ConfigureLianeServices(WebHostBuilderContext context, IServi

services.AddService<MockServiceImpl>();

services.AddSettings<GeneratorSettings>(context);
services.AddHostedService<LianeMockGenerator>();
services.AddHostedService<LianeStatusUpdate>();

Expand Down
3 changes: 3 additions & 0 deletions back/src/Liane/Liane.Web/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
"Validity": "0.08:00:00",
"Issuer": "Liane",
"Audience": "Liane"
},
"Generator": {
"IsEnabled": false
}
}
1 change: 1 addition & 0 deletions deploy/liane.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ services:
LIANE_Cloudflare__AccountId: ${CLOUDFLARE_ACCOUNT_ID}
LIANE_Cloudflare__AccountHash: ${CLOUDFLARE_ACCOUNT_HASH}
LIANE_Cloudflare__ApiKey: ${CLOUDFLARE_API_KEY}
LIANE_Generator__IsEnabled: ${GENERATOR_IS_ENABLED}
restart: unless-stopped
networks:
- gateway
Expand Down

0 comments on commit 0290d4d

Please sign in to comment.