예제 #1
0
        private async Task <DateTime?> GetCurrentDate(IDataPumperSource source)
        {
            var curDateTable = (await _context.Settings.FirstOrDefaultAsync(s => s.Key == Setting.CurrentDateTable))?.Value;
            var curDateField = (await _context.Settings.FirstOrDefaultAsync(s => s.Key == Setting.CurrentDateField))?.Value;

            if (string.IsNullOrEmpty(curDateTable) || string.IsNullOrEmpty(curDateField))
            {
                return(null);
            }
            return(await source.GetCurrentDate(new TableName(curDateTable), curDateField));
        }
예제 #2
0
        private async Task <JobLog> RunJobInternal(PumperJobItem job, IDataPumperSource sourceProvider, IDataPumperTarget targetProvider, bool fullReloading)
        {
            Log.Warn($"Processing {job.Name}");
            using (var ctx = new DataPumperContext(_configuration.MetadataConnectionString))
            {
                var tableSync = await ctx.TableSyncs.FirstOrDefaultAsync(ts => ts.TableName == job.TargetTableName);

                if (tableSync == null)
                {
                    tableSync = new TableSync
                    {
                        TableName = job.TargetTableName
                    };
                    ctx.TableSyncs.Add(tableSync);
                }

                var jobLog = new JobLog {
                    TableSync = tableSync
                };
                ctx.Logs.Add(jobLog);
                await ctx.SaveChangesAsync();

                try
                {
                    var sw = new Stopwatch();
                    sw.Start();

                    targetProvider.Progress += UpdateJobLog;

                    await targetProvider.RunQuery(job.PreRunQuery);

                    if (fullReloading)
                    {
                        // При полной перезаливке обнуляем ActualDate
                        tableSync.ActualDate = null;
                    }

                    var jobActualDate = tableSync.ActualDate; // Если переливка не выполнялась, то будет Null
                    var onDate        = jobActualDate ?? DateTime.Today.AddYears(-100);

                    var currentDate = await sourceProvider.GetCurrentDate(_configuration.CurrentDateQuery) ?? DateTime.Now.Date;

                    if (job.HistoricMode)
                    {
                        if (currentDate == tableSync.ActualDate)
                        {
                            onDate = tableSync.PreviousActualDate ?? DateTime.Today.AddYears(-100);
                        }
                        else
                        {
                            tableSync.PreviousActualDate = tableSync.ActualDate;
                        }
                    }

                    var records = await _pumper.Pump(sourceProvider, targetProvider,
                                                     new PumpParameters(
                                                         new TableName(job.SourceTableName),
                                                         new TableName(job.TargetTableName),
                                                         _configuration.ActualityColumnName,
                                                         _configuration.HistoricColumnFrom,
                                                         _configuration.TenantField,
                                                         onDate.AddDays(_configuration.BackwardReloadDays),
                                                         job.HistoricMode,
                                                         currentDate,
                                                         fullReloading,
                                                         _tenantCodes)
                    {
                        DeleteProtectionDate = _configuration.DeleteProtectionDate
                    });

                    tableSync.ActualDate    = currentDate;
                    jobLog.EndDate          = DateTime.Now;
                    jobLog.RecordsProcessed = records;
                    jobLog.Status           = SyncStatus.Success;
                    await targetProvider.RunQuery(job.PostRunQuery);

                    sw.Stop();
                }
                catch (Exception ex)
                {
                    Log.Error($"Error processing job {job}", ex);
                    jobLog.Message = ex.Message;
                    jobLog.Status  = SyncStatus.Error;
                }

                await ctx.SaveChangesAsync();

                return(jobLog);
            }
        }