public Task StopServiceAsync() { Logger.LogInformation("Stopping Service"); this.CurrentStatus = QueueProcessorStatus.Stopping; if (TimerWorker != null) { TimerWorker.Stop(); } //Stop Client if (TorrentEngine != null) { this.TorrentEngine.StopAllAsync(); } this.CurrentStatus = QueueProcessorStatus.Stopped; Logger.LogInformation("Service Stopped"); return(Task.CompletedTask); }
private async void OnTimedEvent(object source, ElapsedEventArgs e) { try { if (TorrentEngine == null) { throw new Exception("Torrent Engine has not been started yet"); } TimerWorker.Stop(); this.CurrentStatus = QueueProcessorStatus.Seeding; Logger.LogInformation($"Running AT: {DateTime.UtcNow} UTC"); using (var scope = ScopeFactory.CreateScope()) { var Db = scope.ServiceProvider.GetRequiredService <HostedRepository>(); var allQueues = Db.BitClientProcessorQueues.Where(x => x.ExecutionStatus == ExecutionStatus.Queued).ToList(); if (allQueues != null && allQueues.Count > 0) { foreach (var queue in allQueues) { //Mark as Seeding queue.ExecutionStatus = ExecutionStatus.Seeding; queue.LastUpdatedTimeUTC = DateTime.UtcNow; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); try { Logger.LogInformation($"Processing, Queue Tracking ID:{queue.TrackingId} AT: {DateTime.UtcNow} UTC"); #region ExecutionMain // Load a .torrent file into memory if (queue.TorrentFileBytes == null) { throw new Exception("Torrent Content in Bytes is Empty"); } Torrent torrent = await Torrent.LoadAsync(queue.TorrentFileBytes); // Set all the files to not download //foreach (TorrentFile file in torrent.Files) // file.Priority = Priority.High; ////Set First File Prioroty //torrent.Files[1].Priority = Priority.Highest; string subPath = $"\\{queue.UserId}\\{queue.TrackingId}".ToUpper(); string saveDirectory = GetTorrentDownloadPath() + subPath; UserTorrentManager manager = new UserTorrentManager(torrent, saveDirectory, new TorrentSettings()); //Asssign manager.UserId = queue.UserId; manager.TrackingId = queue.TrackingId; manager.AvailableDownloadPath = $"{this.options.TorrentDownloadPath}{subPath}"; await TorrentEngine.Register(manager); Db.UserTorrentManagers.Enqueue(manager); // Disable rarest first and randomised picking - only allow priority based picking (i.e. selective downloading) PiecePicker picker = new StandardPicker(); picker = new PriorityPicker(picker); await manager.ChangePickerAsync(picker); await this.TorrentEngine.StartAll(); #endregion queue.ExecutionFeedBack = "Torrent added to Queue Successfully"; queue.ExecutionStatus = ExecutionStatus.Processed; Logger.LogInformation($"Torrent added to Queue Successfully, Tracking Id :{queue.TrackingId} AT: {DateTime.UtcNow} UTC"); } catch (Exception ex) { queue.ErrorsCount++; if (queue.ErrorsCount >= 5) { queue.ExecutionStatus = ExecutionStatus.ErrorOccurred; queue.ExecutionFeedBack = ex.Message; } else { queue.ExecutionStatus = ExecutionStatus.Queued; queue.ExecutionFeedBack = $"Requeued Attempt ({queue.ErrorsCount})"; } Logger.LogError($"ERROR: {ex.Message}, Tracking Id :{queue.TrackingId} AT: {DateTime.UtcNow} UTC"); } //**************** DONE ********************* stopWatch.Stop(); queue.LastUpdatedTimeUTC = DateTime.UtcNow; queue.ExecutionInMiliseconds = stopWatch.ElapsedMilliseconds; } } //Exception Count Reset _ExceptionCounts = 0; } } catch (Exception ex) { _ExceptionCounts++; Logger.LogError($"Processing Exception: {ex.Message}"); } finally { ContinueTimer(); } }