2020-09-29 05:33:53 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
import os
|
2021-08-12 07:27:18 +00:00
|
|
|
import signal
|
2020-09-29 05:33:53 +00:00
|
|
|
import subprocess
|
2024-01-18 10:05:21 +00:00
|
|
|
import sys
|
2020-09-29 05:33:53 +00:00
|
|
|
|
|
|
|
import redis_lock
|
2024-01-18 10:05:21 +00:00
|
|
|
from redis import Redis, Sentinel, ConnectionPool
|
2020-09-29 05:33:53 +00:00
|
|
|
|
|
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
APPS_DIR = os.path.join(BASE_DIR, 'apps')
|
2022-06-28 09:23:20 +00:00
|
|
|
CERTS_DIR = os.path.join(BASE_DIR, 'data', 'certs')
|
2020-09-29 05:33:53 +00:00
|
|
|
|
2022-06-28 09:23:20 +00:00
|
|
|
sys.path.insert(0, APPS_DIR)
|
|
|
|
from jumpserver import settings
|
2020-09-29 05:33:53 +00:00
|
|
|
|
|
|
|
os.environ.setdefault('PYTHONOPTIMIZE', '1')
|
|
|
|
if os.getuid() == 0:
|
|
|
|
os.environ.setdefault('C_FORCE_ROOT', '1')
|
|
|
|
|
2024-02-29 08:06:26 +00:00
|
|
|
connection_params = {}
|
2022-10-25 03:02:44 +00:00
|
|
|
|
|
|
|
if settings.REDIS_USE_SSL:
|
|
|
|
connection_params['ssl'] = settings.REDIS_USE_SSL
|
2022-11-21 10:27:20 +00:00
|
|
|
connection_params['ssl_cert_reqs'] = settings.REDIS_SSL_REQUIRED
|
2022-10-25 03:02:44 +00:00
|
|
|
connection_params['ssl_keyfile'] = settings.REDIS_SSL_KEY
|
|
|
|
connection_params['ssl_certfile'] = settings.REDIS_SSL_CERT
|
|
|
|
connection_params['ssl_ca_certs'] = settings.REDIS_SSL_CA
|
|
|
|
|
|
|
|
REDIS_SENTINEL_SERVICE_NAME = settings.REDIS_SENTINEL_SERVICE_NAME
|
|
|
|
REDIS_SENTINELS = settings.REDIS_SENTINELS
|
2022-11-01 02:59:04 +00:00
|
|
|
REDIS_SENTINEL_PASSWORD = settings.REDIS_SENTINEL_PASSWORD
|
|
|
|
REDIS_SENTINEL_SOCKET_TIMEOUT = settings.REDIS_SENTINEL_SOCKET_TIMEOUT
|
2022-10-25 03:02:44 +00:00
|
|
|
if REDIS_SENTINEL_SERVICE_NAME and REDIS_SENTINELS:
|
|
|
|
connection_params['sentinels'] = REDIS_SENTINELS
|
2024-02-29 08:06:26 +00:00
|
|
|
connection_params['password'] = settings.REDIS_PASSWORD
|
2022-11-01 02:59:04 +00:00
|
|
|
sentinel_client = Sentinel(
|
|
|
|
**connection_params, sentinel_kwargs={
|
2022-11-21 10:27:20 +00:00
|
|
|
'ssl': settings.REDIS_USE_SSL,
|
|
|
|
'ssl_cert_reqs': settings.REDIS_SSL_REQUIRED,
|
|
|
|
'ssl_keyfile': settings.REDIS_SSL_KEY,
|
|
|
|
'ssl_certfile': settings.REDIS_SSL_CERT,
|
|
|
|
'ssl_ca_certs': settings.REDIS_SSL_CA,
|
2022-11-01 02:59:04 +00:00
|
|
|
'password': REDIS_SENTINEL_PASSWORD,
|
|
|
|
'socket_timeout': REDIS_SENTINEL_SOCKET_TIMEOUT
|
|
|
|
}
|
|
|
|
)
|
2022-10-25 03:02:44 +00:00
|
|
|
redis_client = sentinel_client.master_for(REDIS_SENTINEL_SERVICE_NAME)
|
|
|
|
else:
|
2024-03-28 10:44:52 +00:00
|
|
|
REDIS_PROTOCOL = 'rediss' if connection_params.pop('ssl', False) else 'redis'
|
2024-01-18 10:05:21 +00:00
|
|
|
REDIS_LOCATION_NO_DB = '%(protocol)s://:%(password)s@%(host)s:%(port)s' % {
|
|
|
|
'protocol': REDIS_PROTOCOL,
|
2024-02-29 08:06:26 +00:00
|
|
|
'password': settings.REDIS_PASSWORD_QUOTE,
|
2024-01-18 10:05:21 +00:00
|
|
|
'host': settings.REDIS_HOST,
|
|
|
|
'port': settings.REDIS_PORT,
|
|
|
|
}
|
|
|
|
pool = ConnectionPool.from_url(REDIS_LOCATION_NO_DB, **connection_params)
|
|
|
|
redis_client = Redis(connection_pool=pool)
|
2022-10-25 03:02:44 +00:00
|
|
|
|
2024-03-08 08:55:00 +00:00
|
|
|
scheduler = "django_celery_beat.schedulers:DatabaseScheduler"
|
2022-06-28 09:23:20 +00:00
|
|
|
processes = []
|
2020-09-29 05:33:53 +00:00
|
|
|
cmd = [
|
2022-03-02 12:48:43 +00:00
|
|
|
'celery',
|
2020-09-29 05:33:53 +00:00
|
|
|
'-A', 'ops',
|
2022-03-02 12:48:43 +00:00
|
|
|
'beat',
|
2020-09-29 05:33:53 +00:00
|
|
|
'-l', 'INFO',
|
|
|
|
'--scheduler', scheduler,
|
|
|
|
'--max-interval', '60'
|
|
|
|
]
|
|
|
|
|
2021-08-12 07:27:18 +00:00
|
|
|
|
|
|
|
def stop_beat_process(sig, frame):
|
|
|
|
for p in processes:
|
|
|
|
os.kill(p.pid, 15)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
# 父进程结束通知子进程结束
|
|
|
|
signal.signal(signal.SIGTERM, stop_beat_process)
|
|
|
|
|
2022-10-25 03:02:44 +00:00
|
|
|
with redis_lock.Lock(redis_client, name="beat-distribute-start-lock", expire=60, auto_renewal=True):
|
2021-08-12 07:27:18 +00:00
|
|
|
print("Get beat lock start to run it")
|
|
|
|
process = subprocess.Popen(cmd, cwd=APPS_DIR)
|
|
|
|
processes.append(process)
|
|
|
|
process.wait()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|