Provides various official and user-released code examples. For code reference, you are welcome to learn about the timed detection and smooth restart mechanism of workerman.
I. timed Detection
Last time I talked about how workerman and Thinkphp are used in combination. After testing, they have been deployed to the official environment.
Reference: http://www.isbala.com/article/269.html
Thinkphp: http://www.thinkphp1.cn/code/2026.html
Okay, Here workerman is running as a daemon. If the main process of the daemon is down, our business will be affected, such as the missing statistical data.
This time we use timed detection to prevent the main process from being crashed. Here we use the crontab service in the Linux environment:
Run as root regularly, edit/var/spool/cron/root
*/1 *****/bin/sh/data/yunwei/XXXXX/workermanMonitor. sh>/dev/null 2> & 1 &
Check whether the main process is still running every minute. If not, restart immediately.
WorkermanMonitor. sh script:
#! /Bin/bash
# Author: XXXX
# Desc: monitor the master workerman Process
Count = 'ps-ef | grep' WorkerMan '| grep-v 'grep' | grep 'master' | wc-l'
Echo $ count
If [$ count-lt 1]; then
# Write your web project root directory here. The thinkphp command line needs to be run in the root directory
Cd/data/wwwroot/XXXXX/
/Usr/local/php/bin/php index. php Workerman/index restart
Echo "restart ";
Echo $ (date + % Y-% m-% d _ % H: % M: % S)>/data/wwwlogs/CMSWorker/mainRestart. log
Fi
Ii. smooth restart:
We have deployed the workerman controller code in thinkphp last time. If we need to add various configuration items in Home/Conf/config. php, since workerman runs as a daemon,
This configuration item has been loaded when it is enabled. Restarting workerman will affect the official business. At this time, the workerman smooth restart mechanism is required. Of course, workerman is not actually restarted.
We load thinkphp configuration items in the workerman Code as follows:
$ Worker-> onMessage = function ($ connection, $ data ){
Static $ request_count = 0; // number of processed requests
If (++ $ request_count> = MAX_REQUEST) {// if the number of requests reaches 1000, the main process immediately restarts a new process to complete the process restart.
Worker: stopAll ();
}
C (load_config (MODULE_PATH. 'conf/config'. CONF_EXT); // re-load the configuration. Note that you can load your own Thinkphp configuration items here. My own business is other configurations.
... // Other business logic code
}
Reload the Thinkphp configuration in the callback method, so that you do not need to restart it. Let's review the workerman restart:
/Usr/local/php/bin/php index. php Workerman/index restart
Summary:
Speaking of this, some people may think of timing issues. Should I edit/var/spool/cron/root in Linux for every task added? What should I do if I have no permissions in the official environment? Do I have to bother O & M personnel?
Is Thinkphp able to directly configure these crontab services in the background, and then publish configuration items in one click, so that the business will be executed on time and stably according to my configuration?
Yes! First, go to the image:
It's Monday. It's about to go to work. The Code will be replaced next time ~
You can also refer to here: http://www.isbala.com/article/271.html
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB