Browse Source

New process table for a better detection of running workers

tags/3.5
Michael Vogel 2 years ago
parent
commit
22f32d9721

+ 40
- 1
boot.php View File

@@ -38,7 +38,7 @@ define ( 'FRIENDICA_PLATFORM',     'Friendica');
38 38
 define ( 'FRIENDICA_CODENAME',     'Asparagus');
39 39
 define ( 'FRIENDICA_VERSION',      '3.5-dev' );
40 40
 define ( 'DFRN_PROTOCOL_VERSION',  '2.23'    );
41
-define ( 'DB_UPDATE_VERSION',      1201      );
41
+define ( 'DB_UPDATE_VERSION',      1202      );
42 42
 
43 43
 /**
44 44
  * @brief Constant with a HTML line break.
@@ -1099,6 +1099,42 @@ class App {
1099 1099
 
1100 1100
 	}
1101 1101
 
1102
+	/**
1103
+	 * @brief Log active processes into the "process" table
1104
+	 */
1105
+	function start_process() {
1106
+		$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
1107
+
1108
+		$command = basename($trace[0]["file"]);
1109
+
1110
+		$this->remove_inactive_processes();
1111
+
1112
+		$r = q("SELECT `pid` FROM `process` WHERE `pid` = %d", intval(getmypid()));
1113
+		if(!dbm::is_result($r))
1114
+			q("INSERT INTO `process` (`pid`,`command`,`created`) VALUES (%d, '%s', '%s')",
1115
+				intval(getmypid()),
1116
+				dbesc($command),
1117
+				dbesc(datetime_convert()));
1118
+	}
1119
+
1120
+	/**
1121
+	 * @brief Remove inactive processes
1122
+	 */
1123
+	function remove_inactive_processes() {
1124
+		$r = q("SELECT `pid` FROM `process`");
1125
+		if(dbm::is_result($r))
1126
+			foreach ($r AS $process)
1127
+				if (!posix_kill($process["pid"], 0))
1128
+					q("DELETE FROM `process` WHERE `pid` = %d", intval($process["pid"]));
1129
+	}
1130
+
1131
+	/**
1132
+	 * @brief Remove the active process from the "process" table
1133
+	 */
1134
+	function end_process() {
1135
+		q("DELETE FROM `process` WHERE `pid` = %d", intval(getmypid()));
1136
+	}
1137
+
1102 1138
 	/**
1103 1139
 	 * @brief Returns a string with a callstack. Can be used for logging.
1104 1140
 	 *
@@ -1699,6 +1735,9 @@ function login($register = false, $hiddens=false) {
1699 1735
  * @brief Used to end the current process, after saving session state.
1700 1736
  */
