示例#1
0
        }         // Execute

        // async
        public ActionMetaData Execute(ExecuteArguments oArgs)
        {
            ActionMetaData amd = null;

            try {
                if (oArgs == null)
                {
                    throw new Alert(Log, "No ExecuteArguments specified.");
                }

                SetLogThreadProperties(oArgs);
                Log.Debug("Executing " + oArgs.StrategyType + " started...");

                amd = NewAsync(
                    oArgs.StrategyType.ToString(),
                    comment: oArgs.StrategyArgumentsStr,
                    nCustomerID: oArgs.CustomerID,
                    nUserID: oArgs.UserID
                    );

                ConstructorInfo oCreator = oArgs.StrategyType.GetConstructors().FirstOrDefault(
                    ci => ci.GetParameters().Length == oArgs.StrategyArguments.Count
                    );

                if (oCreator == null)
                {
                    string msg = string.Format(
                        "Failed to find a constructor for {0} with {1} arguments.",
                        oArgs.StrategyType,
                        oArgs.StrategyArguments.Count
                        );

                    throw new Alert(Log, msg);
                }                 // if

                Log.Debug(oArgs.StrategyType + " constructor found, invoking...");

                amd.UnderlyingThread = new Thread(() => {
                    try {
                        SetLogThreadProperties(oArgs);
                        AStrategy oInstance = (AStrategy)oCreator.Invoke(oArgs.StrategyArguments.ToArray());

                        if (oInstance == null)
                        {
                            throw new NullReferenceException("Failed to create an instance of " + oArgs.StrategyType);
                        }

                        amd.Strategy = oInstance;

                        oInstance.Context.UserID     = oArgs.UserID;
                        oInstance.Context.CustomerID = oArgs.CustomerID;

                        if (oArgs.OnInit != null)
                        {
                            Log.Debug(oInstance.Name + " instance created, invoking an initialization action...");

                            oArgs.OnInit(oInstance, amd);

                            Log.Debug(oInstance.Name + " initialization action complete.");
                        }                         // if

                        Log.Debug(oInstance.Name + " instance is initialized, executing...");

                        oInstance.Execute();

                        Log.Debug("Executing " + oArgs.StrategyType + " complete.");

                        SaveActionStatus(amd, ActionStatus.Done);

                        if (oArgs.OnSuccess != null)
                        {
                            Log.Debug(oInstance.Name + " instance running complete, invoking an OnSuccess action...");

                            oArgs.OnSuccess(oInstance, amd);

                            Log.Debug(oInstance.Name + " OnSuccess action complete.");
                        }                         // if
                    } catch (Exception e) {
                        Log.Alert(e, "Exception during executing " + oArgs.StrategyType + " strategy.");

                        amd.Comment = e.Message;
                        SaveActionStatus(amd, ActionStatus.Failed);

                        if (oArgs.OnFail != null)
                        {
                            Log.Debug(oArgs.StrategyType + " instance running failed, invoking an OnFail action...");

                            try {
                                oArgs.OnFail(amd);
                                Log.Debug(oArgs.StrategyType + " OnFail action complete.");
                            } catch (Exception ie) {
                                Log.Alert(
                                    ie,
                                    "Exception during executing of OnFail handler of " + oArgs.StrategyType + " strategy."
                                    );
                            }             // try
                        }                 // if
                    }                     // try
                });

                SaveActionStatus(amd, ActionStatus.Launched);

                if (oArgs.OnLaunch != null)
                {
                    Log.Debug(oArgs.StrategyType + " instance is to be launched, invoking an OnLaunch action...");

                    oArgs.OnLaunch(amd);

                    Log.Debug(oArgs.StrategyType + " OnLaunch action complete.");
                }                 // if

                amd.UnderlyingThread.Start();

                Log.Debug(
                    "Executing {0} started on another thread [{1}].",
                    oArgs.StrategyType,
                    amd.UnderlyingThread.ManagedThreadId
                    );

                return(amd);
            } catch (Exception e) {
                if (amd != null)
                {
                    amd.Comment += " Exception caught: " + e.Message;
                    SaveActionStatus(amd, ActionStatus.Failed);
                }                 // if

                if (!(e is AException))
                {
                    string sStrategyType = oArgs == null ? "UNKNOWN" : oArgs.StrategyType.ToString();
                    Log.Alert(e, "Exception during executing " + sStrategyType + " strategy.");
                }                 // if

                if ((oArgs != null) && (oArgs.OnException != null))
                {
                    try {
                        oArgs.OnException(amd);
                    } catch (Exception ie) {
                        Log.Alert(
                            ie,
                            "Exception during executing of OnException handler of " + oArgs.StrategyType + " strategy."
                            );
                    }             // try
                }                 // if

                throw new FaultException(e.Message);
            }     // try
        }         // Execute
