private void DoAutoSubscription() { // Connect to DB and prepare command SqlConnection conn = FoeServerDb.OpenDb(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from AutoSubscriptions where DtSubscribed>=@oldestTime"; cmd.Parameters.Add("@oldestTime", System.Data.SqlDbType.DateTime); cmd.Prepare(); // Calculate the oldest time that we will support DateTime oldest = DateTime.Now.Subtract(new TimeSpan(2, 0, 0)); // Run query cmd.Parameters["@oldestTime"].Value = oldest; SqlDataReader reader = cmd.ExecuteReader(); // Get subscribers FoeServerAutoSubscriber sub = null; while ((sub = FoeServerAutoSubscribe.GetNextAutoSubscriberFromSqlReader(reader)) != null) { // Compare the dates to see if last updated is newer than the RSS cache FoeServerRssFeed feed = FoeServerCatalog.GetRssFeedCache(sub.CatalogCode); if ((feed != null) && (feed.DtLastUpdated > sub.DtLastUpdated)) { try { // we need to send the latest news to user FoeServerCatalog.SendRssCache(sub.CatalogCode, sub.UserEmail, sub.RequestId, true); FoeServerLog.Add(_processName, FoeServerLog.LogType.Message, "Sent updated RSS feed to " + sub.UserEmail); } catch (Exception except) { FoeServerLog.Add(_processName, FoeServerLog.LogType.Error, "Error sending updated RSS feed to " + sub.UserEmail + ":\r\n" + except.ToString()); } } else { // User already has the latest feed. } } reader.Close(); conn.Close(); }
private void DoContentDelivery() { //FoeDebug.Print("Loading requests..."); bool hasError = false; // Load content requests FoeRequester req = null; FoeServerRequest requestManager = new FoeServerRequest(RequestType.Content, FoeServerRegistry.Get("ProcessorEmail")); while ((req = requestManager.GetNextRequest()) != null) { //FoeDebug.Print("Processing request from " + req.UserEmail + " with request ID " + req.RequestId); // Check what contents are requested // Get user info FoeUser user = FoeServerUser.GetUser(req.UserEmail); if (user == null) { //FoeDebug.Print("User not registered. Skip this request."); //FoeDebug.Print("---------------------------------------"); // User is not registered, mark this request as "E" (Error) and skip to the next one requestManager.UpdateRequestStatus(req, "E"); FoeServerLog.Add(_processName, FoeServerLog.LogType.Warning, "User " + user.Email + " not registered. Discard content request."); continue; } //FoeDebug.Print("User verified."); // Process request string catalog = req.RequestMessage; string rss = FoeServerCatalog.GetRssCache(catalog); // original RSS if (rss != null) { // Send reply to user try { FoeServerMessage.SendMessage( FoeServerMessage.GetDefaultSmtpServer(), FoeServerRegistry.Get("ProcessorEmail"), req.UserEmail, SubjectGenerator.ReplySubject(RequestType.Content, catalog, req.RequestId, FoeServerUser.GetUser(req.UserEmail).UserId), rss); //FoeDebug.Print("Sent reply to user."); // Add user to auto-subscription list FoeServerAutoSubscribe.Add(req.UserEmail, catalog, req.RequestId); //FoeDebug.Print("Added user to Auto Subscription."); } catch (Exception except) { //FoeDebug.Print("Error sending email."); //FoeDebug.Print(except.ToString()); hasError = true; FoeServerLog.Add(_processName, FoeServerLog.LogType.Error, "Error delivering content to " + user.Email + "\r\n" + except.ToString()); } } // If there is no error, then we'll mark the request as 'C' (Completed). // Otherwise, we'll leave it as 'P' (Pending). if (!hasError) { // mark request as "C" (Completed) requestManager.UpdateRequestStatus(req, "C"); FoeServerLog.Add(_processName, FoeServerLog.LogType.Message, "Sent " + catalog + "to " + user.Email + " and added user to AutoSubscription."); //FoeDebug.Print("Marked request as 'C' (Completed)."); //FoeDebug.Print("----------------------------------"); } else { //FoeDebug.Print("Leave request as 'P' (Pending)."); //FoeDebug.Print("-------------------------------"); FoeServerLog.Add(_processName, FoeServerLog.LogType.Error, "Error delivering content but error is likely caused by temporary email downtime. " + "Leave status as 'P' (Pending) so process can try again later."); } } // Close all requestManager connections requestManager.Close(); }