]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #987 from annando/master
authortobiasd <tobias.diekershoff@gmx.net>
Sun, 25 May 2014 05:59:22 +0000 (07:59 +0200)
committertobiasd <tobias.diekershoff@gmx.net>
Sun, 25 May 2014 05:59:22 +0000 (07:59 +0200)
Auto-killing processes, somce changed code in the plaintext function and speed issues in network

include/cronhooks.php
include/network.php
include/onepoll.php
include/pidfile.php
include/plaintext.php
include/poller.php
include/queue.php
mod/network.php

index cf1d13765faf56d8d55b49bc5d6796cc677ede02..0ba453555ce63c580622d922ed3feee27438e329 100644 (file)
@@ -37,9 +37,15 @@ function cronhooks_run(&$argv, &$argc){
 
        $lockpath = get_config('system','lockpath');
        if ($lockpath != '') {
-               $pidfile = new pidfile($lockpath, 'cron.lck');
+               $pidfile = new pidfile($lockpath, 'cronhooks');
                if($pidfile->is_already_running()) {
                        logger("cronhooks: Already running");
+                       if ($pidfile->running_time() > 9*60) {
+                                $pidfile->kill();
+                                logger("cronhooks: killed stale process");
+                               // Calling a new instance
+                               proc_run('php','include/cronhooks.php');
+                        }
                        exit;
                }
        }
@@ -50,11 +56,12 @@ function cronhooks_run(&$argv, &$argc){
 
        logger('cronhooks: start');
 
-
        $d = datetime_convert();
 
        call_hooks('cron', $d);
 
+       logger('cronhooks: end');
+
        return;
 }
 
index 240d2978f1a6ea26759f8f7f2ead72d946f2e64e..fe08fc5a7b45ca33750548a2e55ec1f913833114 100644 (file)
@@ -122,7 +122,6 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
 
 if(! function_exists('post_url')) {
 function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) {
-
        $stamp1 = microtime(true);
 
        $a = get_app();
@@ -130,6 +129,8 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0)
        if(($redirects > 8) || (! $ch))
                return false;
 
+       logger("post_url: start ".$url, LOGGER_DATA);
+
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_POST,1);
@@ -178,6 +179,8 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0)
        $curl_info = curl_getinfo($ch);
        $http_code = $curl_info['http_code'];
 
+       logger("post_url: result ".$http_code." - ".$url, LOGGER_DATA);
+
        $header = '';
 
        // Pull out multiple headers, e.g. proxy and continuation headers
@@ -190,16 +193,18 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0)
        }
 
        if($http_code == 301 || $http_code == 302 || $http_code == 303 || $http_code == 307) {
-        $matches = array();
-        preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
-        $newurl = trim(array_pop($matches));
+               $matches = array();
+               preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
+               $newurl = trim(array_pop($matches));
                if(strpos($newurl,'/') === 0)
                        $newurl = $old_location_info["scheme"] . "://" . $old_location_info["host"] . $newurl;
-        if (filter_var($newurl, FILTER_VALIDATE_URL)) {
-            $redirects++;
-            return fetch_url($newurl,false,$redirects,$timeout);
-        }
-    }
+               if (filter_var($newurl, FILTER_VALIDATE_URL)) {
+                       $redirects++;
+                       logger("post_url: redirect ".$url." to ".$newurl);
+                       return post_url($newurl,$params, $headers, $redirects, $timeout);
+                       //return fetch_url($newurl,false,$redirects,$timeout);
+               }
+       }
        $a->set_curl_code($http_code);
        $body = substr($s,strlen($header));
 
@@ -209,6 +214,8 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0)
 
        $a->save_timestamp($stamp1, "network");
 
+       logger("post_url: end ".$url, LOGGER_DATA);
+
        return($body);
 }}
 
