Subversion Repositories SmartDukaan

Rev

Rev 8069 | Rev 8119 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 8069 Rev 8111
Line 18... Line 18...
18
class EmailSender(Daemon):
18
class EmailSender(Daemon):
19
    def __init__(self, logfile='/var/log/utils/emailsender/emailsender.log', pidfile='/var/run/email-sender.pid', count = 0):
19
    def __init__(self, logfile='/var/log/utils/emailsender/emailsender.log', pidfile='/var/run/email-sender.pid', count = 0):
20
        Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)
20
        Daemon.__init__(self, pidfile, stdout=logfile, stderr=logfile)
21
        print 'EmailSender initiated at', time.time()
21
        print 'EmailSender initiated at', time.time()
22
        self.email_schedular = sched.scheduler(time.time, time.sleep)
22
        self.email_schedular = sched.scheduler(time.time, time.sleep)
23
        self.time_to_sleep = 2
23
        self.time_to_sleep = 10
24
        self.count = count
24
        self.count = count
-
 
25
        self.exceptionCount = {}
25
        self.authenticate()
26
        self.authenticate()
26
 
27
 
27
    def authenticate(self):
28
    def authenticate(self):
28
        try:
29
        try:
29
            print "Attempting authentication at ", time.time()
30
            print "Attempting authentication at ", time.time()
Line 105... Line 106...
105
#                self.mailServer = self.sendGridMailServer
106
#                self.mailServer = self.sendGridMailServer
106
#                self.password = self.sendGridPassword
107
#                self.password = self.sendGridPassword
107
#                break
108
#                break
108
 
109
 
109
    def send(self):
110
    def send(self):
-
 
111
        '''
-
 
112
        If sending fails for a mail for more than 10 times then we will exit the sender.
-
 
113
        This is done to avoid sending a mail many times if one of the address in the 'emailTo' list is wrong.
-
 
114
        '''
110
        if self.count > 10:
115
        if self.count > 10:
111
            print str(self.count)
116
            print str(self.count)
112
            print "Number of failed attempts is more than 10. Exiting"
117
            print "Number of failed attempts is more than 10. Exiting"
113
            sys.exit()
118
            sys.exit()
-
 
119
            
-
 
120
        for emailId in self.exceptionCount.keys() :
-
 
121
            if self.exceptionCount[emailId] > 10:
-
 
122
                print "Number of exceptions for email id : " + emailId + " is : " + self.exceptionCount[emailId] + ". Exiting"
-
 
123
                sys.exit()
-
 
124
                
114
        print "Despatch stared at ", time.time()
125
        print "Despatch stared at ", time.time()
115
        helper_client = HelperClient().get_client()
126
        helper_client = HelperClient().get_client()
116
        
127
        
117
        emails_to_be_dispatched = helper_client.getEmailsToBeSent()
128
        emails_to_be_dispatched = helper_client.getEmailsToBeSent()
118
        print len(emails_to_be_dispatched)
129
        print len(emails_to_be_dispatched)
Line 122... Line 133...
122
                send_result = "error"
133
                send_result = "error"
123
                send_result = self.sendMail(email)
134
                send_result = self.sendMail(email)
124
            except Exception as e:
135
            except Exception as e:
125
                print sys.exc_info()[0]
136
                print sys.exc_info()[0]
126
                print e
137
                print e
127
                print 'Error occurred sending emails.'
138
                print 'Error occurred sending email with id - ' + email.id
-
 
139
                if self.exceptionCount.has_key(email.id) :
-
 
140
                    self.exceptionCount[email.id] = self.exceptionCount[email.id] + 1
-
 
141
                else :
128
                self.count = self.count + 1
142
                    self.exceptionCount[email.id] = 1
-
 
143
                continue
129
 
144
 
130
            if len(send_result) == 0:
145
            if len(send_result) == 0:
131
                print "Sent at", time.time()
146
                print "Sent at", time.time()
132
                helper_client.markEmailAsSent(email.id)
147
                helper_client.markEmailAsSent(email.id)
-
 
148
                if self.exceptionCount.has_key(email.id) :
-
 
149
                    del self.exceptionCount[email.id]
133
            else:
150
            else:
134
                print "Failed sending email. Id:", " Time:", time.time()
151
                print "Failed sending email. Id:", " Time:", time.time()
135
                self.count = self.count + 1
152
                self.count = self.count + 1
136
            
153
            
137
        self.email_schedular.enter(self.time_to_sleep, 1, self.send, ())
154
        self.email_schedular.enter(self.time_to_sleep, 1, self.send, ())