Beispiel #1
0
        static void Main(string[] args)
        {
            IProcess consoleProcess = new ConsoleProcess();

            //Starting a new process
            consoleProcess.Start();

            //Trying to start a already started process
            consoleProcess.Start();

            //Stopping the process
            consoleProcess.Stop();
        }
        public BulkElasticGroupedTitleIndex Post(BulkElasticGroupedTitleIndex t)
        {
            const int batchCount = 400;

            ConsoleProcess.Start(t.GetType());
            CreateMapping();
            var i = 0;

            foreach (var elasticTitle in t.ElasticGroupedTitles.Batch(batchCount))
            {
                i++;
                ConsoleProcess.Restart();
                var rootDescriptor = GenerateElasticDescriptor(elasticTitle);
                ElasticSearch.Client.Bulk(rootDescriptor);
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine(@"iteration for {0} took - {1} ms", i * batchCount, ConsoleProcess.ElapsedMilliseconds());
            }

            //Parallel.ForEach(t.ElasticGroupedTitles.Batch(batchCount), elasticTitle => {
            //    i++;
            //    ConsoleProcess.Restart();
            //    var rootDescriptor = GenerateElasticDescriptor(elasticTitle);
            //    ElasticSearch.Client.Bulk(rootDescriptor);
            //    if (i%1000 != 0) return;
            //    Console.WriteLine(Environment.NewLine);
            //    Console.WriteLine(@"iteration for {0} took - {1} ms", i * batchCount, ConsoleProcess.ElapsedMilliseconds());
            //});

            ConsoleProcess.End(t.GetType());

            return(t);
        }
Beispiel #3
0
 public BulkElasticMagazineIndex Post(BulkElasticMagazineIndex t)
 {
     ConsoleProcess.Start(t.GetType());
     CreateMapping();
     foreach (var elasticMagazine in t.ElasticMagazine.Batch(5000))
     {
         ConsoleProcess.Increment();
         ElasticSearch.BulkIndex(elasticMagazine.ToList());
     }
     return(t);
 }
Beispiel #4
0
 public BulkElasticOrderHistory Post(BulkElasticOrderHistory t)
 {
     ConsoleProcess.Start(t.GetType());
     CreateMapping();
     foreach (var elasticTitle in t.OrderHistories.Batch(5000))
     {
         ConsoleProcess.Increment();
         ElasticSearch.BulkIndex(elasticTitle.ToList());
     }
     return(t);
 }
        public BulkElasticOwnershipIndexCollection Post(BulkElasticOwnershipIndexCollection t)
        {
            int batchCount = 400;

            ConsoleProcess.Start(t.GetType());
            CreateMapping();
            Console.WriteLine(@"Ownership titles count = " + t.ElasticOwnershipCollections.Count);
            var i = 0;

            foreach (var elasticTitle in t.ElasticOwnershipCollections.Batch(batchCount))
            {
                i++;
                ConsoleProcess.Restart();
                var rootDescriptor = GenerateElasticDescriptor(elasticTitle);
                ElasticSearch.Client.Bulk(rootDescriptor);
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine(@"iteration for {0} took - {1} ms", i * batchCount, ConsoleProcess.ElapsedMilliseconds());
            }

            //Parallel.ForEach(t.ElasticOwnershipCollections.Batch(batchCount), elastictitle =>
            //{
            //    i++;
            //    ConsoleProcess.Restart();
            //    var rootDescriptor = GenerateElasticDescriptor(elastictitle);
            //    ElasticSearch.Client.Bulk(rootDescriptor);
            //    if (i % 1000 != 0) return;
            //    Console.WriteLine(Environment.NewLine);
            //    Console.WriteLine(@"iteration for {0} took - {1} ms", i * batchCount, ConsoleProcess.ElapsedMilliseconds());
            //});

            //foreach (var ownershipList in t.ElasticOwnershipCollections)
            //{
            //    ConsoleProcess.Increment();
            //    if (ownershipList.ElasticOwnership.Count > 0)
            //    {
            //        ElasticSearch.BulkIndex(ownershipList.ElasticOwnership, ownershipList.Isbn);
            //    }

            //    ConsoleProcess.ModOf5000();
            //}
            //ElasticSearch.Client.UpdateIndexSettings(us => us.RefreshInterval("1s"));
            ConsoleProcess.End(t.GetType());
            return(t);
        }
        public BulkElasticGroupedTitleIndex Post(BulkElasticGroupedTitleIndex t)
        {
            const int batchCount = 500;

            ConsoleProcess.Start(t.GetType());
            CreateMapping();
            var i = 0;

            foreach (var elasticTitle in t.ElasticGroupedTitles.Batch(batchCount))
            {
                i++;
                ConsoleProcess.Restart();
                ElasticSearch.BulkIndex(elasticTitle.ToList());
                Console.WriteLine(Environment.NewLine);
                Console.WriteLine(@"iteration for {0} took - {1} ms", i * batchCount, ConsoleProcess.ElapsedMilliseconds());
            }
            ConsoleProcess.End(t.GetType());
            return(t);
        }
