Example #1
0
        public void Start()
        {
            try {
                Type oStrategyType = TypeUtils.FindType(ActionName);

                if (oStrategyType == null)
                {
                    Log.Alert("Job.Start: could not start the job {0}: strategy not found by name '{1}'.", this, ActionName);
                }
                else
                {
                    var oArgs = new ExecuteArguments {
                        StrategyType = oStrategyType,
                        OnInit       = OnInit,
                        OnException  = OnException,
                        OnLaunch     = OnLaunch,
                        OnSuccess    = OnSuccess,
                        OnFail       = OnFail,
                    };

                    foreach (var oArg in m_oArguments)
                    {
                        oArgs.StrategyArguments.Add(oArg.UnderlyingType.CreateInstance(oArg.Value));
                    }

                    new EzServiceImplementation(m_oRuntimeData).Execute(oArgs);
                }                 // if
            }
            catch (Exception e) {
                Log.Alert(e, "Job.Start: failed to start the job {0}.", this);
            }     // try
        }         // Start
Example #2
0
        public ActionMetaData CalculateMedal(int underwriterId, int customerId, long?cashRequestID, long?nlCashRequestID)
        {
            CalculateMedal instance;

            var args = new ExecuteArguments(customerId, cashRequestID, nlCashRequestID, DateTime.UtcNow, false, true)
            {
                CustomerID = customerId,
                UserID     = underwriterId,
                OnSuccess  = (stra, amd) => {
                    try {
                        var calcMedalStra = stra as CalculateMedal;

                        if (calcMedalStra != null)
                        {
                            calcMedalStra.Notify();
                        }
                    } catch (Exception e) {
                        Log.Alert(e, "Failed to send notifications from CalculateMedal strategy.");
                    }                     // try
                },
            };

            return(ExecuteSync(out instance, args));
        }         // CalculateMedal
Example #3
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
Example #4
0
        }         // InstanceID

        private void SetLogThreadProperties(ExecuteArguments args)
        {
            ThreadContext.Properties["UserId"]       = args.UserID;
            ThreadContext.Properties["CustomerId"]   = args.CustomerID;
            ThreadContext.Properties["StrategyType"] = args.StrategyType.Name;
        }         // SetLogThreadProperties
Example #5
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
Example #6
0
        }         // ExecuteSync

        private ActionMetaData ExecuteSync <T>(ExecuteArguments args) where T : AStrategy
        {
            T oInstance;

            return(ExecuteSync <T>(out oInstance, args));
        }         // ExecuteSync