Exemple #1
0
        public static IDisposable Subscribe <T>(
            this IObservable <T> observable,
            Action <T> onNext,
            System.Action onCompleted,
            ILoggable loggable)
        {
            Ensure.That(observable, nameof(observable)).IsNotNull();
            Ensure.That(onNext, nameof(onNext)).IsNotNull();

            void OnNextLogError(T value)
            {
                try
                {
                    onNext(value);
                }
                catch (Exception e)
                {
                    loggable.LogError(e, "Exception occurred on onNext.");
                }
            }

            void OnCompletedLogError()
            {
                try
                {
                    onCompleted();
                }
                catch (Exception e)
                {
                    loggable.LogError(e, "Exception occurred on onCompleted.");
                }
            }

            void OnError(Exception e)
            {
                loggable.LogError(e, "Subscription terminated with an error.");
            }

            return(observable.Subscribe(OnNextLogError, OnError, OnCompletedLogError));
        }
Exemple #2
0
        static ExitStatus Run(BinaryReader sourceReader, BinaryWriter destinationWriter, IPackerEngine packer,
                              PackerMode packerMode, int blockLength, int parallelismDegree, CancellationTokenSource token, ILoggable logger)
        {
            int       blocksNumber = (int)Math.Ceiling((double)sourceReader.BaseStream.Length / blockLength);
            Stopwatch watcher      = new Stopwatch();

            watcher.Start();
            logger?.LogMessage("Parallel Packer started:");
            logger?.LogMessage($"    {packerMode.ToString().ToLower()}ing...");
            try {
                if (packerMode == PackerMode.Unpack)  // we should save blockLength into the packed file because of blockLength parameter can be shanged
                {
                    (blocksNumber, blockLength) = BinaryBlockReader.ReadBlockInfo(sourceReader);
                }

                var commonSourceConveyer      = new LockableConveyer <Block>();
                var commonDestinationConveyer = new LockableConveyer <Block>();

                var workers = new List <IWorkable> {
                    WorkerFactory.CreateSourceWorker(sourceReader, blocksNumber, blockLength, packerMode, commonSourceConveyer, logger),
                    WorkerFactory.CreateDestinationWorker(destinationWriter, blocksNumber, blockLength, packerMode, commonDestinationConveyer, logger)
                };
                for (int index = 1; index <= parallelismDegree; ++index)
                {
                    workers.Add(WorkerFactory.CreatePackerWorker(index, packerMode, packer, commonSourceConveyer, commonDestinationConveyer, logger));
                }

                WorkerFactory.DoWork(workers, token);

                watcher.Stop();
                if (token.IsCancellationRequested)
                {
                    logger?.LogMessage($"{packerMode}ing has been cancelled by user '{Environment.UserName}' after {watcher.Elapsed}");
                    return(ExitStatus.CANCEL);
                }
                else
                {
                    logger?.LogMessage($"{packerMode}ing has been finished successfully in {watcher.Elapsed}:");
                    logger?.LogMessage($"    total blocks number: {blocksNumber}");
                    logger?.LogMessage($"    raw block length: {blockLength}");
                    return(ExitStatus.SUCCESS);
                }
            } catch (Exception e) {
                watcher.Stop();
                logger?.LogError($"{packerMode}ing finished with ERRORS in {watcher.Elapsed}:", e);
                return(ExitStatus.ERROR);
            }
        }