Beispiel #7
0
        public static int RunAndLog(string appFileName, string arguments, Action <Level, string> log)
        {
            var proc = new ConsoleProcess
            {
                Arguments   = arguments,
                AppFileName = appFileName,
                Encoding    = Encoding.UTF8
            };

            proc.StderrLineRead += line => log(Level.Error, line);
            proc.StdoutLineRead += line => log(Level.Info, line);

            log(Level.Verbose, string.Format("cmdline: {0} {1}", appFileName, arguments));

            proc.Start();
            proc.Wait();

            return(proc.ExitCode);
        }
        public BulkProfileOwnership Post(BulkProfileOwnership t)
        {
            var collection = new MongoClient(new MongoUrl("")).GetDatabase("").GetCollection <BsonDocument>("etlownershipdata");
            var builder    = Builders <BsonDocument> .Filter;
            var filter     = builder.Eq("Year", DateTime.Now.Year) & builder.Eq("Month", DateTime.Now.Month) &
                             builder.Eq("Day", DateTime.Now.Day - 1);

            if (collection.CountAsync(filter).Result == 0)
            {
                var groupedOwnership = t.ProfileOwnerships.GroupBy(g => g.ScopeId);
                var dateTime         = t.ProfileOwnerships.First().CreatedAt;
                var ownershipList    = groupedOwnership.Select(item => new BsonDocument()
                {
                    { "ScopeId", item.Key },
                    {
                        "Data", new BsonArray(item.Select(i => new BsonDocument()
                        {
                            { "Isbn", i.Isbn },
                            { "HoldsCopies", i.HoldsCopies },
                            { "TotalCopies", i.TotalCopies },
                            { "CirculationCopies", i.CirculationCopies },
                        }))
                    },
                    { "Day", dateTime.Day },
                    { "Month", dateTime.Month },
                    { "Year", dateTime.Year }
                }).ToList();
                ConsoleProcess.Start(t.GetType());
                foreach (var ownership in ownershipList)
                {
                    collection.InsertOneAsync(ownership);
                }
                ConsoleProcess.End(t.GetType());
            }
            return(t);
        }
