Example #1
0
        public async Task <TaskResult> Execute(CancellationToken cancellationToken)
        {
            using (var taskClient = new TaskClient(taskProperties))
            {
                var taskResult = TaskResult.Failed;
                try
                {
                    // create timelinerecord if not provided
                    await CreateTaskTimelineRecordIfRequired(taskClient, cancellationToken).ConfigureAwait(false);

                    taskLogger = new TaskLogger(taskProperties, taskClient);

                    // report task started
                    await taskLogger.Log("Task started").ConfigureAwait(false);

                    await taskClient.ReportTaskStarted(taskProperties.TaskInstanceId, cancellationToken).ConfigureAwait(false);

                    await taskClient.ReportTaskProgress(taskProperties.TaskInstanceId, cancellationToken).ConfigureAwait(false);

                    // start client handler execute
                    var executeTask = taskExecutionHandler.ExecuteAsync(taskMessage, taskLogger, cancellationToken).ConfigureAwait(false);
                    taskResult = await executeTask;

                    // report task completed with status
                    await taskLogger.Log("Task completed").ConfigureAwait(false);

                    await taskClient.ReportTaskCompleted(taskProperties.TaskInstanceId, taskResult, cancellationToken).ConfigureAwait(false);

                    return(taskResult);
                }
                catch (Exception e)
                {
                    if (taskLogger != null)
                    {
                        await taskLogger.Log(e.ToString()).ConfigureAwait(false);
                    }

                    await taskClient.ReportTaskCompleted(taskProperties.TaskInstanceId, taskResult, cancellationToken).ConfigureAwait(false);

                    throw;
                }
                finally
                {
                    if (taskLogger != null)
                    {
                        await taskLogger.End().ConfigureAwait(false);
                    }
                }
            }
        }
Example #2
0
        private static async Task ExecuteUsingTimelineLogs(
            WebJobsExecutionContext executionContext,
            ILogger log,
            string imageProvenance,
            string policy,
            Guid checkSuiteId,
            TaskProperties taskProperties,
            IDictionary <string, string> variables)
        {
            using (var taskClient = new TaskClient(taskProperties))
            {
                var taskLogger = new TaskLogger(taskProperties, taskClient);
                try
                {
                    // create timelinerecord if not provided
                    await taskLogger.CreateTaskTimelineRecordIfRequired(taskClient, default(CancellationToken)).ConfigureAwait(false);

                    // report task started
                    string taskStartedLog = string.Format("Initializing evaluation. Execution id - {0}", executionContext.InvocationId);
                    CommonUtilities.LogInformation(taskStartedLog, log, taskLogger, variables, null);

                    string outputLog;
                    var    violations = CommonUtilities.ExecutePolicyCheck(
                        executionContext,
                        log,
                        imageProvenance,
                        policy,
                        taskLogger,
                        variables,
                        null,
                        out ViolationType violationType,
                        out outputLog);

                    bool succeeded = !(violations?.Any() == true);
                    CommonUtilities.LogInformation($"Policy check succeeded: {succeeded}", log, taskLogger, variables, null);

                    var telemetryProperties = new Dictionary <string, object>();
                    telemetryProperties.Add("projectId", taskProperties.ProjectId);
                    telemetryProperties.Add("jobId", taskProperties.JobId);
                    telemetryProperties.Add("checkSuiteId", checkSuiteId);
                    telemetryProperties.Add("result", succeeded ? "succeeded" : "failed");
                    telemetryProperties.Add("layer", "Azure function");
                    telemetryProperties.Add(ArtifactPolicyTelemetryReasonKey, $"Found violations in evaluation. Violation type: {violationType}");

                    await UpdateCheckSuiteResult(
                        taskProperties.PlanUrl,
                        taskProperties.AuthToken,
                        taskProperties.ProjectId,
                        checkSuiteId,
                        succeeded,
                        outputLog,
                        log,
                        taskLogger,
                        variables);

                    await CustomerIntelligenceClient.GetClient(taskProperties.PlanUrl, taskProperties.AuthToken)
                    .PublishArtifactPolicyEventAsync(telemetryProperties).ConfigureAwait(false);

                    return;
                }
                catch (Exception e)
                {
                    if (taskLogger != null)
                    {
                        await taskLogger.Log(e.ToString()).ConfigureAwait(false);
                    }

                    throw;
                }
                finally
                {
                    if (taskLogger != null)
                    {
                        await taskLogger.End().ConfigureAwait(false);
                    }
                }
            }
        }