Debian Codegraph @

python/examples/db_send.py

raw
1 #!/usr/bin/env python3
2 #
3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing,
14 # software distributed under the License is distributed on an
15 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 # KIND, either express or implied. See the License for the
17 # specific language governing permissions and limitations
18 # under the License.
19 #
20
21 import optparse
22 import queue
23
24
25 from proton import Message
26 from proton.handlers import MessagingHandler
27 from proton.reactor import ApplicationEvent, Container, EventInjector
28 from db_common import Db
29
30
31 class Send(MessagingHandler):
32 def __init__(self, url, count):
33 super(Send, self).__init__()
34 self.url = url
35 self.delay = 0
36 self.sent = 0
37 self.confirmed = 0
38 self.load_count = 0
39 self.records = queue.Queue(maxsize=50)
40 self.target = count
41 self.db = Db("src_db", EventInjector())
42
43 def keep_sending(self):
44 return self.target == 0 or self.sent < self.target
45
46 def on_start(self, event):
47 self.container = event.container
48 self.container.selectable(self.db.injector)
49 self.sender = self.container.create_sender(self.url)
50
51 def on_records_loaded(self, event):
52 if self.records.empty():
53 if event.subject == self.load_count:
54 print("Exhausted available data, waiting to recheck...")
55 # check for new data after 5 seconds
56 self.container.schedule(5, self)
57 else:
58 self.send()
59
60 def request_records(self):
61 if not self.records.full():
62 print("loading records...")
63 self.load_count += 1
64 self.db.load(self.records, event=ApplicationEvent(
65 "records_loaded", link=self.sender, subject=self.load_count))
66
67 def on_sendable(self, event):
68 self.send()
69
70 def send(self):
71 while self.sender.credit and not self.records.empty():
72 if not self.keep_sending():
73 return
74 record = self.records.get(False)
75 id = record['id']
76 self.sender.send(Message(id=id, durable=True, body=record['description']), tag=str(id))
77 self.sent += 1
78 print("sent message %s" % id)
79 self.request_records()
80
81 def on_settled(self, event):
82 id = int(event.delivery.tag)
83 self.db.delete(id)
84 print("settled message %s" % id)
85 self.confirmed += 1
86 if self.confirmed == self.target:
87 event.connection.close()
88 self.db.close()
89
90 def on_disconnected(self, event):
91 self.db.reset()
92 self.sent = self.confirmed
93
94 def on_timer_task(self, event):
95 print("Rechecking for data...")
96 self.request_records()
97
98
99 parser = optparse.OptionParser(usage="usage: %prog [options]",
100 description="Send messages to the supplied address.")
101 parser.add_option("-a", "--address", default="localhost:5672/examples",
102 help="address to which messages are sent (default %default)")
103 parser.add_option("-m", "--messages", type="int", default=0,
104 help="number of messages to send; 0 sends indefinitely (default %default)")
105 opts, args = parser.parse_args()
106
107 try:
108 Container(Send(opts.address, opts.messages)).run()
109 except KeyboardInterrupt:
110 pass