Пример #1
0
        private static void PerformCallback(APSQueuedJob job, JobProcessor processor)
        {
            m_Logger.Debug("Called PerformCallback()");

            if (String.IsNullOrEmpty(job.CallbackUrl))
            {
                AddLogEntry(job.QueuedJobId, "No callback URL specified, data not posted", LogEntryTarget.All);
            }
            else
            {
                string parsedMetadataXml = processor.MetadataXml.ToString().Replace("&", string.Empty);

                HttpPoster poster = new HttpPoster {
                    Url = job.CallbackUrl
                };
                poster.AddParameter("AssetId", processor.AssetId);
                poster.AddParameter("InputPath", processor.InputPath);
                poster.AddParameter("JobId", job.QueuedJobId);
                poster.AddParameter("MetadataXml", parsedMetadataXml);
                poster.AddParameter("AdditionalData", job.AdditionalData);
                poster.AddParameter("PluginRequested", processor.PluginUsed);
                poster.AddParameter("PluginUsed", processor.PluginUsed);
                poster.AddParameter("PreviewPath", processor.PreviewPath);
                poster.AddParameter("ThumbnailPath", processor.ThumbnailPath);
                poster.AddParameter("StatusId", job.QueuedJobStatusId);
                poster.AddParameter("Status", job.Status);
                poster.AddParameter("DateAdded", job.DateAdded);
                poster.AddParameter("DateProcessed", job.DateProcessed);

                foreach (var fdi in processor.FileDataItems)
                {
                    poster.AddParameter("FDI_" + fdi.Key, fdi.Value);
                }

                try
                {
                    string response = poster.Post();
                    AddLogEntry(job.QueuedJobId, string.Format("Posted data to callback url: {0}.  Response: {1}", poster.Url, response), LogEntryTarget.All);
                }
                catch (Exception ex)
                {
                    AddLogEntry(job.QueuedJobId, string.Format("Error posting data to callback url: {0}.  Error: {1}", poster.Url, ex.Message), LogEntryTarget.Database);

                    m_Logger.Error(string.Format("JobId: {0}, AssetId: {1} - Error posting data to callback url: {2}. Error: {3}", job.QueuedJobId, job.AssetId, poster.Url, ex.Message), ex);
                }
            }

            m_Logger.Debug("Exited PerformCallback()");
        }
Пример #2
0
        /// <summary>
        /// Adds a job to the processing queue
        /// </summary>
        /// <param name="job">The job to be added</param>
        /// <returns>Job ID</returns>
        public long AddJobToQueue(Job job)
        {
            // This method is not static as we want to
            // ensure that this class gets instantiated
            // so that the timer kicks off and processes jobs

            if (!JobIsValid(job))
            {
                return(0);
            }

            using (APSEntities db = new APSEntities(DBHelper.GetConnectionString()))
            {
                m_Logger.DebugFormat("Received job to add to queue.  Asset Id: {0}", job.AssetId);

                APSQueuedJob qj = new APSQueuedJob
                {
                    AssetId         = job.AssetId,
                    InputPath       = job.InputPath,
                    WatermarkPath   = job.WatermarkPath ?? string.Empty,
                    PluginName      = job.PluginName ?? string.Empty,
                    CreateThumbnail = job.CreateThumbnail,
                    CreatePreview   = job.CreatePreview,
                    OverrideWidth   = job.OverrideWidth,
                    OverrideHeight  = job.OverrideHeight,
                    DateAdded       = DateTime.Now,
                    DateProcessed   = null,
                    Status          = Status.NotStarted,
                    Message         = "Added",
                    CallbackUrl     = job.CallbackUrl ?? string.Empty,
                    AdditionalData  = job.AdditionalData
                };

                db.AddToAPSQueuedJob(qj);
                db.SaveChanges();

                string message = string.Format("Job for asset with id '{0}' added to queue with Id '{1}'", qj.AssetId, qj.QueuedJobId);
                AddLogEntry(qj.QueuedJobId, message, LogEntryTarget.All);

                return(qj.QueuedJobId);
            }
        }
