public void Serialize_queued_job_id_with_tenant_id()
 {
     QueuedJob job = new QueuedJob();
     job.TenantId = new TenantId("TEST_TENANT");
     job.Id = new QueuedJobId("some id");
     var json = job.ToJson();
     Assert.That(json, Text.Contains("some id"));
     Assert.That(json, Text.Contains("test_tenant"));
 }
 public async Task<HttpResponseMessage> Compose(
     TenantId tenantId,
     Model.ComposeDocumentsModel dto)
 {
     QueuedJob job = new QueuedJob();
     var id = new QueuedJobId(Guid.NewGuid().ToString());
     job.Id = id;
     job.SchedulingTimestamp = DateTime.Now;
     job.StreamId = 0;
     job.TenantId = tenantId;
     job.Parameters = new Dictionary<string, string>();
     job.Parameters.Add("documentList", String.Join<Object>("|", dto.DocumentList));
     job.Parameters.Add("resultingDocumentHandle", dto.ResultingDocumentHandle);
     job.Parameters.Add("resultingDocumentFileName", dto.ResultingDocumentFileName ?? dto.ResultingDocumentHandle);
     job.Parameters.Add(JobKeys.TenantId, tenantId);
     _queueDispatcher.QueueJob("pdfComposer", job);
     return Request.CreateResponse(
         HttpStatusCode.OK, 
         new  { result = "ok"}
     );
 }
 /// <summary>
 /// Set job execution status.
 /// </summary>
 /// <param name="job"></param>
 /// <param name="errorMessage">if this parameter is not empty or null the job result
 /// will be set to failed.</param>
 private void SetJobExecutionStatus(QueuedJob job, String errorMessage)
 {
     if (!String.IsNullOrEmpty(errorMessage))
     {
         job.ErrorCount++;
         job.ExecutionError = errorMessage;
         if (job.ErrorCount >= _info.MaxNumberOfFailure)
         {
             job.Status = QueuedJobExecutionStatus.Failed;
         }
         else
         {
             job.Status = QueuedJobExecutionStatus.ReQueued;
         }
         job.SchedulingTimestamp = DateTime.Now; //this will move failing job to the end of the queue.
     }
     else
     {
         job.Status = QueuedJobExecutionStatus.Succeeded;
     }
 }
