From eff4ad2e0ac70bc8a07a7c335d503197e7b26c70 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 1 Dec 2016 13:50:26 +0000 Subject: [PATCH 1/5] Spool items that couldn't be stored --- boot.php | 23 +++++++++++++++++++++++ include/items.php | 19 +++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/boot.php b/boot.php index 866e7165c7..d9e5b6529b 100644 --- a/boot.php +++ b/boot.php @@ -2364,6 +2364,29 @@ function get_lockpath() { return ""; } +function get_spoolpath() { + $spoolpath = get_config('system','spoolpath'); + if (($spoolpath != "") AND is_dir($spoolpath) AND is_writable($spoolpath)) + return($spoolpath); + + $temppath = get_temppath(); + + if ($temppath != "") { + $spoolpath = $temppath."/spool"; + + if (!is_dir($spoolpath)) + mkdir($spoolpath); + elseif (!is_writable($spoolpath)) + $spoolpath = $temppath; + + if (is_dir($spoolpath) AND is_writable($spoolpath)) { + set_config("system", "spoolpath", $spoolpath); + return($spoolpath); + } + } + return ""; +} + function get_temppath() { $a = get_app(); diff --git a/include/items.php b/include/items.php index e9354b62d3..8aa292b308 100644 --- a/include/items.php +++ b/include/items.php @@ -417,6 +417,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa $dsprsig = null; if (x($arr,'dsprsig')) { + $encoded_signature = $arr['dsprsig']; $dsprsig = json_decode(base64_decode($arr['dsprsig'])); unset($arr['dsprsig']); } @@ -846,14 +847,24 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa } else { // This can happen - for example - if there are locking timeouts. logger("Item wasn't stored - we quit here."); - q("COMMIT"); + q("ROLLBACK"); + + // Store the data into a spool file so that we can try again later. + + // At first we restore the Diaspora signature that we removed above. + if (isset($encoded_signature)) + $arr['dsprsig'] = $encoded_signature; + + // Now we store the data in the spool directory + $spool = get_spoolpath().'/'.round(microtime(true) * 10000).".msg"; + file_put_contents($spool, json_encode($arr)); return 0; } if ($current_post == 0) { // This is one of these error messages that never should occur. logger("couldn't find created item - we better quit now."); - q("COMMIT"); + q("ROLLBACK"); return 0; } @@ -868,7 +879,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa if (!dbm::is_result($r)) { // This shouldn't happen, since COUNT always works when the database connection is there. logger("We couldn't count the stored entries. Very strange ..."); - q("COMMIT"); + q("ROLLBACK"); return 0; } @@ -883,7 +894,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa } elseif ($r[0]["entries"] == 0) { // This really should never happen since we quit earlier if there were problems. logger("Something is terribly wrong. We haven't found our created entry."); - q("COMMIT"); + q("ROLLBACK"); return 0; } From 6845775ff929d36c8e84cf04660d5aebff70b6ed Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 1 Dec 2016 20:53:18 +0000 Subject: [PATCH 2/5] Store spooled data --- include/items.php | 5 +++-- include/poller.php | 3 +++ mod/worker.php | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/items.php b/include/items.php index 8aa292b308..6d45550a4f 100644 --- a/include/items.php +++ b/include/items.php @@ -846,7 +846,6 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa } } else { // This can happen - for example - if there are locking timeouts. - logger("Item wasn't stored - we quit here."); q("ROLLBACK"); // Store the data into a spool file so that we can try again later. @@ -856,8 +855,10 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa $arr['dsprsig'] = $encoded_signature; // Now we store the data in the spool directory - $spool = get_spoolpath().'/'.round(microtime(true) * 10000).".msg"; + $file = 'item-'.round(microtime(true) * 10000).".msg"; + $spool = get_spoolpath().'/'.$file; file_put_contents($spool, json_encode($arr)); + logger("Item wasn't stored - Item was spooled into file ".$file, LOGGER_DEBUG); return 0; } diff --git a/include/poller.php b/include/poller.php index b3a66fc389..925de3fe5b 100644 --- a/include/poller.php +++ b/include/poller.php @@ -556,6 +556,9 @@ function clear_worker_processes() { function poller_run_cron() { logger('Add cron entries', LOGGER_DEBUG); + // Check for spooled items + proc_run(PRIORITY_HIGH, "include/spool_post.php"); + // Run the cron job that calls all other jobs proc_run(PRIORITY_MEDIUM, "include/cron.php"); diff --git a/mod/worker.php b/mod/worker.php index aebffed744..c202a28d64 100644 --- a/mod/worker.php +++ b/mod/worker.php @@ -14,8 +14,10 @@ function worker_init($a){ return; } - // We don't need the following lines if we can execute background jobs + // We don't need the following lines if we can execute background jobs. + // So we just wake up the worker if it sleeps. if (function_exists("proc_open")) { + call_worker_if_idle(); return; } From 13150c09a56de662677ee17a1614a2ee55931890 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 1 Dec 2016 21:11:52 +0000 Subject: [PATCH 3/5] File was missing --- include/spool_post.php | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 include/spool_post.php diff --git a/include/spool_post.php b/include/spool_post.php new file mode 100644 index 0000000000..c42928a672 --- /dev/null +++ b/include/spool_post.php @@ -0,0 +1,49 @@ + From c97306e734227cf0499a6043d6d1ad2458b6f258 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 1 Dec 2016 22:50:07 +0000 Subject: [PATCH 4/5] Standards ... --- boot.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/boot.php b/boot.php index 9a766d41dc..4974512248 100644 --- a/boot.php +++ b/boot.php @@ -2365,20 +2365,27 @@ function get_lockpath() { return ""; } +/** + * @brief Returns the path where spool files are stored + * + * @return string Spool path + */ function get_spoolpath() { $spoolpath = get_config('system','spoolpath'); - if (($spoolpath != "") AND is_dir($spoolpath) AND is_writable($spoolpath)) + if (($spoolpath != "") AND is_dir($spoolpath) AND is_writable($spoolpath)) { return($spoolpath); + } $temppath = get_temppath(); if ($temppath != "") { $spoolpath = $temppath."/spool"; - if (!is_dir($spoolpath)) + if (!is_dir($spoolpath)) { mkdir($spoolpath); - elseif (!is_writable($spoolpath)) + } elseif (!is_writable($spoolpath)) { $spoolpath = $temppath; + } if (is_dir($spoolpath) AND is_writable($spoolpath)) { set_config("system", "spoolpath", $spoolpath); From 6619e0791d6b8975e09d82dd823c630c5459b479 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 1 Dec 2016 22:52:35 +0000 Subject: [PATCH 5/5] Much more standards --- include/items.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/items.php b/include/items.php index 6d45550a4f..0445c4490c 100644 --- a/include/items.php +++ b/include/items.php @@ -851,8 +851,9 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa // Store the data into a spool file so that we can try again later. // At first we restore the Diaspora signature that we removed above. - if (isset($encoded_signature)) + if (isset($encoded_signature)) { $arr['dsprsig'] = $encoded_signature; + } // Now we store the data in the spool directory $file = 'item-'.round(microtime(true) * 10000).".msg";