Пример #3
0
        public void ProcessQueuedJobs()
        {
            Console.WriteLine("Processing queued jobs");

            if (NumberOfJobsInProgress >= MaxNumberOfConcurrentJobs)
            {
                Debug.Write(string.Format("Already processing maximum number of concurrent jobs available - in progress: {0}, max: {1}", NumberOfJobsInProgress, MaxNumberOfConcurrentJobs));
                Console.WriteLine("Already processing maximum number of concurrent jobs available");
                return;
            }

            try
            {
                using (APSEntities db = new APSEntities(DBHelper.GetConnectionString()))
                {
                    Console.WriteLine("Checking queue...");

                    ServiceStatus.Instance.QueueLastChecked = DateTime.Now;

                    while (GetPendingJobs(db).Count() > 0)
                    {
                        Console.WriteLine("Found jobs");

                        var jobs = GetPendingJobs(db);

                        m_Logger.DebugFormat("Found {0} pending jobs needing to be processed", jobs.Count());

                        APSQueuedJob job = jobs.FirstOrDefault();

                        if (job == null)
                        {
                            break;
                        }

                        AddLogEntry(job.QueuedJobId, string.Format("Processing job: {0}", job.QueuedJobId), LogEntryTarget.All);

                        try
                        {
                            NumberOfJobsInProgress++;

                            job.Message = "Processing...";
                            job.Status  = Status.Processing;
                            db.SaveChanges();

                            m_Logger.Debug("Sending to job processor");

                            JobProcessor processor = new JobProcessor
                            {
                                JobId           = job.QueuedJobId,
                                AssetId         = job.AssetId,
                                InputPath       = job.InputPath,
                                WatermarkPath   = job.WatermarkPath,
                                PluginName      = job.PluginName,
                                CreatePreview   = job.CreatePreview,
                                CreateThumbnail = job.CreateThumbnail,
                                OverrideHeight  = job.OverrideHeight,
                                OverrideWidth   = job.OverrideWidth
                            };
                            processor.Go();

                            AddLogEntry(job.QueuedJobId, "Done processing job", LogEntryTarget.All);

                            job.DateProcessed = DateTime.Now;

                            if (processor.HasErrors)
                            {
                                job.Message = "Completed with errors. Check log for details";
                                job.Status  = Status.CompletedWithErrors;
                            }
                            else
                            {
                                job.Message = "Processing completed successfully";
                                job.Status  = Status.CompletedSuccessfully;
                            }

                            PerformCallback(job, processor);

                            if (DeleteGeneratedFilesAfterCallback)
                            {
                                DeleteGeneratedFiles(processor);
                            }
                            else
                            {
                                m_Logger.Debug("Generated files not deleted as DeleteGeneratedFilesAfterCallback setting is false");
                            }
                        }
                        catch (Exception ex)
                        {
                            AddLogEntry(job.QueuedJobId, "Processing completed with errors: " + ex.Message, LogEntryTarget.Database);

                            m_Logger.Error("Error processing job: " + ex.Message, ex);

                            job.Message = "Processing completed with errors: " + ex.Message;
                            job.Status  = Status.CompletedWithErrors;
                        }
                        finally
                        {
                            NumberOfJobsInProgress--;
                            ServiceStatus.Instance.NumberOfJobsProcessed++;
                        }

                        db.SaveChanges();

                        Console.WriteLine("Finished processing job: {0}", job.QueuedJobId);
                    }

                    Console.WriteLine("No more pending jobs to be processed");
                }
            }
            catch (Exception ex)
            {
                m_Logger.Error(string.Format("ProcessQueuedJobs: Error processing queued jobs: {0}", ex.Message), ex);
            }

            Console.WriteLine("Number of other jobs currently in progress: {0}", NumberOfJobsInProgress);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("...");
            Console.WriteLine();
            Console.WriteLine();
        }