Explorar o código

实现全局单例线程功能。

James Iter %!s(int64=7) %!d(string=hai) anos
pai
achega
a35b3409d4
Modificáronse 6 ficheiros con 28 adicións e 16 borrados
  1. 9 9
      INSTALL.sh
  2. 1 1
      gunicorn_config.py
  3. 1 0
      jimvc/models/initialize.py
  4. 12 5
      main.py
  5. 3 1
      misc/init.sql
  6. 2 0
      misc/v0.6_to_v0.7/update_for_windows.sql

+ 9 - 9
INSTALL.sh

@@ -73,7 +73,7 @@ function check_precondition() {
     source /etc/os-release
     case ${ID} in
     centos|fedora|rhel)
-        if [ ${VERSION_ID} -lt 7 ]; then
+        if [[ ${VERSION_ID} -lt 7 ]]; then
             echo "System version must greater than or equal to 7, We found ${VERSION_ID}."
 	        exit 1
         fi
@@ -91,7 +91,7 @@ function ensure_hosts_layout() {
     echo
     read -p "您已经布局好 /etc/hosts 了吗 [Y/N]? " answer
 
-    if [  "x_"${answer} != "x_Y" ] && [ "x_"${answer} != "x_y" ]; then
+    if [[  "x_"${answer} != "x_Y" ]] && [[ "x_"${answer} != "x_y" ]]; then
         echo "OK, good bye!";
         exit 0
     fi
@@ -126,32 +126,32 @@ function sync_hosts_file() {
 
 function prepare() {
 
-    if [ ! ${JIMV_VERSION} ] || [ ${#JIMV_VERSION} -eq 0 ]; then
+    if [[ ! ${JIMV_VERSION} ]] || [[ ${#JIMV_VERSION} -eq 0 ]]; then
         export JIMV_VERSION='master'
     fi
 
-    if [ ! -e ${GENERATE_PASSWORD_SCRIPT_TMP_PATH} ]; then
+    if [[ ! -e ${GENERATE_PASSWORD_SCRIPT_TMP_PATH} ]]; then
         curl ${JIMVC_REPOSITORY_RAW_URL}'/'${JIMV_VERSION}'/misc/gen_pswd.sh' -o ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
         chmod +x ${GENERATE_PASSWORD_SCRIPT_TMP_PATH}
     fi
 
-    if [ ! ${RDB_ROOT_PSWD} ] || [ ${#RDB_ROOT_PSWD} -eq 0 ]; then
+    if [[ ! ${RDB_ROOT_PSWD} ]] || [[ ${#RDB_ROOT_PSWD} -eq 0 ]]; then
         export RDB_ROOT_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH}`
     fi
 
-    if [ ! ${RDB_JIMV_PSWD} ] || [ ${#RDB_JIMV_PSWD} -eq 0 ]; then
+    if [[ ! ${RDB_JIMV_PSWD} ]] || [[ ${#RDB_JIMV_PSWD} -eq 0 ]]; then
         export RDB_JIMV_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH}`
     fi
 
-    if [ ! ${REDIS_PSWD} ] || [ ${#REDIS_PSWD} -eq 0 ]; then
+    if [[ ! ${REDIS_PSWD} ]] || [[ ${#REDIS_PSWD} -eq 0 ]]; then
         export REDIS_PSWD=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
     fi
 
-    if [ ! ${JWT_SECRET} ] || [ ${#JWT_SECRET} -eq 0 ]; then
+    if [[ ! ${JWT_SECRET} ]] || [[ ${#JWT_SECRET} -eq 0 ]]; then
         export JWT_SECRET=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
     fi
 
-    if [ ! ${SECRET_KEY} ] || [ ${#SECRET_KEY} -eq 0 ]; then
+    if [[ ! ${SECRET_KEY} ]] || [[ ${#SECRET_KEY} -eq 0 ]]; then
         export SECRET_KEY=`${GENERATE_PASSWORD_SCRIPT_TMP_PATH} 128`
     fi
 

+ 1 - 1
gunicorn_config.py

@@ -23,7 +23,7 @@ if not os.path.isdir(log_file_dir):
 
 
 bind = _config['listen'] + ':' + _config['port'].__str__()
-workers = multiprocessing.cpu_count() * 2 + 1
+workers = 8 if multiprocessing.cpu_count() > 4 else multiprocessing.cpu_count() * 2 + 1
 worker_class = 'gevent'
 worker_connections = 1000
 daemon = False

+ 1 - 0
jimvc/models/initialize.py

@@ -42,6 +42,7 @@ class Init(object):
         'upstream_queue': 'Q:Upstream',
         'ipc_queue': 'Q:IPC',
         'hosts_info': 'H:HostsInfo',
+        'global_lock': 'H:GlobalLock',
         'compute_nodes_of_allocation_by_nonrandom': 'S:ComputeNodesOfAllocationByNonrandom',
         'guest_boot_jobs': 'S:GuestBootJobs',
         'guest_boot_jobs_wait_time': 600,

+ 12 - 5
main.py

@@ -35,13 +35,20 @@ try:
         # 父进程退出时,会清理所有提前退出的子进程的环境。所以这里无需对子进程做等待操作。
         # 即:即使子进程提前退出,且因父进程没有做wait处理,使其变成了僵尸进程。但当父进程退出时,会对因其所产生的僵尸进程做统一清理操作。
 
-        t_ = threading.Thread(target=EventProcessor.launch, args=())
-        threads.append(t_)
+        from jimvc.models import Database as db
 
-        t_ = threading.Thread(target=Init.pub_sub_ping_pong, args=())
-        threads.append(t_)
+        if db.r.hincrby(app.config['global_lock'], 'EventProcessor') == 1:
+            # 避免该进程奔溃,重启后无法再次启动如下线程。故对该 key 设定 10 秒生存周期。
+            # 即 10 秒钟后奔溃,被 gunicorn 重启后的进程,依然可以启动如下线程。
+            db.r.expire(app.config['global_lock'], 10)
 
-        t_ = threading.Thread(target=Init.clear_expire_monitor_log, args=())
+            t_ = threading.Thread(target=EventProcessor.launch, args=())
+            threads.append(t_)
+
+            t_ = threading.Thread(target=Init.clear_expire_monitor_log, args=())
+            threads.append(t_)
+
+        t_ = threading.Thread(target=Init.pub_sub_ping_pong, args=())
         threads.append(t_)
 
         for t in threads:

+ 3 - 1
misc/init.sql

@@ -23,7 +23,7 @@ ALTER TABLE user ADD INDEX (mobile_phone);
 ALTER TABLE user ADD INDEX (email);
 INSERT INTO user (login_name, password, create_time) VALUES
     ('admin', 'ji_pbkdf2$sha1$1000$b8UzawEs5kJ68TmbqQEqD07jgZGCYJsa$d3593420edee742499a974f2f377e5b220927dc7', UNIX_TIMESTAMP(NOW()) * 1000000);
-# password jimv.pswd.com
+# password pswd.jimv.cn
 
 
 CREATE TABLE IF NOT EXISTS guest(
@@ -549,6 +549,8 @@ VALUES ('CentOS-7.4', 'CentOS 7.4。', 'linux', 'centos', 7, 4, 'x86_64', 'CentO
 INSERT INTO os_template_profile (label, description, os_type, os_distro, os_major, os_minor, os_arch, os_product_name, active, icon, os_template_initialize_operate_set_id)
 VALUES ('CentOS-6.8', 'CentOS 6.8。', 'linux', 'centos', 6, 8, 'x86_64', 'CentOS release 6.8 (Final)', 1, 'icon-os icon-os-centos', 2);
 INSERT INTO os_template_profile (label, description, os_type, os_distro, os_major, os_minor, os_arch, os_product_name, active, icon, os_template_initialize_operate_set_id)
+VALUES ('CentOS-6.10', 'CentOS 6.10。', 'linux', 'centos', 6, 10, 'x86_64', 'CentOS release 6.10 (Final)', 1, 'icon-os icon-os-centos', 2);
+INSERT INTO os_template_profile (label, description, os_type, os_distro, os_major, os_minor, os_arch, os_product_name, active, icon, os_template_initialize_operate_set_id)
 VALUES ('Gentoo-2.2', 'Gentoo 2.2。', 'linux', 'gentoo', 2, 2, 'x86_64', 'Gentoo Base System release 2.2', 1, 'icon-os icon-os-gentoo', 3);
 INSERT INTO os_template_profile (label, description, os_type, os_distro, os_major, os_minor, os_arch, os_product_name, active, icon, os_template_initialize_operate_set_id)
 VALUES ('CoreOS-1855.4.0', 'CoreOS 1855.4.0。', 'linux', 'coreos', 1855, 4, 'x86_64', 'CoreOS Linux release 1855.4.0', 1, 'icon-os icon-os-coreos', 4);

+ 2 - 0
misc/v0.6_to_v0.7/update_for_windows.sql

@@ -12,6 +12,8 @@ del C:\\WINDOWS\\jimv_init.bat
 shutdown -r -t 0
 @ping 127.0.0.1 -n 2 > NUL', '');
 
+INSERT INTO os_template_profile (label, description, os_type, os_distro, os_major, os_minor, os_arch, os_product_name, active, icon, os_template_initialize_operate_set_id)
+VALUES ('CentOS-6.10', 'CentOS 6.10。', 'linux', 'centos', 6, 10, 'x86_64', 'CentOS release 6.10 (Final)', 1, 'icon-os icon-os-centos', 2);
 INSERT INTO os_template_profile (label, description, os_type, os_distro, os_major, os_minor, os_arch, os_product_name, active, icon, os_template_initialize_operate_set_id)
 VALUES ('Windows-2016-Standard', 'Windows 2016 Standard。', 'windows', 'windows', 10, 0, 'x86_64', 'Windows Server 2016 Standard', 1, 'icon-os icon-os-windows', 5);
 INSERT INTO os_template_profile (label, description, os_type, os_distro, os_major, os_minor, os_arch, os_product_name, active, icon, os_template_initialize_operate_set_id)