Beispiel #9
0
    }     //Main

    /// <summary>
    /// Invokes tar and builds the list of files in the archive which are not to be deleted.
    /// </summary>
    /// <param name="CurrentSettings"></param>
    public static void RunCommand(Settings CurrentSettings)
    {
        try
        {
            StringDictionary FileTable     = new StringDictionary();
            ArrayList        DeleteList    = new ArrayList();
            string           DeleteCommand = null;
            string           sep           = Path.DirectorySeparatorChar.ToString();

            StreamWriter OutFile = new StreamWriter(CurrentSettings.DeletionScriptPath +
                                                    Path.DirectorySeparatorChar +
                                                    m_DeleteScriptName);

            switch (CurrentSettings.OSType)
            {
            case OSTypeEnum.Windows:
            {
                DeleteCommand = "@del /f ";
                break;
            }     //case

            case OSTypeEnum.Unix:
            {
                OutFile.WriteLine("#!/bin/sh");
                DeleteCommand = "rm -f -v ";
                break;
            }     //case

            default:
            {
                throw new System.InvalidOperationException("Invalid OSTypeEnum value.");
            }     //case
            } //switch

            string fullCommand = m_CommandArgument + CurrentSettings.TarFileName;
            // Check to see that tar is in path.

            Console.WriteLine();
            // tar fails on the Mac.  Try gnutar first.
            if (ToolInstalled("gnutar"))
            {
                m_CommandName = "gnutar";
                Console.WriteLine("Found utility named: {0}", m_CommandName);
            } //if
            else
            {
                if (ToolInstalled("tar"))
                {
                    m_CommandName = "tar";
                    Console.WriteLine("Found utility named: {0}", m_CommandName);
                    Console.WriteLine("Tar utility may truncate file names on Mac OS X.");
                }    //if
                else //No tar installed.
                {
                    Console.WriteLine("No tar utility found. Exiting...");
                    System.InvalidOperationException ioe = new System.InvalidOperationException("No tar utility found.");
                    throw ioe;
                } //else
            }

            ConsoleProcess toolProc = new ConsoleProcess(m_CommandName, fullCommand);
            Console.WriteLine(m_nl + "Starting command {0} {1}", m_CommandName, fullCommand);
            toolProc.Start();

            // Wait for all IO to complete.
            toolProc.WaitForOutput();

            // Get standard output and error (if any).
            string toolStdOut   = toolProc.StandardOutputString;
            string toolStdError = toolProc.StandardErrorString;

            // If there is output to stdErr or a bad command exit code output warning.
            if (toolStdError != null || toolProc.BaseProcess.ExitCode != 0)
            {
                Console.WriteLine(m_nl +
                                  "*************************** Tool Error ***************************");
                Console.WriteLine(m_nl +
                                  "Exit code: {0}", toolProc.BaseProcess.ExitCode);
                Console.WriteLine(m_nl +
                                  "Error in tool operation: {0}", toolStdError);
                System.Environment.ExitCode = toolProc.BaseProcess.ExitCode;
                return;
            } //if

            if (toolStdOut == null || toolStdOut.Length < 1)
            {
                Console.WriteLine(m_nl + "No file list generated, exiting");
                System.Environment.ExitCode = 1;
                return;
            } //if

            Console.WriteLine(m_nl + "Finished {0} {1}, searching for files to delete ...",
                              m_CommandName,
                              m_CommandArgument);

            StringReader outputList = new StringReader(toolStdOut);
            string       fname      = null;
            string       line       = null;

            while (outputList.Peek() > -1)
            {
                line = outputList.ReadLine();

                // Tar always outputs using forward slashes as the separator char.
                if (CurrentSettings.OSType == OSTypeEnum.Windows)
                {
                    fname = CurrentSettings.SSCLIRootDirectory + sep + line.Replace("/", sep);
                } //if
                else
                {
                    fname = CurrentSettings.SSCLIRootDirectory + sep + line;
                } //else

                if (!Directory.Exists(fname)) // filter out directory names
                {
                    // There is a rare case where the table already contains the name.
                    if (!FileTable.ContainsKey(fname.ToLower()))
                    {
                        FileTable.Add(fname.ToLower(), fname.ToLower());
                    } //if
                }     //if
            }         //while

            CreateDeletionFile(new DirectoryInfo(CurrentSettings.SSCLIRootDirectory),
                               OutFile,
                               FileTable,
                               DeleteList,
                               DeleteCommand,
                               CurrentSettings);
            OutFile.Flush();
            OutFile.Close();

            // Make script executable on Unix
            if (CurrentSettings.OSType == OSTypeEnum.Unix)
            {
                System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();
                si.FileName  = "chmod";
                si.Arguments = "+x " +
                               CurrentSettings.DeletionScriptPath +
                               Path.DirectorySeparatorChar +
                               m_DeleteScriptName;
                si.UseShellExecute = false;
                System.Diagnostics.Process chmodproc = System.Diagnostics.Process.Start(si);
                chmodproc.WaitForExit();
            } //if

            Console.WriteLine(m_nl +
                              "*********************************************************");
            Console.WriteLine("Deletion script file created at: {0}",
                              CurrentSettings.DeletionScriptPath +
                              Path.DirectorySeparatorChar +
                              m_DeleteScriptName);
        } //try
        catch (Exception e)
        {
            Console.WriteLine("Exception in GenerateFile: {0}", e.ToString());
        } //catch
    }     //GenerateFile()
        public void Start()
        {
            switch (LoggingSourceType)
            {
            case LoggingTypeSourceEnum.Console:
                if (ConsoleProcess != null)
                {
                    if (ConsoleProcess.HasExited)
                    {
                        ConsoleProcess.Start();
                    }

                    return;
                }
                else
                {
                    if (File.Exists(FilePath))
                    {
                        ConsoleOutputStream = new ObservableCollection <OutputText>();

                        var startInfo = new ProcessStartInfo(FilePath)
                        {
                            WorkingDirectory       = RootFolderPath,
                            Arguments              = StartingArguments,
                            RedirectStandardOutput = true,
                            UseShellExecute        = false,
                            CreateNoWindow         = true
                        };
                        ConsoleProcess = new System.Diagnostics.Process {
                            StartInfo = startInfo
                        };

                        ConsoleProcess.EnableRaisingEvents = true;

                        var setts = MeasureSettings.Where(x => x.IsReadyToUse && x.IsActive);

                        ConsoleProcess.OutputDataReceived += (sender, args) =>
                        {
                            if (!_isAppClosing && args.Data != null)
                            {
                                App.Current.Dispatcher.Invoke((System.Action) delegate
                                {
                                    var rowData = args.Data.Trim();

                                    CreateMeasureUnit(rowData, DateTime.Now);
                                    PublishMessageToConsole(rowData);
                                });
                            }
                        };
                        ConsoleProcess.Exited += (sender, args) =>
                        {
                            InProgress = false;
                        };

                        ConsoleProcess.Start();
                        ConsoleProcess.BeginOutputReadLine();
                    }
                    else
                    {
                        Console.WriteLine("File not found.");
                        InProgress = false;
                    }
                }

                _calculationCancellationTokenSource = new CancellationTokenSource();
                _calculationWorker = RepeatHelper.Interval(TimeSpan.FromSeconds(LoopingDelay), () => EvaluateUnitData(), _calculationCancellationTokenSource.Token);

                break;

            case LoggingTypeSourceEnum.RabbitMQ:

                var con = ConfigurationManager.AppSettings.Get(Constants.RabbitMQConnectionName);

                if (con == null)
                {
                    con = @"host=localhost;port=5672;virtualHost=/;username=admin;password=admin;requestedHeartbeat=0";
                }

                var processMQId = $"{FileName}_{Guid.NewGuid()}";
                bus = RabbitHutch.CreateBus(con);
                var exchange = bus.Advanced.ExchangeDeclare("Ariane.MQ", ExchangeType.Topic, autoDelete: true);

                // stops disposes bus and kills queue
                //queue = bus.Advanced.QueueDeclare(processMQId, autoDelete: true, perQueueMessageTtl: 10000);
                queue = bus.Advanced.QueueDeclare(processMQId, autoDelete: true);
                var q = bus.Advanced.Bind(exchange, queue, RabbitMQTopicName);

                bus.Advanced.Consume <LogMessageBase>(queue, (x, a) => {
                    var log = x.Body;

                    string rowData;
                    var m = $"{log.TimeStamp.ToString("HH:mm:ss")} -{log.Level}- {log.FormattedMessage}";

                    // apply aditional formatting
                    if (!String.IsNullOrEmpty(log.Message))
                    {
                        rowData = String.Format(log.Message, m);
                    }
                    else
                    {
                        rowData = m;
                    }

                    var timeStamp = log.TimeStamp;

                    CreateMeasureUnit(rowData, timeStamp);
                    Application.Current.Dispatcher.Invoke(delegate
                    {
                        PublishMessageToConsole(rowData);
                    });

                    //}, x => {
                    //x.WithTopic(RabbitMQTopicName);
                });

                InProgress = true;

                break;

            default:
                throw new Exception($"Logging source '{LoggingSourceType}' has not supported.");
            }
        }
