]> git.mxchange.org Git - fba.git/blob - fba/helpers/locking.py
Continued:
[fba.git] / fba / helpers / locking.py
1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17 import logging
18 import os
19 import sys
20 import tempfile
21 import zc.lockfile
22
23 logging.basicConfig(level=logging.INFO)
24 logger = logging.getLogger(__name__)
25
26 # Lock file
27 lockfile = tempfile.gettempdir() + '/fba.lock'
28 LOCK = None
29
30 def acquire():
31     global LOCK
32     logger.debug("CALLED!")
33
34     try:
35         logger.debug("Acquiring lock: lockfile='%s'", lockfile)
36         LOCK = zc.lockfile.LockFile(lockfile)
37         logger.debug("Lock obtained.")
38
39     except zc.lockfile.LockError:
40         logger.error("Cannot aquire lock: lockfile='%s'", lockfile)
41         sys.exit(100)
42
43     logger.debug("EXIT!")
44
45 def release():
46     logger.debug("CALLED!")
47     if LOCK is not None:
48         logger.debug("Releasing lock ...")
49         LOCK.close()
50
51         logger.debug("Deleting lockfile='%s' ...", lockfile)
52         os.remove(lockfile)
53
54     logger.debug("EXIT!")