Example #1
0
        public static async Task Main(string[] args)
        {
            if (args.Length < 2)
            {
                Usage();
                return;
            }

            string paths = args[0];

            int.TryParse(args[1], out int startPacketIndex);

            // turn on log4net
            log4net.Config.XmlConfigurator.Configure();

            // catch all errors and log them
            AppDomain.CurrentDomain.UnhandledException += (sender, eventArgs) => Utility.LogException(eventArgs.ExceptionObject as Exception);

            // ignore all certificate validation issues
            ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;

            // console encoding will now be unicode
            Console.OutputEncoding = System.Text.Encoding.UTF8;

            // database init
            DatabasePool.Initialise();

            // configure fetcho
            await SetupConfiguration(paths);

            // upgrade the database
            DatabaseUpgrader.Upgrade();

            // buffers to connect the seperate tasks together
            BufferBlock <IEnumerable <QueueItem> > prioritisationBuffer = CreateBufferBlock(DefaultBufferBlockLimit);
            // really beef this buffers max size up since it takes for ever accumulate so we dont want to lose any
            BufferBlock <IEnumerable <QueueItem> > fetchQueueBuffer = CreateBufferBlock(DefaultBufferBlockLimit * 1000);
            //BufferBlock<IEnumerable<QueueItem>> requeueBuffer = CreateBufferBlock(DefaultBufferBlockLimit);
            ITargetBlock <IEnumerable <QueueItem> > outboxWriter   = CreateOutboxWriter();
            BufferBlock <IWebResourceWriter>        dataWriterPool = CreateDataWriterPool();

            // fetcho!
            var readLinko = new ReadLinko(prioritisationBuffer, startPacketIndex);
            var queueo    = new Queueo(prioritisationBuffer, fetchQueueBuffer, outboxWriter); // DataflowBlock.NullTarget<IEnumerable<QueueItem>>()
            var fetcho    = new Fetcho(fetchQueueBuffer, DataflowBlock.NullTarget <IEnumerable <QueueItem> >(), dataWriterPool);
            var stato     = new Stato("stats.csv", fetcho, queueo, readLinko);
            var controlo  = new Controlo(prioritisationBuffer, fetchQueueBuffer, dataWriterPool, () =>
            {
                readLinko.Shutdown();
                queueo.Shutdown();
                fetcho.Shutdown();
                stato.Shutdown();
            });
            //var requeueWriter = new BufferBlockObjectFileWriter<IEnumerable<QueueItem>>(cfg.DataSourcePath, "requeue", requeueBuffer);
            //var rejectsWriter = new BufferBlockObjectFileWriter<IEnumerable<QueueItem>>(cfg.DataSourcePath, "rejects", new NullTarget);

            // execute
            var tasks = new List <Task>();

            tasks.Add(stato.Process());
            tasks.Add(fetcho.Process());
            await Task.Delay(1000);

            tasks.Add(queueo.Process());
            await Task.Delay(1000);

            tasks.Add(readLinko.Process());
            tasks.Add(controlo.Process());

            await Task.WhenAll(tasks.ToArray()).ConfigureAwait(false);

            CloseAllWriters(dataWriterPool);
            DatabasePool.DestroyAll();
        }