Beispiel #11
0
    } //Main

    /// <summary>
    /// Invokes tar and builds the list of files in the archive which are not to be deleted.
    /// </summary>
    /// <param name="CurrentSettings"></param>
    public static void RunCommand(Settings CurrentSettings)
    {
        try
        {
            StringDictionary FileTable = new StringDictionary();
            ArrayList DeleteList = new ArrayList();
            string DeleteCommand = null;
            string sep = Path.DirectorySeparatorChar.ToString();

            StreamWriter OutFile = new StreamWriter(CurrentSettings.DeletionScriptPath + 
                Path.DirectorySeparatorChar + 
                m_DeleteScriptName);

            switch (CurrentSettings.OSType)
            {
                case OSTypeEnum.Windows:
                {
                    DeleteCommand = "@del /f ";
                    break;
                } //case
                case OSTypeEnum.Unix:
                {
                    OutFile.WriteLine("#!/bin/sh");
                    DeleteCommand = "rm -f -v ";
                    break;
                } //case
                default:
                {
                    throw new System.InvalidOperationException("Invalid OSTypeEnum value.");
                } //case
            } //switch

            string fullCommand = m_CommandArgument + CurrentSettings.TarFileName;
            // Check to see that tar is in path.

            Console.WriteLine();
            // tar fails on the Mac.  Try gnutar first.
            if (ToolInstalled("gnutar"))
            {
                m_CommandName = "gnutar";
                Console.WriteLine("Found utility named: {0}", m_CommandName);
            } //if
            else
            {
                if (ToolInstalled("tar"))
                {
                    m_CommandName = "tar";
                    Console.WriteLine("Found utility named: {0}", m_CommandName);
                    Console.WriteLine("Tar utility may truncate file names on Mac OS X.");
                } //if
                else  //No tar installed.
                {
                    Console.WriteLine("No tar utility found. Exiting...");
                    System.InvalidOperationException ioe = new System.InvalidOperationException("No tar utility found.");
                    throw ioe;
                } //else
            }

            ConsoleProcess toolProc = new ConsoleProcess(m_CommandName, fullCommand);
            Console.WriteLine(m_nl + "Starting command {0} {1}", m_CommandName, fullCommand);
            toolProc.Start();

            // Wait for all IO to complete.
            toolProc.WaitForOutput();

            // Get standard output and error (if any).
            string toolStdOut = toolProc.StandardOutputString;
            string toolStdError = toolProc.StandardErrorString;
    
            // If there is output to stdErr or a bad command exit code output warning.
            if (toolStdError != null || toolProc.BaseProcess.ExitCode != 0)
            {
                Console.WriteLine(m_nl + 
                    "*************************** Tool Error ***************************");
                Console.WriteLine(m_nl +
                    "Exit code: {0}", toolProc.BaseProcess.ExitCode);
                Console.WriteLine(m_nl + 
                    "Error in tool operation: {0}", toolStdError);
                System.Environment.ExitCode = toolProc.BaseProcess.ExitCode;
                return;
            } //if
    
            if (toolStdOut == null || toolStdOut.Length < 1)
            {
                Console.WriteLine(m_nl + "No file list generated, exiting");
                System.Environment.ExitCode = 1;
                return;
            } //if

            Console.WriteLine(m_nl + "Finished {0} {1}, searching for files to delete ...", 
                m_CommandName, 
                m_CommandArgument);
		
            StringReader outputList = new StringReader(toolStdOut);
            string fname = null;
            string line = null;
      
            while (outputList.Peek() > -1)
            {
                line = outputList.ReadLine();

                // Tar always outputs using forward slashes as the separator char.
                if (CurrentSettings.OSType == OSTypeEnum.Windows)
                {
                    fname = CurrentSettings.SSCLIRootDirectory + sep + line.Replace("/", sep);
                } //if
                else
                {
                    fname = CurrentSettings.SSCLIRootDirectory + sep + line;
                } //else

                if (!Directory.Exists(fname)) // filter out directory names
                {
                    // There is a rare case where the table already contains the name.
                    if (!FileTable.ContainsKey(fname.ToLower()))
                    {
                        FileTable.Add(fname.ToLower(), fname.ToLower());
                    } //if
                } //if
            } //while
    
            CreateDeletionFile(new DirectoryInfo(CurrentSettings.SSCLIRootDirectory), 
                OutFile, 
                FileTable, 
                DeleteList, 
                DeleteCommand, 
                CurrentSettings);
            OutFile.Flush();
            OutFile.Close();

            // Make script executable on Unix
            if (CurrentSettings.OSType == OSTypeEnum.Unix)
            {
                System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();
                si.FileName = "chmod";
                si.Arguments = "+x " + 
                    CurrentSettings.DeletionScriptPath + 
                    Path.DirectorySeparatorChar + 
                    m_DeleteScriptName;
                si.UseShellExecute = false;
                System.Diagnostics.Process chmodproc= System.Diagnostics.Process.Start(si);
                chmodproc.WaitForExit();
            } //if

            Console.WriteLine(m_nl +
                "*********************************************************");
            Console.WriteLine("Deletion script file created at: {0}", 
                CurrentSettings.DeletionScriptPath +
                Path.DirectorySeparatorChar +
                m_DeleteScriptName);
        } //try
        catch (Exception e)
        {
            Console.WriteLine("Exception in GenerateFile: {0}", e.ToString());
        } //catch
    } //GenerateFile()
