-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ze/labvm status job #58
base: dev
Are you sure you want to change the base?
Conversation
…b that is scheduled every minute
# Conflicts: # CSLabs.Api/Jobs/JobRegistry.cs # CSLabs.Api/Jobs/VmStatusJob.cs # CSLabs.Api/Services/TestProxmoxConnectionService.cs
CSLabs.Api/Jobs/VmStatusJob.cs
Outdated
using var context = scope.ServiceProvider.GetService<DefaultContext>(); | ||
|
||
// Do job | ||
var connectionService = new TestVmConnectionService(context); | ||
await connectionService.TestLabVmConnection(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add the following to the ServiceProvider.ProvideAppServices
method:
services.AddScoped<TestVmConnectionService>();
Then rewrite this section to handle the dependency injection:
using var context = scope.ServiceProvider.GetService<DefaultContext>(); | |
// Do job | |
var connectionService = new TestVmConnectionService(context); | |
await connectionService.TestLabVmConnection(); | |
var connectionService = scope.ServiceProvider.GetService<TestVmConnectionService>(); | |
await connectionService.TestLabVmConnection(); |
Then anything in the constructor of the TestVmConnectionService will be automatically injected.
…oved before merge
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WarriorAchilles
To make the code more testable and separate concerns, do the following:
Create an InjectedAsyncJob class.
using System;
using FluentScheduler;
using Microsoft.Extensions.DependencyInjection;
namespace CSLabs.Api.Jobs
{
public class InjectedAsyncJob<T> : IJob where T : AsyncJob
{
private IServiceProvider _provider;
public InjectedAsyncJob(IServiceProvider provider) => _provider = provider;
public void Execute()
{
using var scope = _provider.CreateScope();
scope.ServiceProvider.GetService<T>().Execute();
}
}
}
Change the VmStatusJob to the following:
using System.Threading.Tasks;
using CSLabs.Api.Services;
namespace CSLabs.Api.Jobs
{
public class VmStatusJob : AsyncJob
{
private readonly TestVmConnectionService _service;
public VmStatusJob(TestVmConnectionService service) =>
_service = service;
protected override async Task ExecuteAsync() => await _service.TestLabVmConnection();
}
}
Change the schedule call to the following:
Schedule(provider.GetService<InjectedAsyncJob<VmStatusJob>>).ToRunEvery(1).Minutes();
Add this to the ServiceProvider:
services.AddTransient<VmStatusJob>();
services.AddTransient<InjectedAsyncJob<VmStatusJob>>();
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
|
||
namespace CSLabs.Api.Proxmox | ||
{ | ||
public class ProxmoxDBApi : ProxmoxApi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You havent configured this ProxmoxDBApito be used anywhere but one place, it should be used instead of the Proxmox api since you want to track the start and stop states of the VM when initiated by the UI.
{ | ||
await base.ShutdownVm(vmId, timeout); | ||
// Save in the database that the VM is stopped | ||
_context.UserLabVms.Find(vmId).Running = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The vm id here is the proxmox vmid so the .Find(vmId) will fail.
} | ||
else | ||
{ | ||
// TODO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ses sender is merged, so you should be able to continue with this.
Implemented service and job to check the status of vms every minute.