Skip to content

Commit

Permalink
refactor(Program.cs): improve job management logic to support pausing…
Browse files Browse the repository at this point in the history
… specific jobs

feat(Program.cs): add console logs for PAUSED_JOBS env var and trigger names for better debugging
style(Program.cs): improve code readability by reformatting service registration and ListenAnyIP calls
fix(Program.cs): remove unnecessary whitespace and ensure file ends with a newline for POSIX compliance
  • Loading branch information
Jossec101 committed Dec 6, 2023
1 parent 71af1f3 commit 5cbb474
Showing 1 changed file with 35 additions and 33 deletions.
68 changes: 35 additions & 33 deletions src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@

namespace NodeGuard
{

public class Program
{
public static void Main(string[] args)
Expand All @@ -58,46 +57,47 @@ public static void Main(string[] args)

async Task ManageJobs(ISchedulerFactory schedulerFactory)
{
var pauseJobs = int.Parse(Environment.GetEnvironmentVariable("PAUSE_JOBS") ?? "0");
var pauseJobs = Environment.GetEnvironmentVariable("PAUSED_JOBS");
Console.WriteLine($"PAUSED_JOBS env var is {pauseJobs}");
var scheduler = Task.Run(() => schedulerFactory.GetScheduler()).Result;
var triggers = (await scheduler.GetTriggerKeys(GroupMatcher<TriggerKey>.AnyGroup())).ToList();

var triggersIndex = triggers.Select(x=> new
{
name = x.Name,
index = triggers.IndexOf(x)
}).ToList();
Console.WriteLine("Triggers:");

triggersIndex.ForEach(x=> Console.WriteLine($"{x.index} - {x.name}"));

switch (pauseJobs)
{
case 0:
case "all":
await scheduler.PauseAll();
Console.WriteLine("All jobs paused");
break;
case > 0:
{
await scheduler.ResumeAll();


var jobCounter = 0;
case null:
//Resume all
await scheduler.ResumeAll();
break;
default:
await scheduler.ResumeAll();

// Get a list of all triggers
var allTriggerKeys = await scheduler.GetTriggerKeys(GroupMatcher<TriggerKey>.AnyGroup());
allTriggerKeys = allTriggerKeys.OrderBy(x=> x.Name).ToList();
foreach (var triggerKey in allTriggerKeys)
triggers = triggers.OrderBy(x => x.Name).ToList();
foreach (var triggerKey in triggers)
{
if (jobCounter >= pauseJobs)
break;

var trigger = await scheduler.GetTrigger(triggerKey);

if (trigger != null)
if (trigger != null && pauseJobs.Contains(triggers.IndexOf(triggerKey).ToString()))
{
Console.WriteLine($"Pausing job trigger {trigger}");
await scheduler.PauseTrigger(triggerKey);
}

jobCounter++;
}

break;
}
default:
//Resume all
await scheduler.ResumeAll();
break;
}
}
Expand Down Expand Up @@ -144,18 +144,22 @@ async Task ManageJobs(ISchedulerFactory schedulerFactory)
builder.Services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
builder.Services.AddTransient<IApplicationUserRepository, ApplicationUserRepository>();
builder.Services.AddTransient<IAPITokenRepository, APITokenRepository>();
builder.Services.AddTransient<IChannelOperationRequestRepository, ChannelOperationRequestRepository>();
builder.Services
.AddTransient<IChannelOperationRequestPSBTRepository, ChannelOperationRequestPSBTRepository>();
.AddTransient<IChannelOperationRequestRepository, ChannelOperationRequestRepository>();
builder.Services
.AddTransient<IChannelOperationRequestPSBTRepository,
ChannelOperationRequestPSBTRepository>();
builder.Services.AddTransient<IChannelRepository, ChannelRepository>();
builder.Services.AddTransient<IKeyRepository, KeyRepository>();
builder.Services.AddTransient<INodeRepository, NodeRepository>();
builder.Services.AddTransient<IWalletRepository, WalletRepository>();
builder.Services.AddTransient<IInternalWalletRepository, InternalWalletRepository>();
builder.Services.AddTransient<IFMUTXORepository, FUTXORepository>();
builder.Services
.AddTransient<IWalletWithdrawalRequestPsbtRepository, WalletWithdrawalRequestPsbtRepository>();
builder.Services.AddTransient<IWalletWithdrawalRequestRepository, WalletWithdrawalRequestRepository>();
.AddTransient<IWalletWithdrawalRequestPsbtRepository,
WalletWithdrawalRequestPsbtRepository>();
builder.Services
.AddTransient<IWalletWithdrawalRequestRepository, WalletWithdrawalRequestRepository>();
builder.Services.AddTransient<IRemoteSignerService, RemoteSignerServiceService>();
builder.Services.AddTransient<ILiquidityRuleRepository, LiquidityRuleRepository>();
builder.Services.AddTransient<ICoinSelectionService, CoinSelectionService>();
Expand Down Expand Up @@ -208,9 +212,10 @@ async Task ManageJobs(ISchedulerFactory schedulerFactory)
// Setup a HTTP/2 endpoint without TLS.
options.ListenAnyIP(50051, o => o.Protocols =
HttpProtocols.Http2);
options.ListenAnyIP(int.Parse(Environment.GetEnvironmentVariable("HTTP1_LISTEN_PORT") ?? "80"), o =>
o.Protocols =
HttpProtocols.Http1);
options.ListenAnyIP(
int.Parse(Environment.GetEnvironmentVariable("HTTP1_LISTEN_PORT") ?? "80"), o =>
o.Protocols =
HttpProtocols.Http1);
});

//DBContextFactory
Expand Down Expand Up @@ -422,7 +427,4 @@ async Task ManageJobs(ISchedulerFactory schedulerFactory)
app.Run();
}
}
}



}

0 comments on commit 5cbb474

Please sign in to comment.