Beispiel #12
0
        void Main_Loaded(object sender, RoutedEventArgs e)
        {
            var traceListener = new TextTraceListener();
            System.Diagnostics.Trace.Listeners.Add(traceListener);

            traceListener.TextOut.Subscribe(s => Dispatcher.Invoke(() =>
                {
                    UiTraceOut.Text += s;
                    ScrollParent(UiTraceOut);
                }));

            Browser.PropertyChanged += (o, args) =>
                {
                    if (args.PropertyName == "SelectedScriptPath")
                    {
                        Dispatcher.Invoke(() =>
                        {
                            var path = Browser.SelectedScriptPath;
                            Title = path;

                            if (_console != null)
                            {
                                _console.Close();
                            }

                            var procStartInfo = new ProcessStartInfo("cmd");
                            var app = (App)Application.Current;
                            procStartInfo.WorkingDirectory = app.AppSettings.AdbDirectory;
                            _console = new ConsoleProcess(procStartInfo);
                            _console.Start();

                            var script = File.ReadAllLines(path);
                            var api = new Api(_console, script);
                            var output = new Subject<string>();
                            var errors = new Subject<string>();

                            api.Console.OnOutputReceived(output.OnNext);
                            api.Console.OnErrorReceived(errors.OnNext);

                            output.Merge(errors).Subscribe(str => System.Diagnostics.Trace.Write(str));

                            UiScriptCommander.Api = api;
                        });
                    }
                };

            var adbDirectory = _app.AppSettings.AdbDirectory;
            if (string.IsNullOrWhiteSpace(adbDirectory))
            {
                SetAdbPath();
            }
        }