示例#2
0
        }         // ExecuteSync

        private ActionMetaData ExecuteSync <T>(out T oInstance, ExecuteArguments args) where T : AStrategy
        {
            ActionMetaData amd = null;

            try {
                string sStrategyType = typeof(T).ToString();

                if (args == null)
                {
                    throw new Alert(Log, "No strategy arguments specified for " + sStrategyType + ".");
                }

                args.StrategyType = typeof(T);                  // just to avoid question which type is used.

                SetLogThreadProperties(args);

                Log.Debug("Executing " + sStrategyType + " started in sync...");

                amd = NewSync(
                    sStrategyType,
                    comment: args.StrategyArgumentsStr,
                    nCustomerID: args.CustomerID,
                    nUserID: args.UserID
                    );

                ConstructorInfo oCreator = typeof(T).GetConstructors().FirstOrDefault(
                    ci => ci.GetParameters().Length == args.StrategyArguments.Count
                    );

                if (oCreator == null)
                {
                    string msg = string.Format(
                        "Failed to find a constructor for {0} with {1} arguments.",
                        sStrategyType,
                        args.StrategyArguments.Count
                        );

                    throw new Alert(Log, msg);
                }                 // if

                Log.Debug(sStrategyType + " constructor found, invoking...");

                oInstance = (T)oCreator.Invoke(args.StrategyArguments.ToArray());

                if (oInstance == null)
                {
                    throw new NullReferenceException("Failed to create an instance of " + sStrategyType);
                }

                amd.Strategy = oInstance;

                oInstance.Context.UserID     = args.UserID;
                oInstance.Context.CustomerID = args.CustomerID;

                if (args.OnInit != null)
                {
                    Log.Debug(oInstance.Name + " instance created, invoking an initialization action...");

                    args.OnInit(oInstance, amd);

                    Log.Debug(oInstance.Name + " initialization action complete.");
                }                 // if

                if (args.OnLaunch != null)
                {
                    Log.Debug(args.StrategyType + " instance is to be launched, invoking an OnLaunch action...");

                    args.OnLaunch(amd);

                    Log.Debug(args.StrategyType + " OnLaunch action complete.");
                }                 // if

                Log.Debug(oInstance.Name + " instance is initialized, executing...");

                oInstance.Execute();

                Log.Debug("Executing " + oInstance.Name + " complete in sync.");

                SaveActionStatus(amd, ActionStatus.Done);

                if (args.OnSuccess != null)
                {
                    Log.Debug(oInstance.Name + " instance running complete, invoking an OnSuccess action...");

                    args.OnSuccess(oInstance, amd);

                    Log.Debug(oInstance.Name + " OnSuccess action complete.");
                }                 // if

                return(amd);
            } catch (Exception e) {
                Exception mostInnerException = e;

                while (mostInnerException.InnerException != null)
                {
                    mostInnerException = mostInnerException.InnerException;
                }

                if (amd != null)
                {
                    amd.Comment += " Exception caught: " + mostInnerException.Message;
                    SaveActionStatus(amd, ActionStatus.Failed);
                }                 // if

                if (!(e is AException))
                {
                    Log.Alert(mostInnerException, "Exception during executing " + typeof(T) + " strategy.");
                }

                if ((args != null) && (args.OnException != null))
                {
                    Log.Debug(args.StrategyType + " instance running failed, invoking an OnException action...");

                    try {
                        args.OnException(amd);
                        Log.Debug(args.StrategyType + " OnException action complete.");
                    } catch (Exception ie) {
                        Log.Alert(
                            ie,
                            "Exception during executing of OnException handler of " + args.StrategyType + " strategy."
                            );
                    }             // try
                }                 // if

                throw new FaultException(mostInnerException.Message);
            }     // try
        }         // ExecuteSync