diff options
author | Friendika <info@friendika.com> | 2011-06-03 01:16:17 -0700 |
---|---|---|
committer | Friendika <info@friendika.com> | 2011-06-03 01:16:17 -0700 |
commit | a27391c33a6b2d3828182ca5f8e8b65998cf47a7 (patch) | |
tree | c49d6ddd2e5689a3493cda27dadcc549748e12ee | |
parent | f2bdcb19e7f2748d3c368ab11deeb98d63fedca7 (diff) | |
download | volse-hubzilla-a27391c33a6b2d3828182ca5f8e8b65998cf47a7.tar.gz volse-hubzilla-a27391c33a6b2d3828182ca5f8e8b65998cf47a7.tar.bz2 volse-hubzilla-a27391c33a6b2d3828182ca5f8e8b65998cf47a7.zip |
facebook queueing on failure, sync update.php with boot.php update version to avoid race condition
-rw-r--r-- | addon/facebook/facebook.php | 84 | ||||
-rw-r--r-- | boot.php | 21 | ||||
-rw-r--r-- | database.sql | 1 | ||||
-rw-r--r-- | include/queue.php | 22 | ||||
-rw-r--r-- | include/queue_fn.php | 16 | ||||
-rw-r--r-- | update.php | 9 |
6 files changed, 119 insertions, 34 deletions
diff --git a/addon/facebook/facebook.php b/addon/facebook/facebook.php index 3cc40330c..7ee2b1b08 100644 --- a/addon/facebook/facebook.php +++ b/addon/facebook/facebook.php @@ -335,18 +335,20 @@ function facebook_content(&$a) { } function facebook_install() { - register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook'); - register_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets'); - register_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings'); - register_hook('cron', 'addon/facebook/facebook.php', 'facebook_cron'); + register_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook'); + register_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets'); + register_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings'); + register_hook('cron', 'addon/facebook/facebook.php', 'facebook_cron'); + register_hook('queue_predeliver', 'addon/facebook/facebook.php', 'fb_queue_hook'); } function facebook_uninstall() { - unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook'); - unregister_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets'); - unregister_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings'); - unregister_hook('cron', 'addon/facebook/facebook.php', 'facebook_cron'); + unregister_hook('post_local_end', 'addon/facebook/facebook.php', 'facebook_post_hook'); + unregister_hook('jot_networks', 'addon/facebook/facebook.php', 'facebook_jot_nets'); + unregister_hook('plugin_settings', 'addon/facebook/facebook.php', 'facebook_plugin_settings'); + unregister_hook('cron', 'addon/facebook/facebook.php', 'facebook_cron'); + unregister_hook('queue_predeliver', 'addon/facebook/facebook.php', 'fb_queue_hook'); } @@ -635,9 +637,19 @@ function facebook_post_hook(&$a,&$b) { ); } else { - // FIXME queue the message so we can attempt to redeliver, see include/notifier.php and include/queue.php - if(! $likes) - notice( t('Facebook delivery failed.') . EOL); + if(! $likes) { + $s = serialize(array('url' => $url, 'item' => $b, 'post' => $postvars)); + q("INSERT INTO `queue` ( `network`, `cid`, `created`, `last`, `content`) + VALUES ( '%s', '%s', '%s', '%s') ", + dbesc(NETWORK_FACEBOOK), + intval($a->contact), + dbesc(datetime_convert()), + dbesc(datetime_convert()), + dbesc($s) + ); + + notice( t('Facebook post failed. Queued for retry.') . EOL); + } } logger('Facebook post returns: ' . $x, LOGGER_DEBUG); @@ -648,6 +660,56 @@ function facebook_post_hook(&$a,&$b) { } +function fb_queue_hook(&$a,&$b) { + + require_once('include/queue_fn.php'); + if((! is_array($b)) || (! count($b))) + return; + foreach($b as $x) { + if($b['network'] !== NETWORK_FACEBOOK) + continue; + $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid` + WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1", + intval($x['cid']) + ); + if(! count($r)) + continue; + + $user = $r[0]; + + $appid = get_config('facebook', 'appid' ); + $secret = get_config('facebook', 'appsecret' ); + + if($appid && $secret) { + $fb_post = intval(get_pconfig($user['uid'],'facebook','post')); + $fb_token = get_pconfig($user['uid'],'facebook','access_token'); + + if($fb_post && $fb_token) { + logger('facebook_queue: able to post'); + require_once('library/facebook.php'); + + $z = unserialize($x['content']); + $item = $z['item']; + $j = post_url($z['url'],$z['post']); + + $retj = json_decode($j); + if($retj->id) { + q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1", + dbesc('fb::' . $retj->id), + intval($item['id']) + ); + logger('facebook queue: success: ' . $j); + remove_queue_item($x['id']); + } + else { + logger('facebook_queue: failed: ' . $j); + update_queue_time($x['id']); + } + } + } + } +} + function fb_consume_all($uid) { require_once('include/items.php'); @@ -6,7 +6,7 @@ ini_set('pcre.backtrack_limit', 250000); define ( 'FRIENDIKA_VERSION', '2.2.999' ); define ( 'DFRN_PROTOCOL_VERSION', '2.21' ); -define ( 'DB_UPDATE_VERSION', 1059 ); +define ( 'DB_UPDATE_VERSION', 1060 ); define ( 'EOL', "<br />\r\n" ); define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' ); @@ -481,17 +481,26 @@ function check_config(&$a) { $stored = intval($build); $current = intval(DB_UPDATE_VERSION); if(($stored < $current) && file_exists('update.php')) { + // We're reporting a different version than what is currently installed. // Run any existing update scripts to bring the database up to current. require_once('update.php'); - for($x = $stored; $x < $current; $x ++) { - if(function_exists('update_' . $x)) { - $func = 'update_' . $x; - $func($a); + + // make sure that boot.php and update.php are the same release, we might be + // updating right this very second and the correct version of the update.php + // file may not be here yet. This can happen on a very busy site. + + if(DB_UPDATE_VERSION == UPDATE_VERSION) { + + for($x = $stored; $x < $current; $x ++) { + if(function_exists('update_' . $x)) { + $func = 'update_' . $x; + $func($a); + } } + set_config('system','build', DB_UPDATE_VERSION); } - set_config('system','build', DB_UPDATE_VERSION); } } diff --git a/database.sql b/database.sql index 8948e9b06..7c26af341 100644 --- a/database.sql +++ b/database.sql @@ -440,6 +440,7 @@ PRIMARY KEY ( `id` ) CREATE TABLE IF NOT EXISTS `queue` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `cid` INT NOT NULL , +`network` CHAR( 32 ) NOT NULL, `created` DATETIME NOT NULL , `last` DATETIME NOT NULL , `content` MEDIUMTEXT NOT NULL diff --git a/include/queue.php b/include/queue.php index ff280cb53..cc36b2f62 100644 --- a/include/queue.php +++ b/include/queue.php @@ -1,20 +1,6 @@ <?php require_once("boot.php"); - -function update_queue_time($id) { - logger('queue: requeue item ' . $id); - q("UPDATE `queue` SET `last` = '%s' WHERE `id` = %d LIMIT 1", - dbesc(datetime_convert()), - intval($id) - ); -} - -function remove_queue_item($id) { - logger('queue: remove queue item ' . $id); - q("DELETE FROM `queue` WHERE `id` = %d LIMIT 1", - intval($id) - ); -} +require_once('include/queue_fn.php'); function queue_run($argv, $argc){ global $a, $db; @@ -58,6 +44,10 @@ function queue_run($argv, $argc){ if(! count($r)){ return; } + + call_hooks('queue_predeliver', $a, $r); + + // delivery loop require_once('include/salmon.php'); @@ -69,6 +59,7 @@ function queue_run($argv, $argc){ if(! count($qi)) continue; + $c = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1", intval($qi[0]['cid']) ); @@ -121,7 +112,6 @@ function queue_run($argv, $argc){ } break; default: - $a = get_app(); $params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false); call_hooks('queue_deliver', $a, $params); diff --git a/include/queue_fn.php b/include/queue_fn.php new file mode 100644 index 000000000..bc47ceffd --- /dev/null +++ b/include/queue_fn.php @@ -0,0 +1,16 @@ +<?php + +function update_queue_time($id) { + logger('queue: requeue item ' . $id); + q("UPDATE `queue` SET `last` = '%s' WHERE `id` = %d LIMIT 1", + dbesc(datetime_convert()), + intval($id) + ); +} + +function remove_queue_item($id) { + logger('queue: remove queue item ' . $id); + q("DELETE FROM `queue` WHERE `id` = %d LIMIT 1", + intval($id) + ); +} diff --git a/update.php b/update.php index a53e4df78..be1b184ae 100644 --- a/update.php +++ b/update.php @@ -1,5 +1,7 @@ <?php +define( 'UPDATE_VERSION' , 1060 ); + /** * * update.php - automatic system update @@ -28,7 +30,7 @@ * 2. Update this file by adding a new function at the end with the number of the current DB_UPDATE_VERSION. * This function should modify the current database schema and perform any other steps necessary * to ensure that upgrade is silent and free from requiring interaction. - * 3. Increment the DB_UPDATE_VERSION in boot.php + * 3. Increment the DB_UPDATE_VERSION in boot.php *AND* the UPDATE_VERSION in this file to match it * 4. TEST the upgrade prior to checkin and filing a pull request. * */ @@ -499,3 +501,8 @@ function update_1057() { function update_1058() { q("ALTER TABLE `item` ADD `event-id` INT NOT NULL AFTER `resource-id` "); } + +function update_1059() { + q("ALTER TABLE `queue` ADD `network` CHAR( 32 ) NOT NULL AFTER `cid` "); +} + |