private string RunProcess()
        {
            IBatchProcess process = null;
            var           type    = TypeToInvoke();

            if (ImplementsIPersistentScheduledProcess(type))
            {
                int id = this.ProcessInstanceId.Value;
                process = GetInstanceOfPersistentScheduledProcess(type, id);
                if (process == null)
                {
                    throw new Exception("No instance of type " + type + " with Id =" + id);
                }
            }
            else if (ImplementsIBatchProcess(type))
            {
                process = (IServiceBatchProcess)Container.NewViewModel(type);
            }
            else
            {
                throw new DomainException("Type is not a recognised Scheduled Process implementation");
            }
            string outcome = process.Invoke();

            LastRun = Clock.Now();
            CalculateNextRunDate(LastRun.Value);
            return(outcome);
        }
Exemple #2
0
        /// <summary>
        /// Runs the batch.
        /// </summary>
        /// <param name="args">The arguments.</param>
        public static void RunBatch(string[] args)
        {
            Initialize(args);

            string batchTypeName = ExtensionsSection.ThisSection.Settings["IBatchProcess"].Type;

            IBatchProcess batchProcess = Reflection.CreateInstance <IBatchProcess>(batchTypeName);

            try
            {
                batchProcess.PreProcess();
                batchProcess.Process();
                batchProcess.PostProcess();
            }
            catch (Exception e)
            {
                try
                {
                    batchProcess.Exception(e);
                    Log.Error(string.Format("An unhandled exception was thrown by the batch job {0}", e));
                }
                catch (Exception badEx)
                {
                    Log.Error(string.Format("Bad Error: {0}", badEx));
                }
            }
        }
Exemple #3
0
 public FileDownloadDecorator(IBatchProcess batchProcess
                              , IOptions <FTPConfigModel> options
                              , IHostEnvironment env) : base(batchProcess)
 {
     _ftpConfigModel = options.Value.Download;
     _fileName       = $"download_{DateTime.Now:yyyyMMdd}.txt";
     _remotePath     = Path.Combine(_ftpConfigModel.RemotePath, _fileName);
     _localPath      = Path.Combine(env.ContentRootPath, _ftpConfigModel.LocalPath, _fileName);
 }
Exemple #4
0
        public IBatchProcess GetBatchProcess()
        {
            IBatchProcess result = Component;

            if (DecoratorsSequence != null)
            {
                foreach (var decorator in DecoratorsSequence)
                {
                    _logger.Info($"[DecoratorFactory:GetBatchProcess] {JsonConvert.SerializeObject(decorator)}");
                    result = result.Decorate(decorator);
                }
            }

            return(result);
        }
        public static BaseDecorator Decorate(this IBatchProcess component, DecoratorType decorator)
        {
            switch (decorator)
            {
            case DecoratorType.FileDownload:
                return(ActivatorUtilities.CreateInstance(Startup.ServiceProvider
                                                         , typeof(FileDownloadDecorator), new object[]
                {
                    component
                    , Startup.ServiceProvider.GetService <IOptions <FTPConfigModel> >()
                    , Startup.ServiceProvider.GetService <IHostEnvironment>()
                }) as BaseDecorator);

            case DecoratorType.ExecuteSP:
                return(ActivatorUtilities.CreateInstance(Startup.ServiceProvider
                                                         , typeof(ExecuteSPDecorator), new object[]
                {
                    component
                    , Startup.ServiceProvider.GetService <IConfiguration>()
                }) as BaseDecorator);

            case DecoratorType.FileUpload:
                return(ActivatorUtilities.CreateInstance(Startup.ServiceProvider
                                                         , typeof(FileUploadDecorator), new object[]
                {
                    component
                    , Startup.ServiceProvider.GetService <IOptions <FTPConfigModel> >()
                    , Startup.ServiceProvider.GetService <IConfiguration>()
                    , Startup.ServiceProvider.GetService <IHostEnvironment>()
                }) as BaseDecorator);

            default:
                return(ActivatorUtilities.CreateInstance(Startup.ServiceProvider
                                                         , typeof(FileDownloadDecorator), new object[]
                {
                    component
                    , Startup.ServiceProvider.GetService <IOptions <FTPConfigModel> >()
                    , Startup.ServiceProvider.GetService <IHostEnvironment>()
                }) as BaseDecorator);
            }
        }
