source: OpenWorkouts-current/ow/tasks/mail.py @ b5d87e0

currentfeature/docs
Last change on this file since b5d87e0 was b5d87e0, checked in by Borja Lopez <borja@…>, 5 years ago

(#29) Added a tasks module, implemented a task to run the mail queue processor,
sending any automatically generated emails in the queue.

Tasks can be run manually, with the virtual env active:

python ow/tasks/run.py development.ini TASK_NAME

(where TASK_NAME is the name of a task, like send_emails)

Running the run.py script without parameters gives an usage-like message
with instructions on how to properly call the script.

  • Property mode set to 100644
File size: 1.8 KB
Line 
1"""
2Mail handling code. Any periodic task related to send/get emails is here
3"""
4
5import fcntl
6import logging
7from repoze.sendmail.queue import ConsoleApp
8
9log = logging.getLogger(__name__)
10
11
12def 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()
Note: See TracBrowser for help on using the repository browser.