Skip to content

Commit

Permalink
Node disable/enabled. (#344)
Browse files Browse the repository at this point in the history
* Node disable/enabled.

* Code improvements

* Removed unscheduled and interrupted job.

* Update src/NodeGuard.csproj

Co-authored-by: Rodrigo <[email protected]>

* Update src/Pages/Nodes.razor

Co-authored-by: Rodrigo <[email protected]>

* Fix a messsage of the toast.

* Added some improvements.

* Returned migration logic.

* Added migrations files.

* Redid migration and renamed field

---------

Co-authored-by: Rodrigo <[email protected]>
  • Loading branch information
2 people authored and Jossec101 committed Dec 8, 2023
1 parent 1c12a45 commit a6852c4
Show file tree
Hide file tree
Showing 7 changed files with 1,497 additions and 46 deletions.
5 changes: 5 additions & 0 deletions src/Data/Models/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public class Node : Entity

public Wallet? ReturningFundsWallet { get; set; }

/// <summary>
/// enable/disable node
/// </summary>
public bool IsNodeDisabled { get; set; }

/// <summary>
/// Returns true if the node is managed by us. We defer this from the existence of an Endpoint
/// </summary>
Expand Down
11 changes: 7 additions & 4 deletions src/Data/Repositories/NodeRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public NodeRepository(IRepository<Node> repository,
.Include(x => x.ReturningFundsWallet)
.SingleOrDefaultAsync(x => x.PubKey == key);
}


public async Task<Node> GetOrCreateByPubKey(string pubKey, ILightningService lightningService)
{
Expand Down Expand Up @@ -113,15 +114,17 @@ public async Task<List<Node>> GetAllManagedByNodeGuard()
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();

var resultAsync = await applicationDbContext.Nodes
var query = applicationDbContext.Nodes
.Include(x => x.ReturningFundsWallet)
.ThenInclude(x => x.Keys)
.Where(node => node.Endpoint != null)
.ToListAsync();
.Include(x => x.ReturningFundsWallet)
.Where(node => node.Endpoint != null);

var resultAsync = await query.ToListAsync();

return resultAsync;
}

public async Task<List<Node>> GetAllManagedByUser(string userId)
{
await using var applicationDbContext = await _dbContextFactory.CreateDbContextAsync();
Expand Down
71 changes: 58 additions & 13 deletions src/Helpers/JobTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/

using Quartz;
using Quartz.Impl;
using Quartz.Impl.Matchers;
using Quartz.Impl.Triggers;

namespace NodeGuard.Helpers;
Expand All @@ -33,9 +35,9 @@ public class SimpleJob
public static JobAndTrigger Create<T>(JobDataMap data, string identitySuffix) where T : IJob
{
var job = JobBuilder.Create<T>()
.WithIdentity($"{typeof(T).Name}-{identitySuffix}")
.SetJobData(data ?? new JobDataMap())
.Build();
.WithIdentity($"{typeof(T).Name}-{identitySuffix}")
.SetJobData(data ?? new JobDataMap())
.Build();

var trigger = TriggerBuilder.Create()
.WithIdentity($"{typeof(T).Name}Trigger-{identitySuffix}")
Expand All @@ -44,6 +46,46 @@ public static JobAndTrigger Create<T>(JobDataMap data, string identitySuffix) wh

return new JobAndTrigger(job, trigger);
}

public static async Task Reschedule<T>(IScheduler scheduler, string identitySuffix) where T : IJob
{
try
{
var triggerKey = new TriggerKey($"{typeof(T).Name}-{identitySuffix}");

var trigger = TriggerBuilder.Create()
.WithIdentity($"{typeof(T).Name}Trigger-{identitySuffix}")
.StartNow()
.Build();

await scheduler.RescheduleJob(triggerKey, trigger);

}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error rescheduling job of type {typeof(T).Name} with identity suffix {identitySuffix}: {ex.Message}");
}
}

public static async Task DeleteJob<T>(IScheduler scheduler, string identitySuffix) where T : IJob
{
try
{
JobKey jobKey = new JobKey($"{typeof(T).Name}-{identitySuffix}");
await scheduler.DeleteJob(jobKey);
}
catch (Exception ex)
{
Console.WriteLine($"Unexpected error occurred while removing the job of type {typeof(T).Name} with identity suffix {identitySuffix}: {ex.Message}");
}
}

public static async Task<bool> IsJobExists<T>(IScheduler scheduler, string identitySuffix) where T : IJob
{
JobKey jobKey = new JobKey($"{typeof(T).Name}-{identitySuffix}");
return await scheduler.CheckExists(jobKey);
}

}

public class RetriableJob
Expand All @@ -55,18 +97,19 @@ public class RetriableJob
/// <param name="identitySuffix">A suffix to identify a specific job, triggered from the same class</param>
/// <param name="intervalListInMinutes">An optional list of retry intervals in minutes, defaults to { 1, 5, 10, 20 }</param>
/// <returns>An object with a job and a trigger to pass to a scheduler</returns>
public static JobAndTrigger Create<T>(JobDataMap data, string identitySuffix, int[]? intervalListInMinutes = null) where T : IJob
public static JobAndTrigger Create<T>(JobDataMap data, string identitySuffix, int[]? intervalListInMinutes = null)
where T : IJob
{
intervalListInMinutes = intervalListInMinutes ?? new int[] { 1, 5, 10, 20 };

var map = data ?? new JobDataMap();
map.Put("intervalListInMinutes", intervalListInMinutes);

var job = JobBuilder.Create<T>()
.DisallowConcurrentExecution()
.SetJobData(map)
.WithIdentity($"{typeof(T).Name}-{identitySuffix}")
.Build();
.DisallowConcurrentExecution()
.SetJobData(map)
.WithIdentity($"{typeof(T).Name}-{identitySuffix}")
.Build();

var trigger = TriggerBuilder.Create()
.WithIdentity($"{typeof(T).Name}Trigger-{identitySuffix}")
Expand Down Expand Up @@ -103,7 +146,7 @@ public static async Task Execute(IJobExecutionContext context, Func<Task> callba
if (intervals == null)
{
throw new Exception("No interval list found, make sure you're using the RetriableJob class");
};
}

var trigger = context.Trigger as SimpleTriggerImpl;

Expand Down Expand Up @@ -134,7 +177,7 @@ public static async Task OnFail(IJobExecutionContext context, Func<Task> callbac
if (intervals == null)
{
throw new Exception("No interval list found, make sure you're using the RetriableJob class");
};
}

var trigger = context.Trigger as SimpleTriggerImpl;

Expand All @@ -144,7 +187,7 @@ public static async Task OnFail(IJobExecutionContext context, Func<Task> callbac
await callback();
}
}

/// <summary>
/// Get the next interval time
/// </summary>
Expand All @@ -156,7 +199,7 @@ public static async Task OnFail(IJobExecutionContext context, Func<Task> callbac
if (intervals == null)
{
throw new Exception("No interval list found, make sure you're using the RetriableJob class");
};
}

var trigger = context.Trigger as SimpleTriggerImpl;

Expand All @@ -166,7 +209,7 @@ public static async Task OnFail(IJobExecutionContext context, Func<Task> callbac
}

return null;
}
}
}

public class JobAndTrigger
Expand All @@ -180,10 +223,12 @@ public JobAndTrigger(IJobDetail job, ITrigger trigger)
{
throw new Exception("Job parameter is needed");
}

if (trigger == null)
{
throw new Exception("Trigger parameter is needed");
}

Job = job;
Trigger = trigger;
}
Expand Down
Loading

0 comments on commit a6852c4

Please sign in to comment.