/// <summary> /// Adds a new message to the log /// </summary> /// <param name="jobId">Job ID</param> /// <param name="message">Message</param> public static void AddLogEntry(long jobId, string message) { using (APSEntities db = new APSEntities(DBHelper.GetConnectionString())) { APSLog log = new APSLog { JobId = jobId, Message = message, Date = DateTime.Now }; db.AddToAPSLog(log); db.SaveChanges(); } }
/// <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); } }
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(); }