Пример #1
0
        private async Task JobFiber(CancellationToken cancellationToken)
        {
            try {
                var headers = new Metadata {
                    { ProtocolConstants.AgentKeyHeaderName, apiKey }
                };
                using var stream = buildServer.acceptBuildJob(headers, null, cancellationToken);


                var buildTask = await ReadBuildTask(stream, cancellationToken);

                if (buildTask == null)
                {
                    return;
                }


                await using var buildDir = DirectoryCleanup.CreateTempDir(workspacesDir);
                logger.LogInformation("Created workspace for build: {0}, exists: {1}", buildDir.Value, Directory.Exists(buildDir.Value));
                var buildRunner = new BuildJobRunner(logger, buildDir.Value, stream.ResponseStream, stream.RequestStream, cancellationToken);
                await buildRunner.RunJob(buildTask);

                await ProcessArtifacts(stream, buildRunner, cancellationToken);
            }
            catch (OperationCanceledException) { }
            catch (Exception ex) {
                logger.LogError(ex, "Error running build job");
                await Task.Delay(1000, cancellationToken);
            }
            finally {
                using (await jobLock.EnterAsync(cancellationToken)) {
                    --runningJobs;
                    jobLock.Pulse();
                }
            }
        }
Пример #2
0
        private async Task ProcessArtifacts(AsyncDuplexStreamingCall <BuildStatusUpdate, RunBuildCommand> stream, BuildJobRunner buildRunner, CancellationToken cancellationToken)
        {
            while (await stream.ResponseStream.MoveNext(cancellationToken))
            {
                var command = stream.ResponseStream.Current;
                switch (command.PayloadCase)
                {
                case RunBuildCommand.PayloadOneofCase.Artifact:
                    var artifact = command.Artifact;
                    switch (artifact.PayloadCase)
                    {
                    case ArtifactRequest.PayloadOneofCase.Replay:
                        await buildRunner.SendReplay();

                        break;

                    case ArtifactRequest.PayloadOneofCase.Artifact:
                        await buildRunner.SendArtifact(artifact.Artifact);

                        break;

                    default:
                        throw new Exception("Unknown artifact type.");
                    }
                    break;

                default:
                    throw new Exception("Unexpected build request command");
                }
            }
        }