Beispiel #1
0
        static int Main(string[] args)
        {
            SetupLog();

            var container = new Container();

            Registrations.RegisterTypes(container);

            var scopeFactory       = container.GetInstance <IDbContextScopeFactory>();
            var tenantsRepo        = container.GetInstance <IReadOnlyRepository <REPORT_LOG_PARSER_TENANTS> >();
            var executionsRepo     = container.GetInstance <IRepository <REPORT_LOG_PARSER_EXECUTION> >();
            var executionsRepoForm = container.GetInstance <IRepositoryForm <REPORT_LOG_PARSER_EXECUTION_FORM> >();

            //
            if (args != null && args.Length == 2)
            {
                var tenantId = Int32.Parse(ConfigurationManager.AppSettings["tenantId"]);
                var start    = DateTime.ParseExact(args[0], "yyyy-MM-dd", CultureInfo.InvariantCulture);
                var end      = DateTime.ParseExact(args[1], "yyyy-MM-dd", CultureInfo.InvariantCulture);

                //var tenantId = 94;
                //var start = DateTime.ParseExact("2018-10-01", "yyyy-MM-dd", CultureInfo.InvariantCulture);
                //var end = DateTime.ParseExact("2018-10-02", "yyyy-MM-dd", CultureInfo.InvariantCulture);

                Log.Info($"Processing tenant {tenantId} from '{start:dd-MM-yyyy}' to '{end:dd-MM-yyyy}'.");

                var worker = container.GetInstance <PopulateAssetDecisionLog>();

                var execution = new REPORT_LOG_PARSER_EXECUTION();

                execution.TenantId  = tenantId;
                execution.RangeFrom = start;
                execution.RangeTo   = end;
                execution.StartDate = DateTime.UtcNow;

                try
                {
                    using (var scope = scopeFactory.Create())
                    {
                        executionsRepo.Insert(execution);

                        scope.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    Log.Warn($"Unable to create execution record for tenant {tenantId}.", ex);

                    return(-1);
                }

                var success = false;

                try
                {
                    worker.Process(execution);

                    success = true;
                }
                catch (Exception ex)
                {
                    Log.Warn($"Unable to process worker for tenant {tenantId}.", ex);
                }
                finally
                {
                    StoreExecution(scopeFactory, executionsRepo, execution, tenantId, success);
                }

                Log.Info("Done");

                //return 0;
            }

            if (args != null && args.Length == 2)
            {
                var executionForm = new REPORT_LOG_PARSER_EXECUTION_FORM();
                executionsRepoForm = container.GetInstance <IRepositoryForm <REPORT_LOG_PARSER_EXECUTION_FORM> >();
                var worker_form = container.GetInstance <PopulateFormDecisionLog>();
                var tenantId    = Int32.Parse(ConfigurationManager.AppSettings["tenantId"]);
                var start       = DateTime.ParseExact(args[0], "yyyy-MM-dd", CultureInfo.InvariantCulture);
                var end         = DateTime.ParseExact(args[1], "yyyy-MM-dd", CultureInfo.InvariantCulture);

                //var tenantId = 94;
                //var start = DateTime.ParseExact("2018-10-01", "yyyy-MM-dd", CultureInfo.InvariantCulture);
                //var end = DateTime.ParseExact("2018-10-02", "yyyy-MM-dd", CultureInfo.InvariantCulture);

                Log.Info($"Processing forms for tenant {tenantId} from '{start:dd-MM-yyyy}' to '{end:dd-MM-yyyy}'.");



                var execution = new REPORT_LOG_PARSER_EXECUTION_FORM();

                execution.TenantId  = tenantId;
                execution.RangeFrom = start;
                execution.RangeTo   = end;
                execution.StartDate = DateTime.UtcNow;

                try
                {
                    using (var scope = scopeFactory.Create())
                    {
                        executionsRepoForm.Insert(execution);

                        scope.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    Log.Warn($"Unable to create execution form record for tenant {tenantId}.", ex);

                    return(-1);
                }

                var success = false;

                try
                {
                    worker_form.Process(execution);

                    success = true;
                }
                catch (Exception ex)
                {
                    Log.Warn($"Unable to process form  worker for tenant {tenantId}.", ex);
                }
                finally
                {
                    FORM_StoreExecution(scopeFactory, executionsRepoForm, execution, tenantId, success);
                }

                Log.Info("Done");

                return(0);
            }


            var tenants = tenantsRepo.Get();

            if (tenants.Any())
            {
                var monthsToProcess = GetMonthsToProcess();

                var worker = container.GetInstance <PopulateAssetDecisionLog>();

                var today = DateTime.UtcNow;

                foreach (var tenant in tenants)
                {
                    var baseRangeFrom = today.AddMonths(-monthsToProcess);
                    var rangeFrom     = new DateTime(baseRangeFrom.Year, baseRangeFrom.Month, 1);
                    var baseRangeTo   = today.AddMonths(1);
                    var rangeTo       = new DateTime(baseRangeTo.Year, baseRangeTo.Month, 1).AddDays(-1);

                    Log.Info($"Processing tenant {tenant.TenantId} from '{rangeFrom:dd-MM-yyyy}' to '{rangeTo:dd-MM-yyyy}'.");

                    var execution = new REPORT_LOG_PARSER_EXECUTION();

                    execution.TenantId  = tenant.TenantId;
                    execution.RangeFrom = rangeFrom;
                    execution.RangeTo   = rangeTo;
                    execution.StartDate = DateTime.UtcNow;

                    try
                    {
                        using (var scope = scopeFactory.Create())
                        {
                            executionsRepo.Insert(execution);

                            scope.SaveChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Warn($"Unable to create execution record for tenant {tenant.TenantId}.", ex);

                        continue;
                    }

                    var success = false;

                    try
                    {
                        worker.Process(execution);

                        success = true;
                    }
                    catch (Exception ex)
                    {
                        Log.Warn($"Unable to process worker for tenant {tenant.TenantId}.", ex);

                        continue;
                    }
                    finally
                    {
                        StoreExecution(scopeFactory, executionsRepo, execution, tenant.TenantId, success);
                    }

                    Log.Info($"Completed processing tenant {tenant.TenantId} from '{rangeFrom:dd-MM-yyyy}' to '{rangeTo:dd-MM-yyyy}'.");
                }

                Log.Info("Done");
            }
            if (tenants.Any())
            {
                var monthsToProcess = GetMonthsToProcess();

                var executionForm = new REPORT_LOG_PARSER_EXECUTION_FORM();
                executionsRepoForm = container.GetInstance <IRepositoryForm <REPORT_LOG_PARSER_EXECUTION_FORM> >();


                var worker_form = container.GetInstance <PopulateFormDecisionLog>();

                var today = DateTime.UtcNow;

                foreach (var tenant in tenants)
                {
                    //var executionForm = new REPORT_LOG_PARSER_EXECUTION_FORM();
                    executionsRepoForm = container.GetInstance <IRepositoryForm <REPORT_LOG_PARSER_EXECUTION_FORM> >();

                    var baseRangeFrom = today.AddMonths(-monthsToProcess);
                    var rangeFrom     = new DateTime(baseRangeFrom.Year, baseRangeFrom.Month, 1);
                    var baseRangeTo   = today.AddMonths(1);
                    var rangeTo       = new DateTime(baseRangeTo.Year, baseRangeTo.Month, 1).AddDays(-1);

                    Log.Info($"Processing tenant {tenant} from '{rangeFrom:dd-MM-yyyy}' to '{rangeTo:dd-MM-yyyy}'.");



                    var execution = new REPORT_LOG_PARSER_EXECUTION_FORM();

                    execution.TenantId  = tenant.TenantId;
                    execution.RangeFrom = rangeFrom;
                    execution.RangeTo   = rangeTo;
                    execution.StartDate = DateTime.UtcNow;

                    try
                    {
                        using (var scope = scopeFactory.Create())
                        {
                            executionsRepoForm.Insert(execution);

                            scope.SaveChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Warn($"Unable to create execution form record for tenant {tenant.TenantId}.", ex);

                        continue;
                    }



                    var success = false;

                    try
                    {
                        worker_form.Process(execution);

                        success = true;
                    }
                    catch (Exception ex)
                    {
                        Log.Warn($"Unable to process form worker for tenant {tenant}.", ex);
                    }
                    finally
                    {
                        FORM_StoreExecution(scopeFactory, executionsRepoForm, execution, tenant.TenantId, success);
                    }

                    Log.Info("Done");

                    return(0);
                }

                Log.Info("Done");
            }

            return(0);
        }