From 45446fcceef55a247d23749a1dcd28417dc73277 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Fri, 20 Sep 2024 02:54:35 +0200 Subject: webmap-import: add option --lockfile to obtain an exclusive lock. This avoids starting multiple imports in parallel. Some layers, such as Skogsstyrelsen's, are quite large and filtering/importing causes rather high load. --- webmap-import | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/webmap-import b/webmap-import index 37826cf..bfee9dc 100755 --- a/webmap-import +++ b/webmap-import @@ -18,7 +18,9 @@ # along with this program. If not, see . #---------------------------------------------------------------------- +from os import O_WRONLY, O_CREAT, O_TRUNC, O_CLOEXEC import os +from fcntl import flock, LOCK_EX import logging import argparse import tempfile @@ -1193,6 +1195,8 @@ if __name__ == '__main__': help=f'cache directory (default: {os.curdir})') parser.add_argument('--debug', action='count', default=0, help=argparse.SUPPRESS) + parser.add_argument('--lockfile', default=None, + help=f'obtain an exclusive lock before starting unpacking and importing') parser.add_argument('groupname', nargs='*', help='group layer name(s) to process') args = parser.parse_args() @@ -1244,6 +1248,12 @@ if __name__ == '__main__': srs = getSRS(common.config.get('SRS', None)) extent = getExtent(common.config.get('extent', None), srs=srs) + if args.lockfile is not None: + # obtain an exclusive lock and don't release it until exiting the program + lock_fd = os.open(args.lockfile, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, mode=0o644) + logging.debug('flock("%s", LOCK_EX)', args.lockfile) + flock(lock_fd, LOCK_EX) + cachedir = Path(args.cachedir) if args.cachedir is not None else None rv = 0 for layername, layerdef in layers.items(): -- cgit v1.2.3