Exemple #6
0
        public void RunProcess(dtBatchProcess batchProcess, string batchUser, DateTime batchDate, BatchCallBackDel callback)
        {
            doBatchProcessResult result = new doBatchProcessResult();

            try
            {
                if (callback != null)
                {
                    result.BatchName   = batchProcess.BatchName;
                    result.BatchCode   = batchProcess.BatchCode;
                    result.BatchStatus = BatchStatus.C_BATCH_STATUS_PROCESSING;
                    result.BatchUser   = batchUser;
                    result.BatchDate   = batchDate;
                    callback(result);
                }

                string[] batchJobName = batchProcess.BatchJobName.Split(',');
                string   assemblyName = batchJobName[0];
                string   typeName     = assemblyName + '.' + batchJobName[1];

                Assembly      assembly = Assembly.Load(assemblyName + ", Version=0.0.0.0, PublicKeyToken=null,Culture=neutral");
                Type          type     = assembly.GetType(typeName);
                IBatchProcess process  = (IBatchProcess)Activator.CreateInstance(type);
                result = process.WorkProcess(batchUser, batchDate);

                // Added by Non A. 30/Mar/2012 : Set BatchStatus only if it's null.
                if (string.IsNullOrWhiteSpace(result.BatchStatus))
                {
                    if (result.Result == true)
                    {
                        result.BatchStatus = BatchStatus.C_BATCH_STATUS_SUCCEEDED;
                    }
                    else
                    {
                        result.BatchStatus = BatchStatus.C_BATCH_STATUS_FAILED;
                    }
                }
            }
            catch (Exception ex)
            {
                result.ErrorMessage = string.Format("{0} {1}", ex.Message, ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                result.BatchStatus  = BatchStatus.C_BATCH_STATUS_FAILED;

                throw ex;
            }
            finally
            {
                if (string.IsNullOrWhiteSpace(result.BatchUser))
                {
                    result.BatchUser = batchUser;
                }
                result.BatchJobName = batchProcess.BatchCode;
                result.BatchName    = batchProcess.BatchName;
                result.BatchCode    = batchProcess.BatchCode;
                result.BatchDate    = batchDate;
                if (callback != null)
                {
                    callback(result);
                }
            }
        }
        public void Execute(JobExecutionContext context)
        {
            JobDataMap dataMap = context.JobDetail.JobDataMap;
            List <BatchProcessRunAll_Result> list = (List <BatchProcessRunAll_Result>)dataMap.Get("BatchList");
            string           UserId    = dataMap.GetString("UserId");
            DateTime         BatchDate = dataMap.GetDateTime("BatchDate");
            BatchWriteLogDel writeLog  = (BatchWriteLogDel)dataMap.Get("WriteLog");

            try
            {
                //=== start 'run batch all' ===
                doBatchProcessResult allResult_start = new doBatchProcessResult();
                allResult_start.BatchName = "Run All Batch";
                allResult_start.BatchUser = UserId;

                allResult_start.BatchStatus  = BatchStatus.C_BATCH_STATUS_PROCESSING;
                allResult_start.BatchCode    = "AL";
                allResult_start.BatchJobName = "";
                allResult_start.BatchDate    = DateTime.Now;

                writeLog(allResult_start);


                foreach (BatchProcessRunAll_Result s in list)
                {
                    doBatchProcessResult result = CommonUtil.CloneObject <BatchProcessRunAll_Result, doBatchProcessResult>(s);

                    try
                    {
                        string[] batchJobName = s.BatchJobName.Split(',');
                        string   assemblyName = batchJobName[0];
                        string   typeName     = assemblyName + '.' + batchJobName[1];
                        result.BatchStatus = BatchStatus.C_BATCH_STATUS_PROCESSING;
                        writeLog(result);

                        Assembly assembly = Assembly.Load(assemblyName + ", Version=0.0.0.0, PublicKeyToken=null,Culture=neutral");
                        Type     type     = assembly.GetType(typeName);

                        IBatchProcess process = (IBatchProcess)Activator.CreateInstance(type);
                        result = process.WorkProcess(UserId, BatchDate);

                        // == add by Narupon ==

                        result.BatchCode    = s.BatchCode;
                        result.BatchName    = s.BatchName;
                        result.BatchJobName = s.BatchJobName;
                        result.BatchDate    = DateTime.Now;


                        if (result.Result)
                        {
                            result.BatchStatus = BatchStatus.C_BATCH_STATUS_SUCCEEDED;
                        }
                        else
                        {
                            result.BatchStatus = BatchStatus.C_BATCH_STATUS_FAILED;
                        }


                        // == (end) add by Narupon ==

                        result.BatchUser = UserId;
                        writeLog(result);
                    }
                    catch (Exception ex)
                    {
                        result.ErrorMessage += string.Format("Error: {0} {1}\n", ex.Message, ex.InnerException != null ? ex.InnerException.Message : "");
                        result.BatchStatus   = BatchStatus.C_BATCH_STATUS_FAILED;
                        writeLog(result);
                    }
                }

                //=== finish 'run batch all' ===
                doBatchProcessResult allResult_finish = new doBatchProcessResult();
                allResult_finish.BatchName = "Run All Batch";
                allResult_finish.BatchUser = UserId;

                allResult_finish.BatchStatus  = BatchStatus.C_BATCH_STATUS_SUCCEEDED;
                allResult_finish.BatchCode    = "AL";
                allResult_finish.BatchJobName = "";
                allResult_finish.BatchDate    = DateTime.Now;

                writeLog(allResult_finish);
            }
            catch (Exception ex)
            {
                doBatchProcessResult errorResult = new doBatchProcessResult();
                errorResult.BatchCode    = null;
                errorResult.ErrorMessage = ex.Message;
                errorResult.BatchUser    = UserId;
                errorResult.BatchStatus  = BatchStatus.C_BATCH_STATUS_FAILED;
                writeLog(errorResult);
                throw ex;
            }
            finally
            {
                context.Scheduler.Shutdown();
            }
        }
        public void Execute(JobExecutionContext context)
        {
            JobDataMap dataMap = context.JobDetail.JobDataMap;

            doBatchProcessResult      result         = new doBatchProcessResult();
            BatchCallBackDel          callback       = (BatchCallBackDel)dataMap.Get("Callback");
            BatchProcessRunAll_Result doBatchProcess = (BatchProcessRunAll_Result)dataMap.Get("doBatchProcess");

            try
            {
                if (callback != null)
                {
                    result.BatchName   = doBatchProcess.BatchName;
                    result.BatchCode   = doBatchProcess.BatchCode;
                    result.BatchStatus = BatchStatus.C_BATCH_STATUS_PROCESSING;
                    result.BatchUser   = doBatchProcess.BatchUser;
                    result.BatchDate   = doBatchProcess.BatchDate;
                    callback(result);
                }

                string assemblyName = dataMap.GetString("assemblyName");
                string typeName     = dataMap.GetString("typeName");

                Assembly      assembly = Assembly.Load(assemblyName + ", Version=0.0.0.0, PublicKeyToken=null,Culture=neutral");
                Type          type     = assembly.GetType(typeName);
                IBatchProcess process  = (IBatchProcess)Activator.CreateInstance(type);
                result = process.WorkProcess(doBatchProcess.BatchUser, (DateTime)doBatchProcess.BatchDate);

                // Added by Non A. 30/Mar/2012 : Set BatchStatus only if it's null.
                if (string.IsNullOrWhiteSpace(result.BatchStatus))
                {
                    if (result.Result == true)
                    {
                        result.BatchStatus = BatchStatus.C_BATCH_STATUS_SUCCEEDED;
                    }
                    else
                    {
                        result.BatchStatus = BatchStatus.C_BATCH_STATUS_FAILED;
                    }
                }
            }
            catch (Exception ex)
            {
                result.ErrorMessage = string.Format("{0} {1}", ex.Message, ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                result.BatchStatus  = BatchStatus.C_BATCH_STATUS_FAILED;

                throw ex;
            }
            finally
            {
                if (string.IsNullOrWhiteSpace(result.BatchUser))
                {
                    result.BatchUser = doBatchProcess.BatchUser;
                }
                result.BatchJobName = doBatchProcess.BatchCode;
                result.BatchName    = doBatchProcess.BatchName;
                result.BatchCode    = doBatchProcess.BatchCode;
                result.BatchDate    = doBatchProcess.BatchDate;
                if (callback != null)
                {
                    callback(result);
                }

                bool keepScheduler = dataMap.GetBoolean("KeepScheduler");
                if (!keepScheduler)
                {
                    context.Scheduler.Shutdown();
                }
            }
        }
Exemple #9
0
 public ExecuteSPDecorator(IBatchProcess batchProcess, IConfiguration config)
     : base(batchProcess)
 {
     _connStr = config.GetConnectionString("DB");
 }
Exemple #10
0
 public BaseDecorator(IBatchProcess batchProcess)
 {
     _batchProcess = batchProcess;
 }
Exemple #11
0
 public DecoratorFactory(IBatchProcess component)
 {
     Component = component;
 }