From 2d73ea68dafb8459ca985fc66c7440df7b37f4d7 Mon Sep 17 00:00:00 2001 From: rong wang Date: Tue, 31 Oct 2023 13:52:29 +0800 Subject: [PATCH] feat: Run anything service in independent process Using an independent process to run anything service can solve known log conflict problems. When the service runs abnormally, the service can be restored by creating a new process. Log: handle high CPU usage Task: https://pms.uniontech.com/task-view-300371.html --- debian/changelog | 7 ++++++ debian/deepin-anything-server.install | 1 + src/server/CMakeLists.txt | 1 + src/server/app/CMakeLists.txt | 33 ++++++++++++++++++++++++++ src/server/app/main.cpp | 34 +++++++++++++++++++++++++++ src/server/backend/anythingexport.h | 1 + src/server/backend/lib/lftmanager.cpp | 7 ++++++ 7 files changed, 84 insertions(+) create mode 100644 src/server/app/CMakeLists.txt create mode 100644 src/server/app/main.cpp diff --git a/debian/changelog b/debian/changelog index 23b1f8f4..e0c2a2d3 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,10 @@ +deepin-anything (6.1.5) unstable; urgency=medium + + * add executable for running anything. + * let process abort when cpu usage limit is exceeded. + + -- Deepin Package Builder Thu, 26 Oct 2023 14:35:37 +0800 + deepin-anything (6.1.4) unstable; urgency=medium * fix index issue. diff --git a/debian/deepin-anything-server.install b/debian/deepin-anything-server.install index 6bba7b22..1d4edb66 100644 --- a/debian/deepin-anything-server.install +++ b/debian/deepin-anything-server.install @@ -1,3 +1,4 @@ +usr/bin/deepin-anything-server usr/lib/*/libdeepin-anything-server-lib.* usr/share/dbus-1/interfaces etc/dbus-1/system.d diff --git a/src/server/CMakeLists.txt b/src/server/CMakeLists.txt index 16a81283..1f343ef5 100644 --- a/src/server/CMakeLists.txt +++ b/src/server/CMakeLists.txt @@ -16,3 +16,4 @@ if(NOT CMAKE_BUILD_TYPE) endif(NOT CMAKE_BUILD_TYPE) add_subdirectory("backend") +add_subdirectory("app") diff --git a/src/server/app/CMakeLists.txt b/src/server/app/CMakeLists.txt new file mode 100644 index 00000000..62a82f06 --- /dev/null +++ b/src/server/app/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.10) + +project(deepin-anything-server) + +# Version +if (NOT PROJECT_VERSION) + set(PROJECT_VERSION "1.0.0") +endif() + +if (NOT PROJECT_VERSION_MAJOR) + set(PROJECT_VERSION_MAJOR 0) +endif() + +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/../backend +) + +set(SRCS + main.cpp +) + +find_package(PkgConfig REQUIRED) +find_package(Qt5 COMPONENTS Core REQUIRED) + +add_executable(${PROJECT_NAME} ${SRCS}) + +target_link_libraries(${PROJECT_NAME} PUBLIC + Qt5::Core + deepin-anything-server-lib +) + +# binary +install(TARGETS ${PROJECT_NAME} DESTINATION bin) diff --git a/src/server/app/main.cpp b/src/server/app/main.cpp new file mode 100644 index 00000000..903e68db --- /dev/null +++ b/src/server/app/main.cpp @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include +#include +#include "anythingexport.h" +#include + +static void handleSIGTERM(int sig) +{ + qDebug() << "received SIGTERM" << sig; + if (qApp) { + qApp->quit(); + } +} + +int main(int argc, char *argv[]) +{ + int ret; + + QCoreApplication app(argc, argv); + app.setOrganizationName("deepin"); + + if (fireAnything()) { + qCritical() << "fireAnything failed!"; + abort(); + } + signal(SIGTERM, handleSIGTERM); + + ret = app.exec(); + downAnything(); + return ret; +} diff --git a/src/server/backend/anythingexport.h b/src/server/backend/anythingexport.h index ec994890..cbf3ebfd 100644 --- a/src/server/backend/anythingexport.h +++ b/src/server/backend/anythingexport.h @@ -10,5 +10,6 @@ #define ANYTHINGBACKEND_SHARED_EXPORT Q_DECL_EXPORT extern "C" ANYTHINGBACKEND_SHARED_EXPORT int fireAnything(); +extern "C" ANYTHINGBACKEND_SHARED_EXPORT void downAnything(); #endif // ANYTHINGEXPORT_H diff --git a/src/server/backend/lib/lftmanager.cpp b/src/server/backend/lib/lftmanager.cpp index 5630e645..3bbf2c00 100755 --- a/src/server/backend/lib/lftmanager.cpp +++ b/src/server/backend/lib/lftmanager.cpp @@ -27,6 +27,7 @@ extern "C" { #include #include +#include Q_LOGGING_CATEGORY(logN, "anything.normal.manager", DEFAULT_MSG_TYPE) Q_LOGGING_CATEGORY(logC, "anything.changes.manager", DEFAULT_MSG_TYPE) @@ -1091,6 +1092,12 @@ void LFTManager::_cpuLimitCheck() QProcess::startDetached(cmd + "50%"); cpu_limited = true; nWarning() << "Limited, long time high CPU usage: " << current_cpu; + + struct stat statbuf; + if (stat("/tmp/anything_disable_abort", &statbuf)) { + nWarning() << "Abort for high CPU usage."; + abort(); + } } else if (current_cpu < low_use) { QProcess::startDetached(cmd); cpu_limited = false;