public async Task EnsureJobItems(SyncJob job, User user) { var items = (await GetItemsForSync(job.Category, job.ParentId, job.RequestedItemIds, user, job.UnwatchedOnly).ConfigureAwait(false)) .ToList(); var jobItems = _syncManager.GetJobItems(new SyncJobItemQuery { JobId = job.Id, AddMetadata = false }).Items.ToList(); foreach (var item in items) { // Respect ItemLimit, if set if (job.ItemLimit.HasValue) { if (jobItems.Count(j => j.Status != SyncJobItemStatus.RemovedFromDevice && j.Status != SyncJobItemStatus.Failed) >= job.ItemLimit.Value) { break; } } var itemId = item.Id.ToString("N"); var jobItem = jobItems.FirstOrDefault(i => string.Equals(i.ItemId, itemId, StringComparison.OrdinalIgnoreCase)); if (jobItem != null) { continue; } var index = jobItems.Count == 0 ? 0 : jobItems.Select(i => i.JobItemIndex).Max() + 1; jobItem = new SyncJobItem { Id = Guid.NewGuid().ToString("N"), ItemId = itemId, ItemName = GetSyncJobItemName(item), JobId = job.Id, TargetId = job.TargetId, DateCreated = DateTime.UtcNow, JobItemIndex = index }; await _syncRepo.Create(jobItem).ConfigureAwait(false); _syncManager.OnSyncJobItemCreated(jobItem); jobItems.Add(jobItem); } jobItems = jobItems .OrderBy(i => i.DateCreated) .ToList(); await UpdateJobStatus(job, jobItems).ConfigureAwait(false); }
public async Task EnsureJobItems(SyncJob job) { var user = _userManager.GetUserById(job.UserId); if (user == null) { throw new InvalidOperationException("Cannot proceed with sync because user no longer exists."); } var items = (await GetItemsForSync(job.Category, job.ParentId, job.RequestedItemIds, user, job.UnwatchedOnly).ConfigureAwait(false)) .ToList(); var jobItems = _syncRepo.GetJobItems(new SyncJobItemQuery { JobId = job.Id }).Items.ToList(); foreach (var item in items) { // Respect ItemLimit, if set if (job.ItemLimit.HasValue) { if (jobItems.Count(j => j.Status != SyncJobItemStatus.RemovedFromDevice && j.Status != SyncJobItemStatus.Failed) >= job.ItemLimit.Value) { break; } } var itemId = item.Id.ToString("N"); var jobItem = jobItems.FirstOrDefault(i => string.Equals(i.ItemId, itemId, StringComparison.OrdinalIgnoreCase)); if (jobItem != null) { continue; } jobItem = new SyncJobItem { Id = Guid.NewGuid().ToString("N"), ItemId = itemId, ItemName = GetSyncJobItemName(item), JobId = job.Id, TargetId = job.TargetId, DateCreated = DateTime.UtcNow }; await _syncRepo.Create(jobItem).ConfigureAwait(false); jobItems.Add(jobItem); } jobItems = jobItems .OrderBy(i => i.DateCreated) .ToList(); await UpdateJobStatus(job, jobItems).ConfigureAwait(false); }
public async Task <SyncJobCreationResult> CreateJob(SyncJobRequest request) { var items = GetItemsForSync(request.ItemIds).ToList(); if (items.Count == 1) { request.Name = GetDefaultName(items[0]); } if (string.IsNullOrWhiteSpace(request.Name)) { throw new ArgumentException("Please supply a name for the sync job."); } var target = GetSyncTargets(request.UserId) .First(i => string.Equals(request.TargetId, i.Id)); var jobId = Guid.NewGuid().ToString("N"); var job = new SyncJob { Id = jobId, Name = request.Name, TargetId = target.Id, UserId = request.UserId, UnwatchedOnly = request.UnwatchedOnly, Limit = request.Limit, LimitType = request.LimitType, RequestedItemIds = request.ItemIds, DateCreated = DateTime.UtcNow, DateLastModified = DateTime.UtcNow, ItemCount = 1 }; await _repo.Create(job).ConfigureAwait(false); return(new SyncJobCreationResult { Job = GetJob(jobId) }); }
public async Task <SyncJobCreationResult> CreateJob(SyncJobRequest request) { var processor = GetSyncJobProcessor(); var user = _userManager.GetUserById(request.UserId); var items = (await processor .GetItemsForSync(request.Category, request.ParentId, request.ItemIds, user, request.UnwatchedOnly).ConfigureAwait(false)) .ToList(); if (items.Any(i => !SupportsSync(i))) { throw new ArgumentException("Item does not support sync."); } if (string.IsNullOrWhiteSpace(request.Name)) { if (request.ItemIds.Count == 1) { request.Name = GetDefaultName(_libraryManager.GetItemById(request.ItemIds[0])); } } if (string.IsNullOrWhiteSpace(request.Name)) { request.Name = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(); } var target = GetSyncTargets(request.UserId) .FirstOrDefault(i => string.Equals(request.TargetId, i.Id)); if (target == null) { throw new ArgumentException("Sync target not found."); } var jobId = Guid.NewGuid().ToString("N"); if (string.IsNullOrWhiteSpace(request.Quality)) { request.Quality = GetQualityOptions(request.TargetId) .Where(i => i.IsDefault) .Select(i => i.Id) .FirstOrDefault(i => !string.IsNullOrWhiteSpace(i)); } var job = new SyncJob { Id = jobId, Name = request.Name, TargetId = target.Id, UserId = request.UserId, UnwatchedOnly = request.UnwatchedOnly, ItemLimit = request.ItemLimit, RequestedItemIds = request.ItemIds ?? new List <string>(), DateCreated = DateTime.UtcNow, DateLastModified = DateTime.UtcNow, SyncNewContent = request.SyncNewContent, ItemCount = items.Count, Category = request.Category, ParentId = request.ParentId, Quality = request.Quality, Profile = request.Profile, Bitrate = request.Bitrate }; if (!request.Category.HasValue && request.ItemIds != null) { var requestedItems = request.ItemIds .Select(_libraryManager.GetItemById) .Where(i => i != null); // It's just a static list if (!requestedItems.Any(i => i.IsFolder || i is IItemByName)) { job.SyncNewContent = false; } } await _repo.Create(job).ConfigureAwait(false); await processor.EnsureJobItems(job).ConfigureAwait(false); // If it already has a converting status then is must have been aborted during conversion var jobItemsResult = GetJobItems(new SyncJobItemQuery { Statuses = new[] { SyncJobItemStatus.Queued, SyncJobItemStatus.Converting }, JobId = jobId, AddMetadata = false }); await processor.SyncJobItems(jobItemsResult.Items, false, new Progress <double>(), CancellationToken.None) .ConfigureAwait(false); jobItemsResult = GetJobItems(new SyncJobItemQuery { Statuses = new[] { SyncJobItemStatus.Queued, SyncJobItemStatus.Converting }, JobId = jobId, AddMetadata = false }); var returnResult = new SyncJobCreationResult { Job = GetJob(jobId), JobItems = jobItemsResult.Items.ToList() }; if (SyncJobCreated != null) { EventHelper.FireEventIfNotNull(SyncJobCreated, this, new GenericEventArgs <SyncJobCreationResult> { Argument = returnResult }, _logger); } if (returnResult.JobItems.Any(i => i.Status == SyncJobItemStatus.Queued || i.Status == SyncJobItemStatus.Converting)) { _taskManager.QueueScheduledTask <SyncConvertScheduledTask>(); } return(returnResult); }
public static void CreatePlanet() { _planetRepo.Create(_planetFaker.Generate()); }
public static void CreateStar() { _starRepo.Create(_starFaker.Generate()); }
public static void CreateGalaxy() { _galaxyRepo.Create(_galaxyFaker.Generate()); }
public static void CreateUniverse() { _universeRepo.Create(_universeFaker.Generate()); }
public async Task <SyncJobCreationResult> CreateJob(SyncJobRequest request) { var processor = new SyncJobProcessor(_libraryManager, _repo, this, _logger, _userManager, _tvSeriesManager, _mediaEncoder()); var user = _userManager.GetUserById(request.UserId); var items = (await processor .GetItemsForSync(request.Category, request.ParentId, request.ItemIds, user, request.UnwatchedOnly).ConfigureAwait(false)) .ToList(); if (items.Any(i => !SupportsSync(i))) { throw new ArgumentException("Item does not support sync."); } if (string.IsNullOrWhiteSpace(request.Name)) { if (request.ItemIds.Count == 1) { request.Name = GetDefaultName(_libraryManager.GetItemById(request.ItemIds[0])); } } if (string.IsNullOrWhiteSpace(request.Name)) { throw new ArgumentException("Please supply a name for the sync job."); } var target = GetSyncTargets(request.UserId) .FirstOrDefault(i => string.Equals(request.TargetId, i.Id)); if (target == null) { throw new ArgumentException("Sync target not found."); } var jobId = Guid.NewGuid().ToString("N"); var job = new SyncJob { Id = jobId, Name = request.Name, TargetId = target.Id, UserId = request.UserId, UnwatchedOnly = request.UnwatchedOnly, ItemLimit = request.ItemLimit, RequestedItemIds = request.ItemIds ?? new List <string> { }, DateCreated = DateTime.UtcNow, DateLastModified = DateTime.UtcNow, SyncNewContent = request.SyncNewContent, ItemCount = items.Count, Quality = request.Quality, Category = request.Category, ParentId = request.ParentId }; // It's just a static list if (!items.Any(i => i.IsFolder || i is IItemByName)) { job.SyncNewContent = false; } await _repo.Create(job).ConfigureAwait(false); await processor.EnsureJobItems(job).ConfigureAwait(false); return(new SyncJobCreationResult { Job = GetJob(jobId) }); }