index 49047118a4b20c3fe96b3a255518657cd0c85de2..15b8e7c2cc20c2679f04cfe2e1408585aed071a4 100644 (file)
@@ -60,7 +60,7 @@ function onepoll_run(&$argv, &$argc){
        // Test
        $lockpath = get_config('system','lockpath');
        if ($lockpath != '') {
-               $pidfile = new pidfile($lockpath, 'onepoll'.$contact_id.'.lck');
+               $pidfile = new pidfile($lockpath, 'onepoll'.$contact_id);
                if($pidfile->is_already_running()) {
                        logger("onepoll: Already running for contact ".$contact_id);
                        exit;
index 47df8d1f4624ef43fa2430d013f4000f8edad411..4f5b25ad756521af2dee7626a26beb6c83395964 100644 (file)
@@ -28,5 +28,14 @@ class pidfile {
        public function is_already_running() {
                return $this->_running;
        }
+
+       public function running_time() {
+               return(time() - filectime($this->_file));
+       }
+
+       public function kill() {
+               if (file_exists($this->_file))
+                       return(posix_kill(file_get_contents($this->_file), SIGTERM));
+       }
 }
 ?>
index 18763f86aaeaea2959705a6b139262f01a342a0e..d824f8759590d3068d3e048db0b12cf355687176 100644 (file)
@@ -9,6 +9,10 @@ function get_attached_data($body) {
  - description:
  - (thumbnail)
 */
+
+       // Simplify image codes
+       $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
+
        $post = array();
 
        if (preg_match_all("(\[class=(.*?)\](.*?)\[\/class\])ism",$body, $attached,  PREG_SET_ORDER)) {
@@ -38,40 +42,11 @@ function get_attached_data($body) {
 
                }
        }
-       return($post);
-}
-
-function shortenmsg($msg, $limit) {
-       $lines = explode("\n", $msg);
-       $msg = "";
-       $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
-       foreach ($lines AS $row=>$line) {
-               if (strlen(trim($msg."\n".$line)) <= $limit)
-                       $msg = trim($msg."\n".$line);
-               // Is the new message empty by now or is it a reshared message?
-               elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
-                       $msg = substr(substr(trim($msg."\n".$line), 0, $limit), 0, -3)."...";
-               else
-                       break;
-       }
-       return($msg);
-}
-
-function plaintext($a, $b, $limit = 0, $includedlinks = false) {
-       require_once("include/bbcode.php");
-       require_once("include/html2plain.php");
-       require_once("mod/parse_url.php");
-       require_once("include/network.php");
-
-       // Simplify image codes
-       $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $b["body"]);
-
-       // At first look at data that is attached via "type-..." stuff
-       // This will hopefully replaced with a dedicated bbcode later
-       $post = get_attached_data($body);
 
        // if nothing is found, it maybe having an image.
        if (!isset($post["type"])) {
+               require_once("mod/parse_url.php");
+
                $URLSearchString = "^\[\]";
                if (preg_match_all("(\[url=([$URLSearchString]*)\]\s*\[img\]([$URLSearchString]*)\[\/img\]\s*\[\/url\])ism", $body, $pictures,  PREG_SET_ORDER)) {
                        if (count($pictures) == 1) {
@@ -125,6 +100,37 @@ function plaintext($a, $b, $limit = 0, $includedlinks = false) {
                }
        }
 
+       return($post);
+}
+
+function shortenmsg($msg, $limit, $twitter = false) {
+       // To-Do:
+       // For Twitter URLs aren't shortened, but they have to be calculated as if.
+
+       $lines = explode("\n", $msg);
+       $msg = "";
+       $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
+       foreach ($lines AS $row=>$line) {
+               if (strlen(trim($msg."\n".$line)) <= $limit)
+                       $msg = trim($msg."\n".$line);
+               // Is the new message empty by now or is it a reshared message?
+               elseif (($msg == "") OR (($row == 1) AND (substr($msg, 0, 4) == $recycle)))
+                       $msg = substr(substr(trim($msg."\n".$line), 0, $limit), 0, -3)."...";
+               else
+                       break;
+       }
+       return($msg);
+}
+
+function plaintext($a, $b, $limit = 0, $includedlinks = false) {
+       require_once("include/bbcode.php");
+       require_once("include/html2plain.php");
+       require_once("include/network.php");
+
+       // At first look at data that is attached via "type-..." stuff
+       // This will hopefully replaced with a dedicated bbcode later
+       $post = get_attached_data($b["body"]);
+
        if (($b["title"] != "") AND ($post["text"] != ""))
                $post["text"] = trim($b["title"]."\n\n".$post["text"]);
        elseif ($b["title"] != "")
index 084067b190059c3c6f0290b2476811b1f2c87d82..b04c77f74446d80ce627fb41786a50b9aae3ed49 100644 (file)
@@ -43,9 +43,15 @@ function poller_run(&$argv, &$argc){
 
        $lockpath = get_config('system','lockpath');
        if ($lockpath != '') {
-               $pidfile = new pidfile($lockpath, 'poller.lck');
+               $pidfile = new pidfile($lockpath, 'poller');
                if($pidfile->is_already_running()) {
                        logger("poller: Already running");
+                       if ($pidfile->running_time() > 9*60) {
+                                $pidfile->kill();
+                                logger("poller: killed stale process");
+                                // Calling a new instance
+                                proc_run('php','include/poller.php');
+                        }
                        exit;
                }
        }
index 4825eb8a4d4cb0524e5e637804fe37d5f01ea566..bc3cc258cc8c0ce04f56b038ebc1deb8161ee242 100644 (file)
@@ -86,9 +86,15 @@ function queue_run(&$argv, &$argc){
 
        $lockpath = get_config('system','lockpath');
        if ($lockpath != '') {
-               $pidfile = new pidfile($lockpath, 'queue.lck');
+               $pidfile = new pidfile($lockpath, 'queue');
                if($pidfile->is_already_running()) {
                        logger("queue: Already running");
+                       if ($pidfile->running_time() > 9*60) {
+                               $pidfile->kill();
+                               logger("queue: killed stale process");
+                               // Calling a new instance
+                               proc_run('php',"include/queue.php");
+                       }
                        return;
                }
        }
index 585c063f567da09689fbc9567606f09a3abd4694..ab44073508c572dba438a74b753a8944f7e7a5eb 100644 (file)
@@ -710,7 +710,7 @@ die("ss");
                        );
                } else {
                        $r = q("SELECT `thread`.`iid` AS `item_id`, `thread`.`network` AS `item_network`, `contact`.`uid` AS `contact_uid`
-                               FROM $sql_table $sql_post_table INNER JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
+                               FROM $sql_table $sql_post_table STRAIGHT_JOIN `contact` ON `contact`.`id` = `thread`.`contact-id`
                                AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
                                WHERE `thread`.`uid` = %d AND `thread`.`visible` = 1 AND `thread`.`deleted` = 0
                                AND `thread`.`moderated` = 0