Ejemplo n.º 1
0
        static DateChunk ValidateDateChunk(DateChunk chunk)
        {
            var solar = GetSolarTimes(chunk.StartDate);

            //if it starts too early, move it up to our first time of the day
            if (chunk.StartDate < solar.Start.AddMinutes(Config.StartTimeSunrise))
            {
                chunk.StartDate = solar.Start.AddMinutes(Config.StartTimeSunrise);
                return(chunk.IsInThePast() ? chunk : null);
            }

            //if it starts after the end of the day, move it to tomorrow
            if (chunk.StartDate > solar.End.AddMinutes(Config.EndTimeSunset))
            {
                chunk.StartDate = chunk.StartDate.Date.AddDays(1);
                return(ValidateDateChunk(chunk));
            }

            //if it would end after the end of the day, cut the length to stop then
            if (chunk.StartDate.AddMinutes(chunk.LengthMinutes) > solar.End.AddMinutes(Config.EndTimeSunset))
            {
                chunk.LengthMinutes = (int)(solar.End.AddMinutes(Config.EndTimeSunset) - chunk.StartDate).TotalMinutes + 1;
                return(chunk.IsInThePast() ? chunk : null);
            }

            //it's the middle of the day
            return(chunk.IsInThePast() ? chunk : null);
        }
Ejemplo n.º 2
0
        public void DeleteDateChunk(DateChunk chunk)
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();

                connection.Execute("DELETE FROM DateChunk WHERE Id = @id", chunk);
            }
        }
Ejemplo n.º 3
0
        public void UpdateDateChunk(DateChunk chunk)
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();

                connection.Execute("UPDATE DateChunk SET ProcessingTime = @processingTime, LengthMinutes = @lengthMinutes, ExportDone = @exportDone, DateProcessed = @dateProcessed WHERE Id = @id", chunk);
            }
        }
Ejemplo n.º 4
0
        public void InsertDateChunk(DateChunk chunk)
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();

                var id = connection.QueryFirst <int>("INSERT INTO DateChunk (StartDate, ProcessingTime, LengthMinutes, ExportDone, DateProcessed, MachineName) VALUES (@startDate, @processingTime, @lengthMinutes, @exportDone, @dateProcessed, @machineName);SELECT CAST(SCOPE_IDENTITY() as INT)", chunk);
                chunk.Id = id;
            }
        }
Ejemplo n.º 5
0
        static bool ShouldDeleteLatestChunk(DateChunk chunk)
        {
            if (chunk == null)
            {
                return(false);
            }

            //This should be updated to make sure only OUR program on dotnet core is counted but it's good enough for now
            var alreadyRunning = Process.GetProcessesByName("dotnet").Count() > 1;
            var sameMachine    = chunk.MachineName?.StartsWith(Environment.MachineName) ?? false;

            return(chunk.ProcessingTime == 0 && !alreadyRunning && sameMachine);
        }
Ejemplo n.º 6
0
        public DateChunk GetNextDateChunk()
        {
            using (var connection = new SqlConnection(ConnectionString))
            {
                connection.Open();

                var exportingCount = connection.QueryFirst <int>(@"SELECT COUNT(*) FROM DateChunk WHERE ExportDone = 0");
                if (exportingCount > 0)
                {
                    return(null);
                }

                var latest    = connection.QueryFirst <DateChunk>(@"SELECT * FROM DateChunk ORDER BY StartDate DESC");
                var nextChunk = new DateChunk
                {
                    StartDate      = latest.StartDate.AddMinutes(latest.LengthMinutes),
                    ProcessingTime = 0,
                    LengthMinutes  = 0,
                    ExportDone     = false
                };

                return(nextChunk);
            }
        }
Ejemplo n.º 7
0
 static void ChunkExported(DateChunk chunk)
 {
     chunk.ExportDone = true;
     Database.UpdateDateChunk(chunk);
 }