From a58a0ec0010ab63580c94bcd86756c6e0cfcd7e3 Mon Sep 17 00:00:00 2001 From: midoks Date: Sun, 19 Jun 2022 19:02:59 +0800 Subject: [PATCH] sphinx to systemd --- plugins/sphinx/conf/sphinx.conf | 24 ++++--- plugins/sphinx/index.py | 88 +++++++++++------------- plugins/sphinx/init.d/sphinx.service.tpl | 12 ++++ 3 files changed, 67 insertions(+), 57 deletions(-) create mode 100644 plugins/sphinx/init.d/sphinx.service.tpl diff --git a/plugins/sphinx/conf/sphinx.conf b/plugins/sphinx/conf/sphinx.conf index 96a83a137..de9e5f270 100755 --- a/plugins/sphinx/conf/sphinx.conf +++ b/plugins/sphinx/conf/sphinx.conf @@ -2,23 +2,27 @@ # Minimal Sphinx configuration sample (clean, simple, functional) # -indexer -{ - mem_limit = 218M -} searchd { - listen = 9312 + listen = 9312 listen = 9306:mysql41 - log = {$SERVER_APP}/index/searchd.log - query_log = {$SERVER_APP}/index/query.log + log = {$SERVER_APP}/index/searchd.log + query_log = {$SERVER_APP}/index/query.log + pid_file = {$SERVER_APP}/index/searchd.pid + #workers = threads # for RT to work + binlog_path = {$SERVER_APP}/index/binlog read_timeout = 5 max_children = 0 - pid_file = {$SERVER_APP}/index/searchd.pid seamless_rotate = 1 preopen_indexes = 1 unlink_old = 1 - #workers = threads # for RT to work - binlog_path = {$SERVER_APP}/index/binlog } + +index mydocs +{ + type = rt + path = {$SERVER_APP}/bin/doc + rt_field = title + rt_attr_json = j +} \ No newline at end of file diff --git a/plugins/sphinx/index.py b/plugins/sphinx/index.py index ef1b16718..676c97a06 100755 --- a/plugins/sphinx/index.py +++ b/plugins/sphinx/index.py @@ -146,6 +146,17 @@ def initDreplace(): conf_content = contentReplace(conf_content) mw.writeFile(getServerDir() + '/sphinx.conf', conf_content) + # systemd + systemDir = '/lib/systemd/system' + systemService = systemDir + '/sphinx.service' + systemServiceTpl = getPluginDir() + '/init.d/sphinx.service.tpl' + if os.path.exists(systemDir) and not os.path.exists(systemService): + service_path = mw.getServerDir() + se_content = mw.readFile(systemServiceTpl) + se_content = se_content.replace('{$SERVER_PATH}', service_path) + mw.writeFile(systemService, se_content) + mw.execShell('systemctl daemon-reload') + mkdirAll() return file_bin @@ -165,45 +176,35 @@ def checkIndexSph(): return True -def start(): +def sphOp(method): file = initDreplace() - data = sphinxConfParse() - if 'index' in data: - if checkIndexSph(): - rebuild() - time.sleep(5) - else: - return '配置不正确!' + if not mw.isAppleSystem(): + data = mw.execShell('systemctl ' + method + ' sphinx') + if data[1] == '': + return 'ok' + return 'fail' - data = mw.execShell(file + ' start') + data = mw.execShell(file + ' ' + method) if data[1] == '': return 'ok' return data[1] +def start(): + return sphOp('start') + + def stop(): - file = initDreplace() - data = mw.execShell(file + ' stop') - if data[1] == '': - return 'ok' - return data[1] + return sphOp('stop') def restart(): - file = initDreplace() - data = mw.execShell(file + ' restart') - if data[1] == '': - return 'ok' - return data[1] + return sphOp('restart') def reload(): - file = initDreplace() - data = mw.execShell(file + ' reload') - if data[1] == '': - return 'ok' - return 'fail' + return sphOp('reload') def rebuild(): @@ -215,36 +216,29 @@ def rebuild(): def initdStatus(): - if not app_debug: - if mw.isAppleSystem(): - return "Apple Computer does not support" - initd_bin = getInitDFile() - if os.path.exists(initd_bin): - return 'ok' - return 'fail' + if mw.isAppleSystem(): + return "Apple Computer does not support" + + shell_cmd = 'systemctl status sphinx | grep loaded | grep "enabled;"' + data = mw.execShell(shell_cmd) + if data[0] == '': + return 'fail' + return 'ok' def initdInstall(): - import shutil - if not app_debug: - if mw.isAppleSystem(): - return "Apple Computer does not support" - - source_bin = initDreplace() - initd_bin = getInitDFile() - shutil.copyfile(source_bin, initd_bin) - mw.execShell('chmod +x ' + initd_bin) - mw.execShell('chkconfig --add ' + getPluginName()) + if mw.isAppleSystem(): + return "Apple Computer does not support" + + mw.execShell('systemctl enable sphinx') return 'ok' def initdUinstall(): - if not app_debug: - if mw.isAppleSystem(): - return "Apple Computer does not support" - initd_bin = getInitDFile() - os.remove(initd_bin) - mw.execShell('chkconfig --del ' + getPluginName()) + if mw.isAppleSystem(): + return "Apple Computer does not support" + + mw.execShell('systemctl disable sphinx') return 'ok' diff --git a/plugins/sphinx/init.d/sphinx.service.tpl b/plugins/sphinx/init.d/sphinx.service.tpl new file mode 100644 index 000000000..fdc62b236 --- /dev/null +++ b/plugins/sphinx/init.d/sphinx.service.tpl @@ -0,0 +1,12 @@ +[Unit] +Description=Open Source Search Server +After=network.target + +[Service] +Type=forking +ExecStart={$SERVER_PATH}/bin/bin/searchd -c {$SERVER_APP}/sphinx.conf +ExecReload=/bin/kill -USR2 $MAINPID +Restart=on-failure + +[Install] +WantedBy=multi-user.target \ No newline at end of file