Пример #1
0
        private async void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            List <ExecuteMail> exec = new List <ExecuteMail>();

            using (MySqlConnection connection = WebApiConfig.Connection())
                using (MySqlCommand command = new MySqlCommand("SELECT * FROM `tbEmailPreferences`", connection))
                {
                    connection.Open();
                    using (MySqlDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (reader["RepeatInJSON"] != DBNull.Value)
                            {
                                EmailSettings email = new EmailSettings()
                                {
                                    EmailAddress = (string)reader["RecievingEmail"], SendOnlyFailed = Convert.ToBoolean(reader["SendOnlyFailed"])
                                };
                                if (this.CorrectTime(JsonSerializationUtility.Deserialize <TaskRepeating>((string)reader["RepeatInJSON"]), 0))
                                {
                                    exec.Add(new ExecuteMail(email, (int)reader["IdAdmin"]));
                                }
                                TaskExtendDatabase(JsonSerializationUtility.Deserialize <TaskRepeating>((string)reader["RepeatInJSON"]), (int)reader["id"], "tbEmailPreferences", "RepeatInJSON");
                            }
                        }
                    }
                    connection.Close();
                    connection.Dispose();
                }
            foreach (var item in exec)
            {
                item.Execute();
            }
        }
Пример #2
0
        /// <summary>
        /// Updates TaskRepeating in database.
        /// </summary>
        /// <param name="taskRepeating">times to edit</param>
        /// <param name="Id">Id of column to edit time in</param>
        /// <param name="TableName">Name of the table to edit time in</param>
        /// <param name="ColumnName">Name of the column to edit value in</param>
        public TaskRepeating TaskExtendDatabase(TaskRepeating taskRepeating, int Id, string TableName, string ColumnName)
        {
            TaskRepeating reference = taskRepeating;

            taskRepeating.ExecutionTimes.Sort();
            for (int i = taskRepeating.ExecutionTimes.Count - 1; i >= 0; i--)
            {
                if (taskRepeating.ExecutionTimes[i] <= DateTime.Now)
                {
                    while (taskRepeating.ExecutionTimes[i] <= DateTime.Now)
                    {
                        taskRepeating.ExecutionTimes[i] = taskRepeating.ExecutionTimes[i] + taskRepeating.Repeating;
                    }
                    if (taskRepeating.ExecutionTimes[i] >= taskRepeating.RepeatTill)
                    {
                        taskRepeating.ExecutionTimes.RemoveAt(i);
                    }
                }
            }
            if (taskRepeating.ExecutionTimes.Count == 0)
            {
                mySql.AlterTable(new ChangeTable()
                {
                    Id = Id, TableName = TableName, ColumnName = ColumnName, Value = DBNull.Value
                });
                return(null);
            }
            else if (taskRepeating.Equals(reference))
            {
                mySql.AlterTable(new ChangeTable()
                {
                    Id = Id, TableName = TableName, ColumnName = ColumnName, Value = JsonSerializationUtility.Serialize(taskRepeating)
                });
            }
            return(taskRepeating);
        }