]> git.ipfire.org Git - oddments/cappie.git/blob - cappie/queue.py
Add garbage collector event.
[oddments/cappie.git] / cappie / queue.py
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # Cappie #
5 # Copyright (C) 2010 Michael Tremer #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import time
23
24 from threading import Thread
25
26 from database import Database
27 from errors import *
28 from events import *
29
30 class Queue(Thread):
31 heartbeat = 1.0
32 maxitems = 10000
33
34 def __init__(self, log):
35 Thread.__init__(self)
36
37 self.log = log
38
39 self.__running = True
40 self.__queue = []
41
42 self.db = Database(log)
43 self.lastgc = None
44
45 def __len__(self):
46 return self.length
47
48 def add(self, event):
49 if self.length > self.maxitems:
50 raise QueueFullError, "Cannot queue new event."
51
52 self.__queue.append(event)
53
54 @property
55 def length(self):
56 return len(self.__queue)
57
58 def run(self):
59 self.log.debug("Started event queue")
60
61 self.db.open()
62
63 while self.__running or self.__queue:
64 if not self.__queue:
65 #self.log.debug("Queue sleeping for %s seconds" % self.heartbeat)
66 time.sleep(self.heartbeat)
67 continue
68
69 self._checkGc()
70
71 event = self.__queue.pop(0)
72 self.log.debug("Processing queue event: %s" % event)
73 try:
74 event.run()
75 except EventException, e:
76 self.log.error("Catched event exception: %s" % e)
77
78 self.db.close()
79
80 def shutdown(self):
81 self.__running = False
82 self.log.debug("Shutting down queue")
83 self.log.debug("%d events in queue left" % len(self.__queue))
84
85 # Wait until queue handled all events
86 self.join()
87
88 def _checkGc(self):
89 if not self.lastgc or self.lastgc <= (time.time() - DB_GC_INTERVAL):
90 self.add(EventGarbageCollector(self.db, self.log))
91 self.lastgc = time.time()