public void TestInvokeStoredProcExeMessage()
        {
            var testSubject = new NoFuture.Hbm.InvokeStoredProcExeMessage()
            {
                StoredProcName = "dbo.TestProc",
                State          = InvokeStoredProcExeState.Complete
            };

            var testInput = testSubject.ToString();
            InvokeStoredProcExeMessage testOutput;

            Assert.IsTrue(InvokeStoredProcExeMessage.TryParse(testInput, out testOutput));

            Assert.AreEqual(testSubject.StoredProcName, testOutput.StoredProcName);
            Assert.AreEqual(testSubject.State, testOutput.State);
        }
Пример #2
0
        public static void Main(string[] args)
        {
            var p = new Program(args);
            try
            {
                //get and test the cmd line arg key\values
                p.StartConsole();

                if (p.PrintHelp()) return;

                p.ParseProgramArgs();

                using (var conn = new SqlConnection(p.ConnectionString))
                {
                    Console.WriteLine($@"{p.MyProcMetadata.ProcName}");
                    conn.Open();
                    using (var cmd = conn.CreateCommand())
                    {
                        using (var da = new SqlDataAdapter(cmd))
                        {
                            var ds = new DataSet(p.MyProcMetadata.ProcName);
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.Clear();

                            //can be controlled from app.config
                            cmd.CommandTimeout = ConfigTimeout;

                            //assigns random values to (hopefully) avoid timeouts
                            p.MyProcMetadata.AssignSpParams(cmd.Parameters);
                            cmd.CommandText = p.MyProcMetadata.ProcName;

                            var processMsg = new InvokeStoredProcExeMessage { StoredProcName = p.MyProcMetadata.ProcName };
                            try
                            {
                                da.Fill(ds);
                                if (ds.Tables.Count <= 0)
                                    processMsg.State = InvokeStoredProcExeState.NoDsReturned;
                                else if (ds.Tables.Count > 1)
                                    processMsg.State = InvokeStoredProcExeState.MultiTableDs;
                            }
                            catch (Exception ex)
                            {
                                if (ex.Message.StartsWith("Timeout expired."))
                                    processMsg.State = InvokeStoredProcExeState.TimedOut;
                                else if (ex.Message.StartsWith("Invalid ") || ex.Message.StartsWith("Incorrect syntax"))
                                {
                                    processMsg.State = InvokeStoredProcExeState.BadSyntax;
                                    Settings.WriteToStoredProcLog(ex, string.Format("bad syntax"));
                                }
                                else
                                {
                                    processMsg.State = InvokeStoredProcExeState.OtherError;
                                    Settings.WriteToStoredProcLog(ex, string.Format("invoke of da.Fill got this unexpected error."));
                                }
                            }

                            var xsdOutput = p.MyProcMetadata.XsdFilePath;
                            if (processMsg.State != InvokeStoredProcExeState.BadSyntax &&
                                processMsg.State != InvokeStoredProcExeState.TimedOut &&
                                processMsg.State != InvokeStoredProcExeState.OtherError &&
                                processMsg.State != InvokeStoredProcExeState.NoDsReturned)
                            {
                                //dump the returned dataset schema to file
                                ds.WriteXmlSchema(xsdOutput);
                            }
                            if(conn.State == ConnectionState.Open)
                                conn.Close();

                            if (!p.SendSocketMessages) return;

                            //once its on the disk then send a message to the manager
                            using (var com = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP))
                            {
                                Settings.WriteToStoredProcLog($"Sent from process '{processMsg}'");
                                com.Connect(new IPEndPoint(IPAddress.Loopback, InvokeStoredProcManager.SOCKET_COMM_PORT));
                                com.Send(Encoding.UTF8.GetBytes(processMsg.ToString()));
                                com.Close();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Settings.WriteToStoredProcLog(ex, "Error while executing NoFuture.Hbm.InvokeStoredProc.exe");
            }
            Thread.Sleep(NfConfig.ThreadSleepTime);//slight pause
        }
Пример #3
0
        public static void Main(string[] args)
        {
            var p = new Program(args);

            try
            {
                //get and test the cmd line arg key\values
                p.StartConsole();

                if (p.PrintHelp())
                {
                    return;
                }

                p.ParseProgramArgs();

                using (var conn = new SqlConnection(p.ConnectionString))
                {
                    Console.WriteLine($@"{p.MyProcMetadata.ProcName}");
                    conn.Open();
                    using (var cmd = conn.CreateCommand())
                    {
                        using (var da = new SqlDataAdapter(cmd))
                        {
                            var ds = new DataSet(p.MyProcMetadata.ProcName);
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Parameters.Clear();

                            //can be controlled from app.config
                            cmd.CommandTimeout = ConfigTimeout;

                            //assigns random values to (hopefully) avoid timeouts
                            p.MyProcMetadata.AssignSpParams(cmd.Parameters);
                            cmd.CommandText = p.MyProcMetadata.ProcName;

                            var processMsg = new InvokeStoredProcExeMessage {
                                StoredProcName = p.MyProcMetadata.ProcName
                            };
                            try
                            {
                                da.Fill(ds);
                                if (ds.Tables.Count <= 0)
                                {
                                    processMsg.State = InvokeStoredProcExeState.NoDsReturned;
                                }
                                else if (ds.Tables.Count > 1)
                                {
                                    processMsg.State = InvokeStoredProcExeState.MultiTableDs;
                                }
                            }
                            catch (Exception ex)
                            {
                                if (ex.Message.StartsWith("Timeout expired."))
                                {
                                    processMsg.State = InvokeStoredProcExeState.TimedOut;
                                }
                                else if (ex.Message.StartsWith("Invalid ") || ex.Message.StartsWith("Incorrect syntax"))
                                {
                                    processMsg.State = InvokeStoredProcExeState.BadSyntax;
                                    Settings.WriteToStoredProcLog(ex, string.Format("bad syntax"));
                                }
                                else
                                {
                                    processMsg.State = InvokeStoredProcExeState.OtherError;
                                    Settings.WriteToStoredProcLog(ex, string.Format("invoke of da.Fill got this unexpected error."));
                                }
                            }

                            var xsdOutput = p.MyProcMetadata.XsdFilePath;
                            if (processMsg.State != InvokeStoredProcExeState.BadSyntax &&
                                processMsg.State != InvokeStoredProcExeState.TimedOut &&
                                processMsg.State != InvokeStoredProcExeState.OtherError &&
                                processMsg.State != InvokeStoredProcExeState.NoDsReturned)
                            {
                                //dump the returned dataset schema to file
                                ds.WriteXmlSchema(xsdOutput);
                            }
                            if (conn.State == ConnectionState.Open)
                            {
                                conn.Close();
                            }

                            if (!p.SendSocketMessages)
                            {
                                return;
                            }

                            //once its on the disk then send a message to the manager
                            using (var com = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP))
                            {
                                Settings.WriteToStoredProcLog($"Sent from process '{processMsg}'");
                                com.Connect(new IPEndPoint(IPAddress.Loopback, InvokeStoredProcManager.DefaultPort));
                                com.Send(Encoding.UTF8.GetBytes(processMsg.ToString()));
                                com.Close();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Settings.WriteToStoredProcLog(ex, "Error while executing NoFuture.Hbm.InvokeStoredProc.exe");
            }
            Thread.Sleep(NfConfig.ThreadSleepTime);//slight pause
        }