diff options
Diffstat (limited to 'src/marlin.agents.shell/JobUnitOfWork.cs')
| -rw-r--r-- | src/marlin.agents.shell/JobUnitOfWork.cs | 55 |
1 files changed, 18 insertions, 37 deletions
diff --git a/src/marlin.agents.shell/JobUnitOfWork.cs b/src/marlin.agents.shell/JobUnitOfWork.cs index 85df806..5d639c0 100644 --- a/src/marlin.agents.shell/JobUnitOfWork.cs +++ b/src/marlin.agents.shell/JobUnitOfWork.cs @@ -12,38 +12,27 @@ using ProtoBuf.Grpc.Client; public class JobUnitOfWork { - private static string _jobInstanceDir = Path.Combine(CommonPaths.AgentRoot, "instance"); + private readonly IInstanceService _instanceService; + private readonly Channel<LogEventRequest> _logQueue; - private readonly MarlinApiClient _apiClient; - private readonly IGitClient _gitClient; - private readonly DispatchJob _job; - private readonly string _checkoutPath; + protected readonly string _workingDir; + protected readonly string _recipe; - private GrpcChannel _channel; - private IInstanceService _instanceService; - - private Channel<LogEventRequest> _logQueue; - - public JobUnitOfWork(DispatchJob job, MarlinApiClient apiClient, IGitClient gitClient) + public JobUnitOfWork(IInstanceService instanceService, string workingDir, string recipe) { - this._job = job; - this._apiClient = apiClient; - this._gitClient = gitClient; + this._instanceService = instanceService; + this._logQueue = Channel.CreateUnbounded<LogEventRequest>(); - this._checkoutPath = Path.Combine(_jobInstanceDir, job.Id.ToString("N")); + this._workingDir = workingDir; + this._recipe = recipe; } public async Task Run() { - this._channel = this._apiClient.CreateJobScopedRpc(this._job); - this._instanceService = this._channel.CreateGrpcService<IInstanceService>(); - this._logQueue = Channel.CreateUnbounded<LogEventRequest>(); - var drainTask = Task.Run(this.DrainLogEvents); await StatusUpdate(JobStatus.Active); - await this.CheckoutSource(); var plan = await this.BuildPlan(); var success = await this.ExecutePlan(plan); @@ -53,28 +42,13 @@ public class JobUnitOfWork await drainTask; } - private async Task CheckoutSource() - { - Directory.CreateDirectory(_jobInstanceDir); - if (Path.Exists(this._checkoutPath)) - Directory.Delete(this._checkoutPath, true); - - await this._gitClient.Checkout(this._checkoutPath, this._job.Vcs, this._job.Ref, this._job.Commit); - } - - private async Task DrainLogEvents() - { - await foreach (var evt in this._logQueue.Reader.ReadAllAsync()) - await this._instanceService.SendLogEvent(evt); - } - private async Task<ExecutionPlan> BuildPlan() { WorkflowBuilder builder = new(); - builder.LoadFromDirectory(Path.Join(this._checkoutPath, ".marlin")); + builder.LoadFromDirectory(Path.Join(this._workingDir, ".marlin")); Workflow workflow = builder.Build(); - Recipe recipe = workflow.Recipes[this._job.Recipe]; + Recipe recipe = workflow.Recipes[this._recipe]; ExecutionPlan plan = ExecutionPlan.Create(workflow, recipe); return plan; @@ -101,6 +75,7 @@ public class JobUnitOfWork Console.WriteLine(component); Task<int> run = CommandRunner.RunAsync( component.Command, + this._workingDir, o => Log(o, false, component), o => Log(o, true, component)); @@ -111,6 +86,12 @@ public class JobUnitOfWork return results.All(x => x.Value == CommandResult.Success); } + private async Task DrainLogEvents() + { + await foreach (var evt in this._logQueue.Reader.ReadAllAsync()) + await this._instanceService.SendLogEvent(evt); + } + private void Log(string message, bool error, ExecutionCommand component) { this._logQueue.Writer.TryWrite( |
