File: amqp_clock.py

package info (click to toggle)
python-amqplib 1.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 404 kB
  • sloc: python: 2,296; makefile: 3
file content (70 lines) | stat: -rwxr-xr-x 2,344 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
"""
AMQP Clock

Fires off simple messages at one-minute intervals to a topic
exchange named 'clock', with the topic of the message being
the local time as 'year.month.date.dow.hour.minute',
for example: '2007.11.26.1.12.33', where the dow (day of week)
is 0 for Sunday, 1 for Monday, and so on (similar to Unix crontab).

A consumer could then bind a queue to the routing key '#.0'
for example to get a message at the beginning of each hour.

2007-11-26 Barry Pederson <bp@barryp.org>

"""
from datetime import datetime
from optparse import OptionParser
from time import sleep

import amqplib.client_0_8 as amqp
Message = amqp.Message

EXCHANGE_NAME = 'clock'
TOPIC_PATTERN = '%Y.%m.%d.%w.%H.%M' # Python datetime.strftime() pattern

def main():
    parser = OptionParser()
    parser.add_option('--host', dest='host',
                        help='AMQP server to connect to (default: %default)',
                        default='localhost')
    parser.add_option('-u', '--userid', dest='userid',
                        help='AMQP userid to authenticate as (default: %default)',
                        default='guest')
    parser.add_option('-p', '--password', dest='password',
                        help='AMQP password to authenticate with (default: %default)',
                        default='guest')
    parser.add_option('--ssl', dest='ssl', action='store_true',
                        help='Enable SSL with AMQP server (default: not enabled)',
                        default=False)

    options, args = parser.parse_args()

    conn = amqp.Connection(options.host, options.userid, options.password)
    ch = conn.channel()
    ch.access_request('/data', write=True, active=True)
    ch.exchange_declare(EXCHANGE_NAME, type='topic')

    # Make sure our first message is close to the beginning
    # of a minute
    now = datetime.now()
    if now.second > 0:
        sleep(60 - now.second)

    while True:
        now = datetime.now()
        msg = Message(timestamp=now)
        msg_topic = now.strftime(TOPIC_PATTERN)
        ch.basic_publish(msg, EXCHANGE_NAME, routing_key=msg_topic)

        # Don't know how long the basic_publish took, so
        # grab the time again.
        now = datetime.now()
        sleep(60 - now.second)

    ch.close()
    conn.close()

if __name__ == '__main__':
    main()