コード例 #1
0
        static void Main(string[] args)
        {
            // initialize core subsystems
            NReco.Logging.LogManager.Configure(new NReco.Log4Net.Logger());
            log4net.Config.XmlConfigurator.Configure();
            NReco.Converting.ConvertManager.Configure();
            var serviceConfig = (IComponentsConfig)ConfigurationSettings.GetConfig("components");

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);

            var runnerParams        = ExtractCmdParams(args, paramDescriptors);
            var threadsCount        = (int)runnerParams[ThreadCountParam];
            var iterationsCount     = (int)runnerParams[ThreadIterationCountParam];
            var iterationDelay      = (int)runnerParams[ThreadIterationDelayParam];
            var serviceName         = (string)runnerParams[ServiceParam];
            var serviceContextParam = (string)runnerParams[ContextParam];
            var threadTimeoutSec    = (int)runnerParams[ThreadTimeoutParam];
            var totalTimeoutSec     = (int)runnerParams[TimeoutParam];
            var debugMode           = (bool)runnerParams[DebugParam];

            if (debugMode)
            {
                System.Diagnostics.Debugger.Break();
            }

            // simple validation
            if (String.IsNullOrEmpty(serviceName))
            {
                log.Write(LogEvent.Fatal, "Service name is required parameter (-s [servicename])");
                return;
            }
            // log params
            log.Write(LogEvent.Info, "Parameters: service={0}, threads={1}, iterations={2}", serviceName, threadsCount, iterationsCount);


            RunnerThread[] threads = new RunnerThread[threadsCount];
            // create thead instances
            for (int threadIdx = 0; threadIdx < threadsCount; threadIdx++)
            {
                threads[threadIdx] = new RunnerThread(
                    serviceConfig, serviceName,
                    new RunnerContext(threadIdx, serviceContextParam),
                    iterationsCount, iterationDelay);
            }
            // start them!
            var startTime = DateTime.Now;

            foreach (var t in threads)
            {
                t.Start();
            }

            // wait/check timeout loop
            while (true)
            {
                // check threads
                bool allFinished = true;
                foreach (var t in threads)
                {
                    t.CheckTimeout(threadTimeoutSec);
                    if (t.IsAlive)
                    {
                        allFinished = false;
                    }
                }
                if (allFinished)
                {
                    log.Write(LogEvent.Info, "All threads are finished");
                    return;
                }
                // check total timeout
                if (totalTimeoutSec >= 0 && startTime.AddSeconds(totalTimeoutSec) < DateTime.Now)
                {
                    log.Write(LogEvent.Warn, "Runner timeout reached, stopping (duration={0})",
                              DateTime.Now.Subtract(startTime));
                    // aborting alive threads
                    foreach (var t in threads)
                    {
                        t.Abort();
                    }
                    return;
                }
                // wait sleep
                Thread.Sleep(100);
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: Mariamfelicia/nreco
        static void Main(string[] args)
        {
            // initialize core subsystems
            NReco.Logging.LogManager.Configure(new NReco.Log4Net.Logger());
            log4net.Config.XmlConfigurator.Configure();
            NReco.Converting.ConvertManager.Configure();
            var serviceConfig = (IComponentsConfig)ConfigurationSettings.GetConfig("components");

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);

            var runnerParams = ExtractCmdParams(args, paramDescriptors);
            var threadsCount = (int)runnerParams[ThreadCountParam];
            var iterationsCount = (int)runnerParams[ThreadIterationCountParam];
            var iterationDelay = (int)runnerParams[ThreadIterationDelayParam];
            var serviceName = (string)runnerParams[ServiceParam];
            var serviceContextParam = (string)runnerParams[ContextParam];
            var threadTimeoutSec = (int)runnerParams[ThreadTimeoutParam];
            var totalTimeoutSec = (int)runnerParams[TimeoutParam];
            var debugMode = (bool)runnerParams[DebugParam];

            if (debugMode)
                System.Diagnostics.Debugger.Break();

            // simple validation
            if (String.IsNullOrEmpty(serviceName)) {
                log.Write(LogEvent.Fatal, "Service name is required parameter (-s [servicename])");
                return;
            }
            // log params
            log.Write(LogEvent.Info, "Parameters: service={0}, threads={1}, iterations={2}", serviceName, threadsCount, iterationsCount);

            RunnerThread[] threads = new RunnerThread[threadsCount];
            // create thead instances
            for (int threadIdx = 0; threadIdx < threadsCount; threadIdx++) {
                threads[threadIdx] = new RunnerThread(
                    serviceConfig, serviceName,
                    new RunnerContext(threadIdx, serviceContextParam),
                    iterationsCount, iterationDelay);
            }
            // start them!
            var startTime = DateTime.Now;
            foreach (var t in threads)
                t.Start();

            // wait/check timeout loop
            while (true) {
                // check threads
                bool allFinished = true;
                foreach (var t in threads) {
                    t.CheckTimeout( threadTimeoutSec );
                    if (t.IsAlive)
                        allFinished = false;
                }
                if (allFinished) {
                    log.Write(LogEvent.Info, "All threads are finished");
                    return;
                }
                // check total timeout
                if (totalTimeoutSec>=0 && startTime.AddSeconds(totalTimeoutSec)<DateTime.Now ) {
                    log.Write(LogEvent.Warn, "Runner timeout reached, stopping (duration={0})",
                        DateTime.Now.Subtract(startTime) );
                    // aborting alive threads
                    foreach (var t in threads)
                        t.Abort();
                    return;
                }
                // wait sleep
                Thread.Sleep(100);
            }
        }