public void SendProblemReport(Pipe ReportPipe)
 {
     TimeStamp = DateTime.Now;
     var envelope = new PipeMessageEnvelope()
                         {
                             Body = this,
                             Label = MessageLabel,
                         };
     ReportPipe.Send(envelope);
 }
        public JobSection(WorkRequest workRequest, WorkerCard workerCard)
        {
            WorkRequest = workRequest;
            WorkerCard = workerCard;

            WorkerIDs = new List<string>();

            HiringPipe = new Pipe(workRequest.HiringPipeName);
            HiringPipe.Open();
        }
 public WorkerStatistics(WorkAssignment workAssignment, string machineName, Pipe reportPipe)
 {
     WorkerBadge = workAssignment.WorkerBadge;
     WorkerBorn = DateTime.Now;
     HeartBeatInterval = new TimeSpan(0, 0, 1);
     PreviousHeartBeatSentTime = WorkerBorn;
     CurrentHeartBeatSentTime = WorkerBorn;
     if (reportPipe == null) throw new ArgumentException("reportPipe is null");
     ReportPipe = reportPipe;
 }
        /// <summary>
        /// Reports the errors.
        /// </summary>
        /// <param name="logPipe"></param>
        /// <param name="errorDocumentDetails">The error document details.</param>
        /// <param name="pipelineId">The pipeline identifier.</param>
        /// <param name="workerId">The worker identifier.</param>
        /// <param name="workerRoleTypeId"></param>
        /// <returns></returns>
        internal static IEnumerable<JobWorkerLog<ProductionParserLogInfo>> SendProductionLogs(Pipe logPipe, IEnumerable<ProductionDocumentDetail> errorDocumentDetails, string pipelineId, string workerId, string workerRoleTypeId)
        {
            if(errorDocumentDetails==null) return null;
             var productionParserLogInfos = new List<JobWorkerLog<ProductionParserLogInfo>>();
            try
            {
               
                var jobRunId = Convert.ToInt64(pipelineId);
                foreach (var errorDocument in errorDocumentDetails)
                {


                    var parserLog = new JobWorkerLog<ProductionParserLogInfo>
                    {
                        JobRunId =jobRunId,
                        CorrelationId = errorDocument.CorrelationId,
                        WorkerRoleType = workerRoleTypeId,
                        WorkerInstanceId = workerId,
                        IsMessage = false,
                        Success = false,
                        CreatedBy = errorDocument.CreatedBy,
                        LogInfo =
                            new ProductionParserLogInfo
                            {
                                Information = errorDocument.ErrorMessage,
                                BatesNumber = errorDocument.AllBates,
                                DatasetName = errorDocument.OriginalDatasetName,
                                DCN = errorDocument.DCNNumber,
                                ProductionDocumentNumber = errorDocument.DocumentProductionNumber,
                                ProductionName = errorDocument.Profile.ProfileName
                            }
                    };
                  productionParserLogInfos.Add(parserLog);
                  if (logPipe == null) 
                      Trace.TraceError("log pipe is empty");
                  logPipe.Open();
                  var message = new PipeMessageEnvelope()
                  {
                      Body = productionParserLogInfos
                  };
                  logPipe.Send(message);
                }
              
            }
            catch (Exception exception)
            {
                var dcns = String.Join(",", errorDocumentDetails.Select(errorDoc => errorDoc.DCNNumber));
                exception.AddDbgMsg("Failed to send documents to Redact-It and failed in reporting errors");
                exception.AddDbgMsg(string.Format("Document Control Numbers:{0}", dcns));
                exception.Trace().Swallow();
            }
            return productionParserLogInfos;
        }
 /// <summary>
 /// Send message
 /// </summary>
 private void SendMessage()
 {
     // We get here if Completed() is called for the first time. 
     // Need to send special message to DeleteProjectCleanup worker.
     var pipelineSection = FindPipelineSection("IncludeSubSystemsFinal");
     var dataPipeName = pipelineSection.DataPipeName;
     using (var dataPipe = new Pipe(dataPipeName))
     {
         dataPipe.Open();
         var envelope = new PipeMessageEnvelope() { Label = "PleaseBuildProject" };
         dataPipe.Send(envelope);
     }
     _finalizationRequested = true;
 }
        public RoleSlotToken GetFreeSlot()
        {
            try
            {
                PipeMessageEnvelope envelope = HiringPipe.Receive(hiringPipeTimeout);
                if (envelope != null)
                {
                    object message = envelope.Body;
                    RoleSlotToken roleSlot = message as RoleSlotToken;
                    Debug.Assert(null != roleSlot);
                    return roleSlot;
                }

                // Debugging
                //if (SectionName == "S1")
                //{
                //    throw new EVException().AddDbgMsg("Test");
                //}
            }
            catch (Exception ex)
            {
                MessageQueueException messageQueueException = ex as MessageQueueException;
                if (messageQueueException != null && (uint)messageQueueException.ErrorCode == 0x80004005)
                {
                    Tracer.Debug("Cannot find hiring pipe {0}.{1}, so skip processing it.", 
                        WorkRequest.HiringPipeName.SectionName, WorkRequest.PipelineId);
                }
                else
                {
                    ProblemReport problemReport = new ProblemReport(WorkRequest.SectionName, WorkerStage.Unknown, ex);
                    using (Pipe reportPipe = new Pipe(WorkRequest.ReportPipeName))
                    {
                        try
                        {
                            reportPipe.Open();
                            problemReport.SendProblemReport(reportPipe);
                        }
                        catch (Exception innerEx)
                        {
                            innerEx.Trace().Swallow();
                        }
                    }
                }
                throw;
            }
            return null;
        }
            public OutputSection(string name, DataPipeName dataPipeName)
            {
                Name = name;
                DataPipeName = dataPipeName;

                if (null != DataPipeName)
                {
                    DataPipe = new Pipe(DataPipeName);
                    DataPipe.Open();
                }
            }
        private void SetupLogPipe(DataPipeName logDataPipeName)
        {
            if (null == logDataPipeName)
            {
                LogPipe = null;
                return;
            }
            LogPipe = new Pipe(logDataPipeName);
            LogPipe.Open();

            LogPipe.Before = WorkerStatistics.PauseNetTime;
            LogPipe.After = WorkerStatistics.ResumeNetTime;
        }
        /// <summary>
        /// Setups the input data pipe.
        /// </summary>
        /// <param name="inputDataPipeName">Name of the input data pipe.</param>
        /// <remarks></remarks>
        private void SetupInputDataPipe(PipeName inputDataPipeName)
        {
            if (null == inputDataPipeName)
            {
                InputDataPipe = null;
                return;
            }
            InputDataPipe = new Pipe(inputDataPipeName);
            InputDataPipe.Open();

            InputDataPipe.Before = WorkerStatistics.PauseNetTime;
            InputDataPipe.After = WorkerStatistics.ResumeNetTime;
        }
        /// <summary>
        /// Inits this instance.
        /// </summary>
        /// <remarks></remarks>
        private void Init()
        {
            try
            {
                InputDataPipeReceiveTimeout = new TimeSpan(0, 0, 10);
                PauseSleep = new TimeSpan(0, 0, 0, 10);

                RoleType = new RoleType(Utils.RemoveSuffix(GetType().Name, "Worker"));

                Debug.Assert(WorkerId != null);
                PipelineType = WorkAssignment.WorkRequest.PipelineType;
                PipelineId = WorkAssignment.WorkRequest.PipelineId;
                BootParameters = WorkAssignment.WorkRequest.BootParameters;
                JobParameters = WorkAssignment.WorkRequest.JobParameters;

                ReportPipe = new Pipe(WorkAssignment.WorkRequest.ReportPipeName);
                ReportPipe.Open();

                WorkerStatistics = new WorkerStatistics(WorkAssignment, Environment.MachineName, ReportPipe);

                ReportPipe.Before = WorkerStatistics.PauseNetTime;
                ReportPipe.After = WorkerStatistics.ResumeNetTime;

                SetupInputDataPipe(WorkAssignment.WorkRequest.InputDataPipeName);
                SetupOutputSections(WorkAssignment.WorkRequest.OutputSections);
                SetupLogPipe(WorkAssignment.WorkRequest.LogDataPipeName);

            }
            catch (Exception ex)
            {
                Tracer.Fatal("Unable to initialize worker {0}. Exception: {1}", WorkerId, ex);
                throw;
            }
        }