コード例 #1
0
        protected void UpdateTimeBehind()
        {
            var    path = $"{EventName}{Separator}{MinutesBehindFileName}";
            string txt  = "";

            if (File.Exists(path))
            {
                txt = File.ReadAllText(path);
            }

            var canParseFile = int.TryParse(txt, out int minutes);

            if (canParseFile == false && File.Exists(path))
            {
                ReminderServiceLog.Error($"Unable to parse {path}. [{txt}] is not a valid integer");
                return;
            }

            if (txt.IsNullOrEmpty())
            {
                _minutesBehind = 0;
            }
            else
            {
                _minutesBehind = minutes;
            }
        }
コード例 #2
0
        private void ReadReminderCsv(string fileName)
        {
            //if all records have been read in every file, kill the timer
            if (_runnersDone && _volunteersDone)
            {
                ReminderServiceLog.Information($"All records sent, ending {EventName} timer");
                KillTimer();
                return;
            }

            //Update how far we are behind
            UpdateTimeBehind();

            //Don't process files that are already finished
            switch (fileName)
            {
            case Filename_Runners:
                if (_runnersDone)
                {
                    return;
                }
                break;

            case Filename_Volunteers:
                if (_volunteersDone)
                {
                    return;
                }
                break;

            default: throw new ArgumentException($"{fileName} is invalid!");
            }

            var filePath       = $@"{_filePath}{fileName}.csv";
            var processingPath = $@"{_filePath}{fileName}_Processing.csv";

            try
            {
                var records = new List <ReminderRecord>();
                //Setup the CSV Reader
                using var reader = new StreamReader(filePath);
                using var csv    = new CsvReader(reader, CultureInfo.InvariantCulture);
                csv.Configuration.MissingFieldFound = null;
                csv.Read();
                csv.ReadHeader();

                //Read the CSV and populate list of records
                while (csv.Read())
                {
                    var record = new ReminderRecord
                    {
                        Scheduled       = csv.GetField <DateTime>(ScheduleTimeHeader),
                        Game            = csv.GetField <string>(GameHeader),
                        StreamKey       = csv.GetField <string>(StreamKeyHeader),
                        VolunteerType   = csv.GetField <string>(VolunteerTypeHeader),
                        Category        = csv.GetField <string>(CategoryHeader),
                        DiscordUserName = csv.GetField <string>(DiscordUserHeader),
                        Sent            = csv.GetField <int>(SentHeader)
                    };

                    records.Add(record);
                }

                //Process the reminders here, send any reminders that are due & update the sent flag
                var processedRecords = ProcessReminderRecords(records).ToList();

                //Mark the file done if all rows have been sent
                if (processedRecords.Count(x => x.Sent == 1) == processedRecords.Count)
                {
                    switch (fileName)
                    {
                    case Filename_Runners:
                        _runnersDone = true;
                        break;

                    case Filename_Volunteers:
                        _volunteersDone = true;
                        break;

                    default: throw new ArgumentException($"{fileName} is invalid!");
                    }
                }

                using (var writer = new StreamWriter(processingPath))
                {
                    using var csv2 = new CsvWriter(writer, CultureInfo.InvariantCulture);
                    csv2.WriteRecords(processedRecords);
                }
                reader.Close();

                ReminderServiceLog.Verbose($"Replacing file {fileName} with processed content");
                File.Replace(processingPath, filePath, null); //Replace existing file with processing
            }
            catch (Exception e)
            {
                ReminderServiceLog.Error(e.Message);
                ReminderServiceLog.Error(e.StackTrace);
                _client.GetUser(167082289325408256).SendMessageAsync(e.StackTrace);
            }
        }