forked from nirgal/pgsf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabort_refresh.py
More file actions
executable file
·97 lines (74 loc) · 2.35 KB
/
abort_refresh.py
File metadata and controls
executable file
·97 lines (74 loc) · 2.35 KB
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
91
92
93
94
95
96
97
#!/usr/bin/python3
'''
Module to handle cancellation of refreshes.
It handles the system killing of process.
'''
import argparse
import logging
import sys
import psutil
import config
import synctable
from tabledesc import TableDesc
def find_refresh_process(tablename, sync_check=True):
'''
Find the psutil.Process that is currently refreshing a table.
Can return None.
If sync_check, __sync table is read first, verifying the state is
'running'. Returns None if it is not.
'''
logger = logging.getLogger(__name__)
if sync_check:
status = synctable.get_status(tablename)
if status != 'running':
logger.error('TABLE %s status is %s',
tablename, status)
return None
else:
logger.debug('Skipping sync table checks')
for proc in psutil.process_iter():
cmdline = proc.cmdline()
if (
len(cmdline) >= 3
and 'python' in cmdline[0]
and 'query_poll_table' in cmdline[1]
and cmdline[2] == tablename
):
return proc
return None
def kill_refresh(tablename, sync_check=True):
'''
Stop a table from being refreshed.
Any running refresh process is killed.
__sync table status is set to 'error'.
'''
logger = logging.getLogger(__name__)
td = TableDesc(tablename)
proc = find_refresh_process(tablename, sync_check)
if not proc:
logger.error('Process not found')
return False
synctable.update(td, 'error')
proc.kill()
return True
if __name__ == '__main__':
def main():
parser = argparse.ArgumentParser(
description='Abort a table refresh')
parser.add_argument(
'table',
help='table name')
parser.add_argument(
'--no-check-sync',
default=False, action='store_true',
help="Don't check __sync table")
args = parser.parse_args()
logging.basicConfig(
filename=config.LOGFILE,
format=config.LOGFORMAT.format('abort_refresh '+args.table),
level=config.LOGLEVEL)
tablename = args.table
sync_check = not args.no_check_sync
if not kill_refresh(tablename, sync_check):
print('Failed', file=sys.stderr)
main()