/// <summary>
        /// Complete processing the current media item by updating the media queue instance and
        /// reseting the status of the conversion queue.
        /// </summary>
        /// <param name="settings">An instance of <see cref="MediaConversionSettings" /> containing
        /// settings and results used in the conversion. A null value is acceptable.</param>
        private void CompleteProcessItem(MediaConversionSettings settings)
        {
            // Update status and persist to data store
            MediaQueueItem mqItem = GetCurrentMediaQueueItem();

            mqItem.DateConversionCompleted = DateTime.Now;

            if (settings != null && settings.FileCreated)
            {
                mqItem.Status = MediaQueueItemStatus.Complete;
            }
            else
            {
                mqItem.Status = MediaQueueItemStatus.Error;

                var    fileName = (settings != null && !String.IsNullOrEmpty(settings.FilePathSource) ? Path.GetFileName(settings.FilePathSource) : "<Unknown>");
                string msg      = String.Format(CultureInfo.CurrentCulture, "Unable to process file '{0}'.", fileName);
                RecordEvent(msg, settings);
            }

            //Factory.GetDataProvider().MediaQueue_Save(mediaQueueDto);
            mqItem.Save();

            // Update the item in the collection.
            //MediaQueueItems[mediaQueueDto.MediaQueueId] = mediaQueueDto;

            Reset();
        }
        /// <summary>
        /// Begins processing the current media item, returning <c>true</c> when the action succeeds.
        /// Specifically, a few properties are updated and the item is persisted to the data store.
        /// If the item cannot be processed (may be null or has a status other than 'Waiting'), this
        /// function returns <c>false</c>.
        /// </summary>
        /// <returns>Returns <c>true</c> when the item has successfully started processing; otherwise
        /// <c>false</c>.</returns>
        private bool BeginProcessItem()
        {
            MediaQueueItem mqItem = GetCurrentMediaQueueItem();

            if (mqItem == null)
            {
                return(false);
            }

            if (!mqItem.Status.Equals(MediaQueueItemStatus.Waiting))
            {
                ProcessNextItemInQueue(false);
                return(false);
            }

            mqItem.Status = MediaQueueItemStatus.Processing;
            mqItem.DateConversionStarted = DateTime.Now;

            //Factory.GetDataProvider().MediaQueue_Save(mediaQueueDto);
            mqItem.Save();

            // Update the item in the collection.
            //MediaQueueItems[mediaQueueDto.MediaQueueId] = mediaQueueDto;

            return(true);
        }
        /// <summary>
        /// Removes the item from the queue. If the item is currently being processed, the task
        /// is cancelled.
        /// </summary>
        /// <param name="mediaQueueId">The media queue ID.</param>
        public void RemoveMediaQueueItem(int mediaQueueId)
        {
            MediaQueueItem item;

            if (MediaQueueItemDictionary.TryGetValue(mediaQueueId, out item))
            {
                MediaQueueItem currentItem = GetCurrentMediaQueueItem();
                if ((currentItem != null) && (currentItem.MediaQueueId == mediaQueueId))
                {
                    CancelTokenSource.Cancel();

                    if (Task != null)
                    {
                        Task.Wait(20000);                         // Wait up to 20 seconds
                    }

                    Instance.Status = MediaQueueStatus.Idle;
                }

                //Factory.GetDataProvider().MediaQueue_Delete(item);
                item.Delete();

                MediaQueueItemDictionary.TryRemove(mediaQueueId, out item);
            }
        }
        /// <summary>
        /// Processes the next item in the queue. If the instance is already processing items, the
        /// action is canceled.
        /// </summary>
        private void ProcessNextItemInQueue(bool useBackgroundThread)
        {
            if (Status == MediaQueueStatus.Processing)
            {
                return;
            }

            Reset();

            MediaQueueItem mqItem = GetNextItemInQueue();

            if (mqItem == null)
            {
                return;
            }

            // We have an item to process.
            Status = MediaQueueStatus.Processing;
            _currentMediaQueueItemId = mqItem.MediaQueueId;

            CancelTokenSource = new CancellationTokenSource();

            if (useBackgroundThread)
            {
                Task = Task.Factory.StartNew(ProcessItem);
            }
            else
            {
                ProcessItem();
            }
        }
        /// <summary>
        /// Determines whether the specified media object undergoing the specified <paramref name="conversionType" />
        /// is currently being processed by the media queue or is waiting in the queue.
        /// </summary>
        /// <param name="mediaObjectId">The ID of the media object.</param>
        /// <param name="conversionType">Type of the conversion. If the parameter is omitted, then a matching
        /// media object having any conversion type will cause the method to return <c>true</c>.</param>
        /// <returns>Returns <c>true</c> if the media object is currently being processed by the media queue
        /// or is waiting in the queue; otherwise, <c>false</c>.</returns>
        public bool IsWaitingInQueueOrProcessing(int mediaObjectId, MediaQueueItemConversionType conversionType = MediaQueueItemConversionType.Unknown)
        {
            MediaQueueItem item = GetCurrentMediaQueueItem();

            if ((item != null) && item.MediaObjectId == mediaObjectId && (item.ConversionType == conversionType || conversionType == MediaQueueItemConversionType.Unknown))
            {
                return(true);
            }
            else
            {
                return(IsWaitingInQueue(mediaObjectId, conversionType));
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MediaConversionQueue"/> class.
        /// </summary>
        private MediaConversionQueue()
        {
            //MediaQueueItemDictionary = new ConcurrentDictionary<int, MediaQueueDto>(Factory.GetDataProvider().MediaQueue_GetMediaQueues().ToDictionary(m => m.MediaQueueId));
            using (var repo = new MediaQueueRepository())
            {
                var items = MediaQueueItem.ToMediaQueueItems(repo.GetAll().OrderBy(mq => mq.DateAdded));

                MediaQueueItemDictionary = new ConcurrentDictionary <int, MediaQueueItem>(items.ToDictionary(m => m.MediaQueueId));
            }

            Reset();

            Status = MediaQueueStatus.Idle;
        }
Пример #7
0
 /// <summary>
 /// Converts the <paramref name="item" /> to an instance of <see cref="MediaQueueDto" />.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <returns>An instance of <see cref="MediaQueueDto" />.</returns>
 private static MediaQueueDto ToMediaQueueDto(MediaQueueItem item)
 {
     return(new MediaQueueDto
     {
         MediaQueueId = item.MediaQueueId,
         FKMediaObjectId = item.MediaObjectId,
         Status = item.Status.ToString(),
         StatusDetail = item.StatusDetail,
         ConversionType = item.ConversionType,
         RotationAmount = item.RotationAmount,
         DateAdded = item.DateAdded,
         DateConversionStarted = item.DateConversionStarted,
         DateConversionCompleted = item.DateConversionCompleted
     });
 }
        /// <summary>
        /// Adds the specified <paramref name="mediaObject" /> to the queue. It will be processed in a first-in, first-out
        /// order. If the media object is already waiting in the queue, no action is taken.
        /// </summary>
        /// <param name="mediaObject">The media object to be processed.</param>
        /// <param name="conversionType">Type of the conversion.</param>
        public void Add(IGalleryObject mediaObject, MediaQueueItemConversionType conversionType)
        {
            lock (_sharedLock)
            {
                var mqItem = new MediaQueueItem
                {
                    MediaQueueId            = int.MinValue,
                    MediaObjectId           = mediaObject.Id,
                    Status                  = MediaQueueItemStatus.Waiting,
                    ConversionType          = conversionType,
                    RotationAmount          = mediaObject.CalculateNeededRotation(),
                    StatusDetail            = String.Empty,
                    DateAdded               = DateTime.Now,
                    DateConversionStarted   = null,
                    DateConversionCompleted = null
                };

                mqItem.Save();
                //Factory.GetDataProvider().MediaQueue_Save(mediaQueueDto);

                MediaQueueItemDictionary.TryAdd(mqItem.MediaQueueId, mqItem);
            }
        }
Пример #9
0
 /// <summary>
 /// Converts the <paramref name="item" /> to an instance of <see cref="MediaQueueDto" />.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <returns>An instance of <see cref="MediaQueueDto" />.</returns>
 private static MediaQueueDto ToMediaQueueDto(MediaQueueItem item)
 {
     return new MediaQueueDto
                {
                    MediaQueueId = item.MediaQueueId,
                    FKMediaObjectId = item.MediaObjectId,
                    Status = item.Status.ToString(),
                    StatusDetail = item.StatusDetail,
                          ConversionType = item.ConversionType,
                          RotationAmount = item.RotationAmount,
                    DateAdded = item.DateAdded,
                    DateConversionStarted = item.DateConversionStarted,
                    DateConversionCompleted = item.DateConversionCompleted
                };
 }
 /// <summary>
 /// Update the status of the <paramref name="item" /> to the specified <paramref name="status" />.
 /// </summary>
 /// <param name="item">The item whose status is to be updated.</param>
 /// <param name="status">The status to update the item to.</param>
 private static void ChangeStatus(MediaQueueItem item, MediaQueueItemStatus status)
 {
     item.Status = status;
     item.Save();
     //Factory.GetDataProvider().MediaQueue_Save(item);
 }
Пример #11
0
 /// <summary>
 /// Update the status of the <paramref name="item" /> to the specified <paramref name="status" />.
 /// </summary>
 /// <param name="item">The item whose status is to be updated.</param>
 /// <param name="status">The status to update the item to.</param>
 private static void ChangeStatus(MediaQueueItem item, MediaQueueItemStatus status)
 {
     item.Status = status;
     item.Save();
     //Factory.GetDataProvider().MediaQueue_Save(item);
 }
Пример #12
0
        /// <summary>
        /// Adds the specified <paramref name="mediaObject" /> to the queue. It will be processed in a first-in, first-out
        /// order. If the media object is already waiting in the queue, no action is taken.
        /// </summary>
        /// <param name="mediaObject">The media object to be processed.</param>
        /// <param name="conversionType">Type of the conversion.</param>
        public void Add(IGalleryObject mediaObject, MediaQueueItemConversionType conversionType)
        {
            lock (_sharedLock)
            {
                var mqItem = new MediaQueueItem
                                                                                {
                                                                                    MediaQueueId = int.MinValue,
                                                                                    MediaObjectId = mediaObject.Id,
                                                                                    Status = MediaQueueItemStatus.Waiting,
                                                                                    ConversionType = conversionType,
                                                                                    RotationAmount = mediaObject.CalculateNeededRotation(),
                                                                                    StatusDetail = String.Empty,
                                                                                    DateAdded = DateTime.Now,
                                                                                    DateConversionStarted = null,
                                                                                    DateConversionCompleted = null
                                                                                };

                mqItem.Save();
                //Factory.GetDataProvider().MediaQueue_Save(mediaQueueDto);

                MediaQueueItemDictionary.TryAdd(mqItem.MediaQueueId, mqItem);
            }
        }