aboutsummaryrefslogtreecommitdiffstats
path: root/src/acit/db.py
blob: 99ba50dfdc5291940ce9cfccb1721de17e2cf127 (about) (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import cherrypy
import mariadb
from cherrypy.process import plugins
from time import sleep
from threading import Event

if not mariadb.threadsafety:
	print("Mariadb connector not threadsafe. Failing.")
	exit(1)

class DBPoolManager(plugins.SimplePlugin):
	def __init__(self, bus):
		plugins.SimplePlugin.__init__(self, bus)
		self.pool:mariadb.ConnectionPool=None
		self.poolStartedEvent=Event()

	def start(self):
		# get amount of concurrent connections
		conns=os.getenv("DB_CONNECTIONS",4)
		self.maxconns=conns
		if not type(conns)==int and not conns.isdigit():
			self._log("DB_CONNECTIONS does not have a valid value:",conns,", using 4 instead.")
			conns=4
		conns=int(conns)

		# get host/port from MYSQL_HOST
		HOST=os.getenv("MYSQL_HOST","database")
		if ":" in HOST:
			HOST,PORT=HOST.split(":",2)
		else:
			PORT=3306

		self._log(f"Connecting to database at {HOST}:{PORT}")
		
		# fill connection pool
		attempts=0
		while True:
			try:
				self.pool=mariadb.ConnectionPool(
						pool_name="main", pool_size=conns,
						user=os.getenv("MYSQL_USER","root"),
						password=os.getenv("MYSQL_PASSWORD"),
						host=HOST,
						port=PORT,
						database=os.getenv("MYSQL_DATABASE","db")
						)
				self._log("Database connection pool set up in %d attempts"%attempts)
				break

			except mariadb.Error as e:
				self._log(f"Error connecting to database: {e}")
				attempts+=1
				if attempts>=5:
					self._log("ERROR: Limit for connecting to database reached, stopping cherrypy!")
					cherrypy.engine.exit()
					return

				self._log("Attempt %d to connect to database failed (see above error), retrying in 10 seconds..."%attempts)
				sleep(10)

		cherrypy.engine.publish("db-started")
		self.poolStartedEvent.set()

	def stop(self):
		if not self.pool:
			self._log("Nothing to stop.")
			return
		self.pool.close()
		self._log("Stopped database pool.")

	def _log(self, *msg):
		"""
		Just a simple wrapper around cherrypy.log adding a context
		"""
		cherrypy.log(
				context="DBCONN",
				msg=" ".join(( str(i) for i in msg )),
				)
	
	def get_connection(self):
		if self.pool==None:
			self.poolStartedEvent.wait(cherrypy)

		conn=self.pool.get_connection()
		conn.rollback() # reset any previous actions and refresh
		return conn