Esempio n. 4
0
 public bool QueueJob(String queueName, QueuedJob job)
 {
     return((Boolean)ExecuteWithQueueHandler("set job executed", queueName, qh => qh.QueueJob(job)));
 }
        /// <summary>
        /// Handle a <see cref="StreamReadModel" /> and generates job for the queue
        /// if needed.
        /// </summary>
        /// <param name="streamElement"></param>
        /// <param name="tenantId"></param>
        /// <param name="forceReSchedule"></param>
        public QueuedJobId Handle(
            StreamReadModel streamElement,
            TenantId tenantId,
            Boolean forceReSchedule = false)
        {
            if (_info.ShouldCreateJob(streamElement))
            {
                if (!forceReSchedule)
                {
                    //look for already existing job with the same blobid, there is no need to re-queue again
                    //because if a job with the same blobid was already fired for this queue there is no need
                    //to re-issue
                    var existing = _collection.Find(
                        Builders <QueuedJob> .Filter.And(
                            Builders <QueuedJob> .Filter.Eq(j => j.BlobId, streamElement.FormatInfo.BlobId),
                            Builders <QueuedJob> .Filter.Eq(j => j.TenantId, tenantId)
                            )
                        ).Count() > 0;
                    if (existing)
                    {
                        return(null);
                    }
                }
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info($"Queue {_info.Name} CREATE JOB to process {streamElement.Describe()}");
                }

                QueuedJob job = new QueuedJob();
                job.Id = new QueuedJobId(Guid.NewGuid().ToString());
                job.SchedulingTimestamp  = DateTime.Now;
                job.StreamId             = streamElement.Id;
                job.TenantId             = tenantId;
                job.DocumentDescriptorId = streamElement.DocumentDescriptorId;
                job.BlobId     = streamElement.FormatInfo.BlobId;
                job.Handle     = new DocumentHandle(streamElement.Handle);
                job.Parameters = new Dictionary <string, string>();
                job.Parameters.Add(JobKeys.FileExtension, streamElement.Filename.Extension);
                job.Parameters.Add(JobKeys.Format, streamElement.FormatInfo.DocumentFormat);
                job.Parameters.Add(JobKeys.FileName, streamElement.Filename);
                job.Parameters.Add(JobKeys.TenantId, tenantId);
                job.Parameters.Add(JobKeys.MimeType, MimeTypes.GetMimeType(streamElement.Filename));
                job.Parameters.Add(JobKeys.PipelineId, streamElement.FormatInfo?.PipelineId?.ToString());
                if (forceReSchedule)
                {
                    job.Parameters.Add(JobKeys.Force, "true");
                }
                job.HandleCustomData = streamElement.DocumentCustomData;
                if (_info.Parameters != null)
                {
                    foreach (var parameter in _info.Parameters)
                    {
                        job.Parameters.Add(parameter.Key, parameter.Value);
                    }
                }

                _collection.InsertOne(job);

                return(job.Id);
            }
            else
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug($"Queue {_info.Name} do not need to process {streamElement.Describe()}");
                }
            }
            return(null);
        }
 /// <summary>
 /// This function add a job from external code. Usually jobs are automatically
 /// created after an handle is loaded in document store or after a new
 /// format is present, but some queue, as PdfComposer, creates job only from
 /// manual input.
 /// </summary>
 /// <param name="job"></param>
 public Boolean QueueJob(QueuedJob job)
 {
     _collection.InsertOne(job);
     return(true);
 }
        /// <summary>
        /// Handle a <see cref="StreamReadModel" /> and generates job for the queue
        /// if needed.
        /// </summary>
        /// <param name="streamElement"></param>
        /// <param name="tenantId"></param>
        /// <param name="forceReSchedule"></param>
        public void Handle(
            StreamReadModel streamElement, 
            TenantId tenantId,
            Boolean forceReSchedule = false)
        {
            if (_info.ShouldCreateJob(streamElement)) 
            {
                if (!forceReSchedule)
                {
                    //look for already existing job with the same blobid, there is no need to re-queue again
                    //because if a job with the same blobid was already fired for this queue there is no need
                    //to re-issue
                    var existing = _collection.Find(
                        Builders< QueuedJob>.Filter.And(
                            Builders< QueuedJob>.Filter.Eq(j => j.BlobId, streamElement.FormatInfo.BlobId),
                            Builders<QueuedJob>.Filter.Eq(j => j.TenantId, tenantId)
                        )
                    ).Count() > 0;
                    if (existing) return;
                }
                if (Logger.IsDebugEnabled) Logger.DebugFormat("Create queue for readmodel stream id {0} and queue {1}", streamElement.Id, _info.Name);
                QueuedJob job = new QueuedJob();
                var id = new QueuedJobId(Guid.NewGuid().ToString());
                job.Id = id;
                job.SchedulingTimestamp = DateTime.Now;
                job.StreamId = streamElement.Id;
                job.TenantId = tenantId;
                job.DocumentDescriptorId = streamElement.DocumentDescriptorId;
                job.BlobId = streamElement.FormatInfo.BlobId;
                job.Handle = new DocumentHandle( streamElement.Handle);
                job.Parameters = new Dictionary<string, string>();
                job.Parameters.Add(JobKeys.FileExtension, streamElement.Filename.Extension);
                job.Parameters.Add(JobKeys.Format, streamElement.FormatInfo.DocumentFormat);
                job.Parameters.Add(JobKeys.FileName, streamElement.Filename);
                job.Parameters.Add(JobKeys.TenantId, tenantId);
                job.Parameters.Add(JobKeys.MimeType, MimeTypes.GetMimeType(streamElement.Filename));
                job.HandleCustomData = streamElement.DocumentCustomData;
                if (_info.Parameters != null) 
                {
                    foreach (var parameter in _info.Parameters)
                    {
                        job.Parameters.Add(parameter.Key, parameter.Value);
                    }
                }

                _collection.InsertOne(job);
            }
        }
 private void SetErrorInfoToJob(QueuedJob job, String errorMessage)
 {
     if (!String.IsNullOrEmpty(errorMessage))
     {
         job.ErrorCount += 1;
         job.ExecutionError = errorMessage;
         if (job.ErrorCount >= _info.MaxNumberOfFailure)
         {
             job.Status = QueuedJobExecutionStatus.Failed;
         }
         else
         {
             job.Status = QueuedJobExecutionStatus.ReQueued;
         }
         job.SchedulingTimestamp = DateTime.Now; //this will move failing job to the end of the queue.
     }
     else
     {
         job.Status = QueuedJobExecutionStatus.Succeeded;
     }
 }
 /// <summary>
 /// This function add a job from external code. Usually jobs are automatically
 /// created after an handle is loaded in document store or after a new 
 /// format is present, but some queue, as PdfComposer, creates job only from
 /// manual input.
 /// </summary>
 /// <param name="job"></param>
 public Boolean QueueJob(QueuedJob job)
 {
     _collection.InsertOne(job);
     return true;
 }