1701 1737
 function killme() {
1738
+
1739
+	get_app()->end_process();
1740
+
1702 1741
 	if (!get_app()->is_backend())
1703 1742
 		session_write_close();
1704 1743
 

+ 2
- 0
include/auth_ejabberd.php View File

@@ -58,6 +58,8 @@ if(is_null($db)) {
58 58
 	unset($db_host, $db_user, $db_pass, $db_data);
59 59
 };
60 60
 
61
+$a->start_process();
62
+
61 63
 // the logfile to which to write, should be writeable by the user which is running the server
62 64
 $sLogFile = get_config('jabber','logfile');
63 65
 

+ 3
- 1
include/cli_startup.php View File

@@ -11,7 +11,7 @@ function cli_startup() {
11 11
 	if(is_null($a)) {
12 12
 		$a = new App;
13 13
 	}
14
-  
14
+
15 15
 	if(is_null($db)) {
16 16
 	    @include(".htconfig.php");
17 17
     	require_once("dba.php");
@@ -19,6 +19,8 @@ function cli_startup() {
19 19
     	unset($db_host, $db_user, $db_pass, $db_data);
20 20
   	};
21 21
 
22
+	$a->start_process();
23
+
22 24
 	require_once('include/session.php');
23 25
 
24 26
 	load_config('config');

+ 1
- 0
include/cron.php View File

@@ -27,6 +27,7 @@ function cron_run(&$argv, &$argc){
27 27
 		unset($db_host, $db_user, $db_pass, $db_data);
28 28
 	};
29 29
 
30
+	$a->start_process();
30 31
 
31 32
 	require_once('include/session.php');
32 33
 	require_once('include/datetime.php');

+ 2
- 0
include/cronhooks.php View File

@@ -17,6 +17,8 @@ function cronhooks_run(&$argv, &$argc){
17 17
 		unset($db_host, $db_user, $db_pass, $db_data);
18 18
 	};
19 19
 
20
+	$a->start_process();
21
+
20 22
 	require_once('include/session.php');
21 23
 	require_once('include/datetime.php');
22 24
 

+ 1
- 0
include/cronjobs.php View File

@@ -27,6 +27,7 @@ function cronjobs_run(&$argv, &$argc){
27 27
 		unset($db_host, $db_user, $db_pass, $db_data);
28 28
 	};
29 29
 
30
+	$a->start_process();
30 31
 
31 32
 	require_once('include/session.php');
32 33
 	require_once('include/datetime.php');

+ 13
- 0
include/dbstructure.php View File

@@ -1114,6 +1114,17 @@ function db_definition() {
1114 1114
 					"choice" => array("choice"),
1115 1115
 					)
1116 1116
 			);
1117
+	$database["process"] = array(
1118
+			"fields" => array(
1119
+					"pid" => array("type" => "int(10) unsigned", "not null" => "1", "primary" => "1"),
1120
+					"command" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
1121
+					"created" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
1122
+					),
1123
+			"indexes" => array(
1124
+					"PRIMARY" => array("pid"),
1125
+					"command" => array("command"),
1126
+					)
1127
+			);
1117 1128
 	$database["profile"] = array(
1118 1129
 			"fields" => array(
1119 1130
 					"id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
@@ -1456,6 +1467,8 @@ function dbstructure_run(&$argv, &$argc) {
1456 1467
 			unset($db_host, $db_user, $db_pass, $db_data);
1457 1468
 	}
1458 1469
 
1470
+	$a->start_process();
1471
+
1459 1472
 	if ($argc==2) {
1460 1473
 		switch ($argv[1]) {
1461 1474
 			case "update":

+ 2
- 0
include/dbupdate.php View File

@@ -16,6 +16,8 @@ function dbupdate_run(&$argv, &$argc) {
16 16
 		        unset($db_host, $db_user, $db_pass, $db_data);
17 17
 	}
18 18
 
19
+	$a->start_process();
20
+
19 21
 	load_config('config');
20 22
 	load_config('system');
21 23
 

+ 2
- 0
include/delivery.php View File

@@ -21,6 +21,8 @@ function delivery_run(&$argv, &$argc){
21 21
 		unset($db_host, $db_user, $db_pass, $db_data);
22 22
 	}
23 23
 
24
+	$a->start_process();
25
+
24 26
 	require_once("include/session.php");
25 27
 	require_once("include/datetime.php");
26 28
 	require_once('include/items.php');

+ 2
- 0
include/directory.php View File

@@ -15,6 +15,8 @@ function directory_run(&$argv, &$argc){
15 15
 				unset($db_host, $db_user, $db_pass, $db_data);
16 16
 	};
17 17
 
18
+	$a->start_process();
19
+
18 20
 	load_config('config');
19 21
 	load_config('system');
20 22
 

+ 2
- 0
include/discover_poco.php View File

@@ -18,6 +18,8 @@ function discover_poco_run(&$argv, &$argc){
18 18
 	unset($db_host, $db_user, $db_pass, $db_data);
19 19
 	};
20 20
 
21
+	$a->start_process();
22
+
21 23
 	require_once('include/session.php');
22 24
 	require_once('include/datetime.php');
23 25
 

+ 2
- 0
include/expire.php View File

@@ -16,6 +16,8 @@ function expire_run(&$argv, &$argc){
16 16
     	unset($db_host, $db_user, $db_pass, $db_data);
17 17
   	};
18 18
 
19
+	$a->start_process();
20
+
19 21
 	require_once('include/session.php');
20 22
 	require_once('include/datetime.php');
21 23
 	require_once('include/items.php');

+ 2
- 0
include/gprobe.php View File

@@ -18,6 +18,8 @@ function gprobe_run(&$argv, &$argc){
18 18
     	unset($db_host, $db_user, $db_pass, $db_data);
19 19
   	};
20 20
 
21
+	$a->start_process();
22
+
21 23
 	require_once('include/session.php');
22 24
 	require_once('include/datetime.php');
23 25
 

+ 2
- 0
include/notifier.php View File

@@ -54,6 +54,8 @@ function notifier_run(&$argv, &$argc){
54 54
 			unset($db_host, $db_user, $db_pass, $db_data);
55 55
 	}
56 56
 
57
+	$a->start_process();
58
+
57 59
 	require_once("include/session.php");
58 60
 	require_once("include/datetime.php");
59 61
 	require_once('include/items.php');

+ 1
- 0
include/onepoll.php View File

@@ -24,6 +24,7 @@ function onepoll_run(&$argv, &$argc){
24 24
 		unset($db_host, $db_user, $db_pass, $db_data);
25 25
 	};
26 26
 
27
+	$a->start_process();
27 28
 
28 29
 	require_once('include/session.php');
29 30
 	require_once('include/datetime.php');

+ 18
- 10
include/poller.php View File

@@ -29,6 +29,10 @@ function poller_run(&$argv, &$argc){
29 29
 		unset($db_host, $db_user, $db_pass, $db_data);
30 30
 	};
31 31
 
32
+	$a->start_process();
33
+
34
+	$mypid = getmypid();
35
+
32 36
 	if ($a->max_processes_reached())
33 37
 		return;
34 38
 
@@ -81,15 +85,19 @@ function poller_run(&$argv, &$argc){
81 85
 
82 86
 		q("UPDATE `workerqueue` SET `executed` = '%s', `pid` = %d WHERE `id` = %d AND `executed` = '0000-00-00 00:00:00'",
83 87
 			dbesc(datetime_convert()),
84
-			intval(getmypid()),
88
+			intval($mypid),
85 89
 			intval($r[0]["id"]));
86 90
 
87 91
 		// Assure that there are no tasks executed twice
88
-		$id = q("SELECT `id` FROM `workerqueue` WHERE `id` = %d AND `pid` = %d",
89
-			intval($r[0]["id"]),
90
-			intval(getmypid()));
92
+		$id = q("SELECT `pid`, `executed` FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
91 93
 		if (!$id) {
92
-			logger("Queue item ".$r[0]["id"]." was executed multiple times - skip this execution", LOGGER_DEBUG);
94
+			logger("Queue item ".$r[0]["id"]." vanished - skip this execution", LOGGER_DEBUG);
95
+			continue;
96
+		} elseif ((strtotime($id[0]["executed"]) <= 0) OR ($id[0]["pid"] == 0)) {
97
+			logger("Entry for queue item ".$r[0]["id"]." wasn't stored - we better stop here", LOGGER_DEBUG);
98
+			return;
99
+		} elseif ($id[0]["pid"] != $mypid) {
100
+			logger("Queue item ".$r[0]["id"]." is to be executed by process ".$id[0]["pid"]." and not by me (".$mypid.") - skip this execution", LOGGER_DEBUG);
93 101
 			continue;
94 102
 		}
95 103
 
@@ -111,15 +119,15 @@ function poller_run(&$argv, &$argc){
111 119
 		$funcname = str_replace(".php", "", basename($argv[0]))."_run";
112 120
 
113 121
 		if (function_exists($funcname)) {
114
-			logger("Process ".getmypid()." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." ".$r[0]["parameter"]);
122
+			logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." ".$r[0]["parameter"]);
115 123
 			$funcname($argv, $argc);
116 124
 
117 125
 			if ($cooldown > 0) {
118
-				logger("Process ".getmypid()." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
126
+				logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - in cooldown for ".$cooldown." seconds");
119 127
 				sleep($cooldown);
120 128
 			}
121 129
 
122
-			logger("Process ".getmypid()." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - done");
130
+			logger("Process ".$mypid." - Prio ".$r[0]["priority"]." - ID ".$r[0]["id"].": ".$funcname." - done");
123 131
 
124 132
 			q("DELETE FROM `workerqueue` WHERE `id` = %d", intval($r[0]["id"]));
125 133
 		} else
@@ -319,9 +327,9 @@ function poller_too_much_workers() {
319 327
 }
320 328
 
321 329
 function poller_active_workers() {
322
-	$workers = q("SELECT COUNT(*) AS `workers` FROM `workerqueue` WHERE `executed` != '0000-00-00 00:00:00'");
330
+	$workers = q("SELECT COUNT(*) AS `processes` FROM `process` WHERE `command` = 'poller.php'");
323 331
 
324
-	return($workers[0]["workers"]);
332
+	return($workers[0]["processes"]);
325 333
 }
326 334
 
327 335
 if (array_search(__file__,get_included_files())===0){

+ 2
- 0
include/pubsubpublish.php View File

@@ -70,6 +70,8 @@ function pubsubpublish_run(&$argv, &$argc){
70 70
 		unset($db_host, $db_user, $db_pass, $db_data);
71 71
 	};
72 72
 
73
+	$a->start_process();
74
+
73 75
 	require_once('include/items.php');
74 76
 
75 77
 	load_config('config');

+ 1
- 0
include/queue.php View File

@@ -17,6 +17,7 @@ function queue_run(&$argv, &$argc){
17 17
 		unset($db_host, $db_user, $db_pass, $db_data);
18 18
 	};
19 19
 
20
+	$a->start_process();
20 21
 
21 22
 	require_once("include/session.php");
22 23
 	require_once("include/datetime.php");

+ 2
- 0
include/shadowupdate.php View File

@@ -14,6 +14,8 @@ if(is_null($db)) {
14 14
 	unset($db_host, $db_user, $db_pass, $db_data);
15 15
 }
16 16
 
17
+$a->start_process();
18
+
17 19
 load_config('config');
18 20
 load_config('system');
19 21
 

+ 2
- 0
include/tagupdate.php View File

@@ -14,6 +14,8 @@ if(is_null($db)) {
14 14
 	unset($db_host, $db_user, $db_pass, $db_data);
15 15
 }
16 16
 
17
+$a->start_process();
18
+
17 19
 load_config('config');
18 20
 load_config('system');
19 21
 

+ 2
- 0
include/threadupdate.php View File

@@ -14,6 +14,8 @@ if(is_null($db)) {
14 14
 	unset($db_host, $db_user, $db_pass, $db_data);
15 15
 }
16 16
 
17
+$a->start_process();
18
+
17 19
 load_config('config');
18 20
 load_config('system');
19 21
 

+ 2
- 0
include/update_gcontact.php View File

@@ -16,6 +16,8 @@ function update_gcontact_run(&$argv, &$argc){
16 16
 		unset($db_host, $db_user, $db_pass, $db_data);
17 17
 	};
18 18
 
19
+	$a->start_process();
20
+
19 21
 	require_once('include/Scrape.php');
20 22
 	require_once("include/socgraph.php");
21 23
 

+ 1
- 1
update.php View File

@@ -1,6 +1,6 @@
1 1
 <?php
2 2
 
3
-define('UPDATE_VERSION' , 1201);
3
+define('UPDATE_VERSION' , 1202);
4 4
 
5 5
 /**
6 6
  *

Loading…
Cancel
Save