|
27 | 27 | """ |
28 | 28 |
|
29 | 29 | import argparse |
| 30 | +import fcntl |
| 31 | +import time |
30 | 32 | import sys |
31 | 33 | import ujson |
32 | 34 | from requests.exceptions import HTTPError, Timeout |
33 | 35 | from lib import QFeedsActions |
| 36 | +from lib.api import QFeedsConfig |
34 | 37 |
|
35 | 38 |
|
36 | 39 | if __name__ == '__main__': |
37 | 40 | parser = argparse.ArgumentParser() |
38 | 41 | parser.add_argument('--target_dir', default='/var/db/qfeeds-tables') |
39 | 42 | parser.add_argument('-f', help='forced (auto index)' , default=False, action='store_true') |
40 | 43 | parser.add_argument('-v', help='verbose output' , default=False, action='store_true') |
| 44 | + parser.add_argument('-l', help='lock operation' , default=False, action='store_true') |
41 | 45 | parser.add_argument("action", choices=QFeedsActions.list_actions(), nargs='*') |
42 | 46 | args = parser.parse_args() |
| 47 | + |
| 48 | + fhandle = None |
| 49 | + if args.l: |
| 50 | + lck_filename = '/tmp/qfeeds_prc.LCK' |
| 51 | + fhandle = open(lck_filename, 'a+') |
| 52 | + try: |
| 53 | + fcntl.flock(fhandle, fcntl.LOCK_EX | fcntl.LOCK_NB) |
| 54 | + except IOError: |
| 55 | + print('already busy, exit') |
| 56 | + sys.exit(0) |
| 57 | + |
43 | 58 | if args.v: |
44 | 59 | # verbose mode |
45 | 60 | import http.client as http_client |
|
51 | 66 | print(msg) |
52 | 67 | except HTTPError as exc: |
53 | 68 | print('exit with HTTPError %d (%s)' % (exc.response.status_code, exc.response.text)) |
| 69 | + if exc.response.status_code == 401 and 'update' in args.action: |
| 70 | + print('batch mode - wait for configuration update or timeout') |
| 71 | + t_start = time.time() |
| 72 | + while not QFeedsConfig.has_changed(): |
| 73 | + if time.time() - t_start > 3600: |
| 74 | + print('timeout waiting for config change') |
| 75 | + break |
| 76 | + time.sleep(5) |
54 | 77 | sys.exit(-1) |
55 | 78 | except Timeout as exc: |
56 | 79 | print('timeout reaching api endpoint') |
|
61 | 84 | except ujson.JSONDecodeError: |
62 | 85 | print("JSON decode error") |
63 | 86 | sys.exit(-1) |
| 87 | + finally: |
| 88 | + if fhandle: |
| 89 | + fcntl.flock(fhandle, fcntl.LOCK_UN) |
0 commit comments