Compare commits

..

6 Commits

3 changed files with 37 additions and 5 deletions

View File

@ -1,6 +1,5 @@
import os
import functools
import threading
from Hook import Hook
import importlib.machinery
import HookUtils
@ -94,7 +93,7 @@ class HookSystem:
threading.Thread(
name=f'bootstrap-hook-thread-{operation_id}',
target=lambda: HookSystem._call_hooks(operation_id, hooks, copy.deepcopy(get_latest())),
target=lambda: HookSystem._call_hooks(operation_id, hooks, get_latest()),
).start()
@staticmethod
@ -103,5 +102,5 @@ class HookSystem:
threading.Thread(
name=f'hook-{hook.__class__.__name__}-{operation_id}',
target=hook.update,
args=(operation_id, latest_records)
args=(operation_id, copy.deepcopy(latest_records))
).start()

33
main.py
View File

@ -57,6 +57,11 @@ def shutdown_callback(sig: int, frame) -> None:
exit(0)
def threads_dump(sig: int, frame) -> None:
running_threads = ', '.join((thread.name for thread in threading.enumerate()))
logger.info(f'Running threads: {running_threads}')
def discover(back_count: int = 0):
"""Discover new squads
:param back_count: int how many squads back we should check, it is helpful to recheck newly created squads
@ -153,6 +158,11 @@ def main():
global can_be_shutdown
signal.signal(signal.SIGTERM, shutdown_callback)
signal.signal(signal.SIGINT, shutdown_callback)
try:
signal.signal(signal.SIGUSR1, threads_dump)
except AttributeError:
pass
def help_cli() -> str:
return """Possible arguments:
@ -160,7 +170,8 @@ def main():
main.py update
main.py update amount <amount: int>
main.py update id <id: int>
main.py daemon"""
main.py daemon
main.py hooks notify <inserted;deleted> <operation_id: int>"""
logger.debug(f'argv: {sys.argv}')
@ -243,6 +254,26 @@ def main():
else:
logger.info(f'Unknown argument {sys.argv[2]}')
elif len(sys.argv) == 5:
# main.py hooks notify <inserted;deleted> <operation_id: int>
if sys.argv[1] == 'hooks' and sys.argv[2] == 'notify' and sys.argv[3] in ('inserted', 'deleted'):
try:
operation_id = int(sys.argv[4])
except ValueError:
operation_id = 0
print('operation_id must be integer')
exit(1)
logger.info(f'Notifying {sys.argv[3]} hooks with {operation_id=}')
if sys.argv[3] == 'inserted':
FAPI.hook_system.notify_inserted(operation_id)
else: # deleted
FAPI.hook_system.notify_deleted(operation_id)
exit(0)
else:
print(help_cli())
exit(1)

View File

@ -311,4 +311,6 @@ begin
user_id = new.user_id,
news_id = new.news_id,
"date" = new.date;
end;
end;
create index if not exists idx_snh_operation_id_desc on squadrons_news_historical (operation_id desc);