Merge pull request #2986 from annando/1612-spool

Spool items that weren't stored before
This commit is contained in:
Tobias Diekershoff 2016-12-02 17:14:10 +01:00 committed by GitHub
commit e20e64390b
5 changed files with 103 additions and 6 deletions

View File

@ -2365,6 +2365,36 @@ function get_lockpath() {
return ""; 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)) {
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() { function get_temppath() {
$a = get_app(); $a = get_app();

View File

@ -417,6 +417,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
$dsprsig = null; $dsprsig = null;
if (x($arr,'dsprsig')) { if (x($arr,'dsprsig')) {
$encoded_signature = $arr['dsprsig'];
$dsprsig = json_decode(base64_decode($arr['dsprsig'])); $dsprsig = json_decode(base64_decode($arr['dsprsig']));
unset($arr['dsprsig']); unset($arr['dsprsig']);
} }
@ -845,15 +846,27 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
} }
} else { } else {
// This can happen - for example - if there are locking timeouts. // This can happen - for example - if there are locking timeouts.
logger("Item wasn't stored - we quit here."); q("ROLLBACK");
q("COMMIT");
// 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
$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; return 0;
} }
if ($current_post == 0) { if ($current_post == 0) {
// This is one of these error messages that never should occur. // This is one of these error messages that never should occur.
logger("couldn't find created item - we better quit now."); logger("couldn't find created item - we better quit now.");
q("COMMIT"); q("ROLLBACK");
return 0; return 0;
} }
@ -868,7 +881,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
if (!dbm::is_result($r)) { if (!dbm::is_result($r)) {
// This shouldn't happen, since COUNT always works when the database connection is there. // This shouldn't happen, since COUNT always works when the database connection is there.
logger("We couldn't count the stored entries. Very strange ..."); logger("We couldn't count the stored entries. Very strange ...");
q("COMMIT"); q("ROLLBACK");
return 0; return 0;
} }
@ -883,7 +896,7 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa
} elseif ($r[0]["entries"] == 0) { } elseif ($r[0]["entries"] == 0) {
// This really should never happen since we quit earlier if there were problems. // 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."); logger("Something is terribly wrong. We haven't found our created entry.");
q("COMMIT"); q("ROLLBACK");
return 0; return 0;
} }

View File

@ -556,6 +556,9 @@ function clear_worker_processes() {
function poller_run_cron() { function poller_run_cron() {
logger('Add cron entries', LOGGER_DEBUG); 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 // Run the cron job that calls all other jobs
proc_run(PRIORITY_MEDIUM, "include/cron.php"); proc_run(PRIORITY_MEDIUM, "include/cron.php");

49
include/spool_post.php Normal file
View File

@ -0,0 +1,49 @@
<?php
/**
* @file include/spool_post.php
* @brief Posts items that wer spooled because they couldn't be posted.
*/
require_once("boot.php");
require_once("include/items.php");
function spool_post_run($argv, $argc) {
global $a, $db;
if (is_null($a)) {
$a = new App;
}
if (is_null($db)) {
@include(".htconfig.php");
require_once("include/dba.php");
$db = new dba($db_host, $db_user, $db_pass, $db_data);
unset($db_host, $db_user, $db_pass, $db_data);
}
load_config('config');
load_config('system');
$path = get_spoolpath();
if (is_writable($path)){
if ($dh = opendir($path)) {
while (($file = readdir($dh)) !== false) {
$fullfile = $path."/".$file;
if (filetype($fullfile) != "file") {
continue;
}
$arr = json_decode(file_get_contents($fullfile), true);
$result = item_store($arr);
logger("Spool file ".$file." stored: ".$result, LOGGER_DEBUG);
unlink($fullfile);
}
closedir($dh);
}
}
}
if (array_search(__file__, get_included_files()) === 0) {
spool_post_run($_SERVER["argv"], $_SERVER["argc"]);
killme();
}
?>

View File

@ -14,8 +14,10 @@ function worker_init($a){
return; 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")) { if (function_exists("proc_open")) {
call_worker_if_idle();
return; return;
} }