1 | """ |
---|
2 | Mail handling code. Any periodic task related to send/get emails is here |
---|
3 | """ |
---|
4 | |
---|
5 | import fcntl |
---|
6 | import logging |
---|
7 | from repoze.sendmail.queue import ConsoleApp |
---|
8 | |
---|
9 | log = logging.getLogger(__name__) |
---|
10 | |
---|
11 | |
---|
12 | def queue_processor(env): |
---|
13 | """ |
---|
14 | Process the email queue, reusing repoze.sendmail default queue management |
---|
15 | machinery. |
---|
16 | """ |
---|
17 | # This mimics what is done by running the "qp" utility from |
---|
18 | # repoze.sendmail, but uses our default .ini settings instead of using a |
---|
19 | # separate ini file. |
---|
20 | |
---|
21 | # This function expects some qp.* parameters in the provided settings, |
---|
22 | # using the mail.* parameters as a fallback in case the former were not |
---|
23 | # there. |
---|
24 | |
---|
25 | # Before doing anything, check if a lock file exists. If it exists, exit |
---|
26 | # without doing anything, as another queue processor process is running. |
---|
27 | # See ticket #1389 for more information |
---|
28 | settings = env['registry'].settings |
---|
29 | lock_filename = settings.get('mail.queue_processor_lock') |
---|
30 | lock_file = open(lock_filename, 'w') |
---|
31 | try: |
---|
32 | fcntl.lockf(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB) |
---|
33 | except IOError: |
---|
34 | # Can't lock, probably another process is running, report to logging |
---|
35 | # and exit |
---|
36 | log.warning('Could not run the mail queue processing task, ' |
---|
37 | 'could not acquire lock (maybe another process is ' |
---|
38 | 'running?)') |
---|
39 | return False |
---|
40 | |
---|
41 | args = ['qp', |
---|
42 | '--hostname', settings.get('qp.host', settings.get('mail.host')), |
---|
43 | '--username', settings.get('qp.username', |
---|
44 | settings.get('mail.username')), |
---|
45 | '--password', settings.get('qp.password', |
---|
46 | settings.get('mail.password')), |
---|
47 | settings.get('mail.queue_path')] |
---|
48 | app = ConsoleApp(args) |
---|
49 | app.main() |
---|