background_process.module 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  1. <?php
  2. /**
  3. * @file
  4. * This module implements a framework for calling funtions in the background.
  5. *
  6. * @code
  7. * $handle = background_process_start('mymodule_dosomething', $myvar1, $myvar2);
  8. * $handle = background_process_start(array('myclass', 'mystaticfunction'), $myvar1, $myvar2);
  9. * $handle = background_process_start(array($myobject, 'mymethod'), $myvar1, $myvar2);
  10. * $handle = background_process_start_locked('dontrunconcurrently', 'mymodule_dosomething', $myvar1, $myvar2);
  11. * @endcode
  12. *
  13. * For implementation of load balancing functionality:
  14. * @see hook_service_group()
  15. *
  16. * @todo Add functionality for killing keep alive requests?
  17. */
  18. include 'BackgroundProcess.class.php';
  19. define('BACKGROUND_PROCESS_SERVICE_TIMEOUT', 0);
  20. define('BACKGROUND_PROCESS_CONNECTION_TIMEOUT', 2);
  21. define('BACKGROUND_PROCESS_STREAM_TIMEOUT', 2);
  22. define('BACKGROUND_PROCESS_CLEANUP_AGE', 120);
  23. define('BACKGROUND_PROCESS_CLEANUP_AGE_RUNNING', 8 * 3600);
  24. define('BACKGROUND_PROCESS_CLEANUP_AGE_QUEUE', 86400);
  25. define('BACKGROUND_PROCESS_REDISPATCH_THRESHOLD', 10);
  26. define('BACKGROUND_PROCESS_STATUS_NONE', 0);
  27. define('BACKGROUND_PROCESS_STATUS_LOCKED', 1);
  28. define('BACKGROUND_PROCESS_STATUS_RUNNING', 2);
  29. define('BACKGROUND_PROCESS_STATUS_QUEUED', 3);
  30. // ---------- HOOKS ----------
  31. /**
  32. * Implements hook_menu().
  33. */
  34. function background_process_menu() {
  35. $items = array();
  36. $items['bgp-start/%/%'] = array(
  37. 'type' => MENU_CALLBACK,
  38. 'title' => 'Run background process',
  39. 'description' => 'Run background process',
  40. 'page callback' => 'background_process_service_start',
  41. 'page arguments' => array(1),
  42. 'access callback' => 'background_process_service_access',
  43. 'access arguments' => array(1, 2),
  44. );
  45. $items['background-process/unlock/%'] = array(
  46. 'type' => MENU_CALLBACK,
  47. 'title' => 'Unlock background process',
  48. 'description' => 'Unlock background process',
  49. 'page callback' => 'background_process_service_unlock',
  50. 'page arguments' => array(2),
  51. 'access arguments' => array('administer background process'),
  52. 'file' => 'background_process.admin.inc',
  53. );
  54. $items['background-process/check-token'] = array(
  55. 'type' => MENU_CALLBACK,
  56. 'title' => 'Check background process token',
  57. 'description' => 'Check background process token',
  58. 'page callback' => 'background_process_check_token',
  59. 'page arguments' => array(2),
  60. 'access callback' => TRUE,
  61. 'file' => 'background_process.pages.inc',
  62. );
  63. $items['admin/config/system/background-process/settings'] = array(
  64. 'type' => MENU_DEFAULT_LOCAL_TASK,
  65. 'title' => 'Settings',
  66. 'weight' => 1,
  67. );
  68. $items['admin/config/system/background-process'] = array(
  69. 'title' => 'Background process',
  70. 'description' => 'Administer background processes',
  71. 'page callback' => 'drupal_get_form',
  72. 'page arguments' => array('background_process_settings_form'),
  73. 'access arguments' => array('administer background process'),
  74. 'file' => 'background_process.admin.inc',
  75. );
  76. $items['admin/config/system/background-process/overview'] = array(
  77. 'type' => MENU_LOCAL_TASK,
  78. 'title' => 'Overview',
  79. 'description' => 'Administer background processes',
  80. 'page callback' => 'background_process_overview_page',
  81. 'access arguments' => array('administer background process'),
  82. 'file' => 'background_process.admin.inc',
  83. 'weight' => 3,
  84. );
  85. return $items;
  86. }
  87. /**
  88. * Implements hook_permission().
  89. */
  90. function background_process_permission() {
  91. return array(
  92. 'administer background process' => array(
  93. 'title' => t('Administer background processes'),
  94. 'description' => t('Perform administration tasks for background processes.'),
  95. ),
  96. );
  97. }
  98. /**
  99. * Implements hook_cron().
  100. */
  101. function background_process_cron() {
  102. // Don't use more than 120 seconds to unlock
  103. $expire = 120;
  104. @set_time_limit($expire);
  105. // Cleanup old handles
  106. $time = time();
  107. $msg = t('Never started (auto unlock due to timeout)');
  108. do {
  109. if (time() >= $_SERVER['REQUEST_TIME'] + $expire) {
  110. break;
  111. }
  112. $result = db_query_range("SELECT handle, start_stamp FROM {background_process} WHERE start_stamp < :start AND exec_status = :status", 0, 10, array(
  113. ':start' => $time - variable_get('background_process_cleanup_age', BACKGROUND_PROCESS_CLEANUP_AGE),
  114. ':status' => BACKGROUND_PROCESS_STATUS_LOCKED
  115. ));
  116. $handles = $result->fetchAllAssoc('handle', PDO::FETCH_ASSOC);
  117. foreach ($handles as $handle => $process) {
  118. // Unlock the process
  119. if (background_process_unlock($handle, $msg, $process['start_stamp'])) {
  120. drupal_set_message(t("%handle unlocked: !msg", array('%handle' => $handle, '!msg' => $msg)));
  121. }
  122. else {
  123. drupal_set_message(t("%handle could not be unlocked: !msg", array('%handle' => $handle, '!msg' => $msg)), 'error');
  124. }
  125. }
  126. } while (!empty($handles));
  127. // Cleanup stale requests
  128. $time = time();
  129. $msg = t('Never finished (auto unlock due to long run)');
  130. do {
  131. if (time() >= $_SERVER['REQUEST_TIME'] + $expire) {
  132. break;
  133. }
  134. $result = db_query_range("SELECT handle, start_stamp FROM {background_process} WHERE start_stamp < :start AND exec_status = :status", 0, 10, array(
  135. ':start' => $time - variable_get('background_process_cleanup_age_running', BACKGROUND_PROCESS_CLEANUP_AGE_RUNNING),
  136. ':status' => BACKGROUND_PROCESS_STATUS_RUNNING
  137. ));
  138. $handles = $result->fetchAllAssoc('handle', PDO::FETCH_ASSOC);
  139. foreach ($handles as $handle => $process) {
  140. // Unlock the process
  141. if (background_process_unlock($handle, $msg, $process['start_stamp'])) {
  142. drupal_set_message(t("%handle unlocked: !msg", array('%handle' => $handle, '!msg' => $msg)));
  143. }
  144. else {
  145. drupal_set_message(t("%handle could not be unlocked: !msg", array('%handle' => $handle, '!msg' => $msg)), 'error');
  146. }
  147. }
  148. } while (!empty($results));
  149. // Cleanup queued requests that were never processed
  150. $time = time();
  151. $msg = t('Never picked up by cron worker (auto unlock due to timeout)');
  152. do {
  153. if (time() >= $_SERVER['REQUEST_TIME'] + $expire) {
  154. break;
  155. }
  156. $result = db_query_range("SELECT handle, start_stamp FROM {background_process} WHERE start_stamp < :start AND exec_status = :status", 0, 10, array(
  157. ':start' => $time - variable_get('background_process_cleanup_age_queue', BACKGROUND_PROCESS_CLEANUP_AGE_QUEUE),
  158. ':status' => BACKGROUND_PROCESS_STATUS_QUEUED
  159. ));
  160. $handles = $result->fetchAllAssoc('handle', PDO::FETCH_ASSOC);
  161. foreach ($handles as $handle => $process) {
  162. // Unlock the process
  163. if (background_process_unlock($handle, $msg, $process['start_stamp'])) {
  164. drupal_set_message(t("%handle unlocked: !msg", array('%handle' => $handle, '!msg' => $msg)));
  165. }
  166. else {
  167. drupal_set_message(t("%handle could not be unlocked: !msg", array('%handle' => $handle, '!msg' => $msg)), 'error');
  168. }
  169. }
  170. } while (!empty($results));
  171. }
  172. /**
  173. * Implements hook_cron_alter().
  174. */
  175. function background_process_cron_alter(&$items) {
  176. $items['background_process_cron']['override_congestion_protection'] = TRUE;
  177. // Unlock background if too old.
  178. // @todo Move to some access handler or pre-execute?
  179. if ($process = background_process_get_process('uc:background_process_cron')) {
  180. if ($process->start + 30 < time()) {
  181. background_process_unlock($process->handle, t('Self unlocking stale lock'), $process->start);
  182. }
  183. }
  184. }
  185. /**
  186. * Implements hook_cronapi().
  187. */
  188. function background_process_cronapi($op, $job = NULL) {
  189. switch ($op) {
  190. case 'list':
  191. return array('background_process_cron' => t('Cleanup old process handles'));
  192. case 'rule':
  193. return '* * * * *';
  194. case 'configure':
  195. return 'admin/config/system/background-process';
  196. }
  197. }
  198. /**
  199. * Implements hook_service_group().
  200. *
  201. * Default load balancing using random.
  202. */
  203. function background_process_service_group() {
  204. $info = array();
  205. $info['methods']['background_process_service_group_random'] = t('Random');
  206. $info['methods']['background_process_service_group_round_robin'] = t('Pseudo round-robin');
  207. return $info;
  208. }
  209. /**
  210. * Load balancing based on random pick.
  211. */
  212. function background_process_service_group_random($service_group) {
  213. return $service_group['hosts'][rand(0, count($service_group['hosts']) - 1)];
  214. }
  215. /**
  216. * Round-robin load balancing based on random pick.
  217. */
  218. function background_process_service_group_round_robin($service_group) {
  219. static $idx = NULL;
  220. if (isset($idx)) {
  221. $idx = ($idx + 1) % count($service_group['hosts']);
  222. }
  223. else {
  224. $idx = rand(0, count($service_group['hosts']) - 1);
  225. }
  226. return $service_group['hosts'][$idx];
  227. }
  228. /**
  229. * Access handler for service call
  230. */
  231. function background_process_service_access($handle, $token) {
  232. // Setup service
  233. ignore_user_abort(TRUE);
  234. // Damn those slashes!
  235. $handle = rawurldecode($handle);
  236. $token = rawurldecode($token);
  237. // Ensure no session!
  238. drupal_save_session(FALSE);
  239. unset($_SESSION);
  240. $process = background_process_get_process($handle);
  241. if (!$process) {
  242. watchdog('bg_process', 'Unknown process: %handle', array('%handle' => $handle));
  243. return FALSE;
  244. }
  245. if ($token !== $process->token) {
  246. watchdog('bg_process', 'Invalid token: %token for handle: %handle', array('%token' => $token, '%handle' => $handle));
  247. return FALSE;
  248. }
  249. // Login as the user that requested the call
  250. if ($process->uid) {
  251. global $user;
  252. $user = user_load($process->uid);
  253. if (!$user) {
  254. // Invalid user!
  255. return FALSE;
  256. }
  257. }
  258. else {
  259. $user = drupal_anonymous_user();
  260. }
  261. return TRUE;
  262. }
  263. /**
  264. * Implements hook_init().
  265. */
  266. function background_process_init() {
  267. // Only determine if we're told to do so
  268. if (empty($_SESSION['background_process_determine_default_service_host'])) {
  269. return;
  270. }
  271. // Don't determine on check-token page, to avoid infinite loop
  272. if ($_GET['q'] == 'background-process/check-token') {
  273. return;
  274. }
  275. // Only determine when installation of site is finished
  276. if (variable_get('install_task', FALSE) != 'done') {
  277. return;
  278. }
  279. // Determine the default service host
  280. background_process_determine_and_save_default_service_host();
  281. unset($_SESSION['background_process_determine_default_service_host']);
  282. }
  283. /**
  284. * Implements hook_cron_queue_info().
  285. */
  286. function background_process_cron_queue_info() {
  287. $queues['background_process'] = array(
  288. 'worker callback' => '_background_process_queue',
  289. );
  290. $background_process_queues = variable_get('background_process_queues', array());
  291. foreach ($background_process_queues as $queue_name) {
  292. $queues['bgp:' . $queue_name] = array(
  293. 'worker callback' => '_background_process_queue',
  294. );
  295. }
  296. return $queues;
  297. }
  298. // ---------- HELPER FUNCTIONS ----------
  299. /**
  300. * Worker callback for processing queued function call
  301. */
  302. function _background_process_queue($item) {
  303. $oldhandle = background_process_current_handle();
  304. list ($handle, $token) = $item;
  305. if (background_process_service_access($handle, $token)) {
  306. try {
  307. background_process_service_execute(rawurldecode($handle), TRUE);
  308. background_process_current_handle($oldhandle);
  309. }
  310. catch (Exception $e) {
  311. background_process_current_handle($oldhandle);
  312. background_process_update_status(rawurldecode($handle), BACKGROUND_PROCESS_STATUS_QUEUED);
  313. throw $e;
  314. }
  315. }
  316. }
  317. /**
  318. * Get/set current handle.
  319. *
  320. * @staticvar $current_handle
  321. * @param $handle
  322. * @return string
  323. * Current handle.
  324. */
  325. function background_process_current_handle($handle = NULL) {
  326. static $current_handle = NULL;
  327. if (isset($handle)) {
  328. $current_handle = $handle;
  329. }
  330. return $current_handle;
  331. }
  332. /**
  333. * Get a unique handle based on a callback.
  334. *
  335. * @param $callback
  336. * Function to generate handle from.
  337. * @return string
  338. * Handle.
  339. */
  340. function background_process_generate_handle($callback) {
  341. return md5(serialize($callback) . ':' . microtime(TRUE) . ':' . rand(1, 5000));
  342. }
  343. /**
  344. * Start background process
  345. *
  346. * Calls the service handler through http passing function arguments as serialized data
  347. * Be aware that the callback will run in a new request
  348. *
  349. * @global string $base_url
  350. * Base URL for this Drupal request
  351. *
  352. * @param $callback
  353. * Function to call
  354. * @param $var [, $... ]]
  355. * Arbitrary number of variables to pass on to the callback
  356. * @return mixed
  357. * Handle on success, FALSE on failure
  358. */
  359. function background_process_start($callback /* [, $var [, $... ]] */) {
  360. $process = new BackgroundProcess();
  361. $args = func_get_args();
  362. array_splice($args, 0, 1);
  363. $result = $process->start($callback, $args);
  364. return $result ? $process->handle : $result;
  365. }
  366. /**
  367. * Start locked background process
  368. *
  369. * Calls the service handler through http passing function arguments as serialized data
  370. * Be aware that the callback will run in a new request
  371. *
  372. * @global string $base_url
  373. * Base URL for this Drupal request
  374. *
  375. * @param $handle
  376. * Handle to give background process
  377. * @param $callback
  378. * Function to call
  379. * @param $var [, $... ]]
  380. * Arbitrary number of variables to pass on to the callback
  381. * @return mixed
  382. * Handle on success, FALSE on failure
  383. */
  384. function background_process_start_locked($handle, $callback /* [, $var [, $... ]] */) {
  385. $process = new BackgroundProcess($handle);
  386. $args = func_get_args();
  387. array_splice($args, 0, 2);
  388. $result = $process->start($callback, $args);
  389. return $result ? $process->handle : $result;
  390. }
  391. /**
  392. * Start background process
  393. *
  394. * Queue the function call passing function arguments as serialized data
  395. * Be aware that the callback will run in a new request
  396. *
  397. * @global string $base_url
  398. * Base URL for this Drupal request
  399. *
  400. * @param $callback
  401. * Function to call
  402. * @param $var [, $... ]]
  403. * Arbitrary number of variables to pass on to the callback
  404. * @return mixed
  405. * Handle on success, FALSE on failure
  406. */
  407. function background_process_queue($callback /* [, $var [, $... ]] */) {
  408. $process = new BackgroundProcess();
  409. $args = func_get_args();
  410. array_splice($args, 0, 1);
  411. return $process->queue($callback, $args);
  412. }
  413. /**
  414. * Queue locked background process
  415. *
  416. * Queue the function call passing function arguments as serialized data
  417. * Be aware that the callback will run in a new request
  418. *
  419. * @global string $base_url
  420. * Base URL for this Drupal request
  421. *
  422. * @param $handle
  423. * Handle to give background process
  424. * @param $callback
  425. * Function to call
  426. * @param $var [, $... ]]
  427. * Arbitrary number of variables to pass on to the callback
  428. * @return mixed
  429. * Handle on success, FALSE on failure
  430. */
  431. function background_process_queue_locked($handle, $callback /* [, $var [, $... ]] */) {
  432. $process = new BackgroundProcess($handle);
  433. $args = func_get_args();
  434. array_splice($args, 0, 2);
  435. return $process->queue($callback, $args);
  436. }
  437. /**
  438. * Cleanup cache menu and ensure all locks are released (again).
  439. */
  440. function _background_process_cleanup_menu($cid) {
  441. cache_clear_all($cid, 'cache_menu');
  442. // Release locks in case cache_clear_all() set's a lock and lock_release_all()
  443. // has already been run.
  444. lock_release_all();
  445. }
  446. /**
  447. * Call the function requested by the service call
  448. *
  449. * @param $handle
  450. * Raw URL encoded handle of process
  451. * @param $return
  452. * Whether or not the function should return or exit.
  453. */
  454. function background_process_service_start($handle, $return = FALSE) {
  455. drupal_add_http_header('Content-Type', 'text/plain');
  456. // Let's clean up the mess the menu-router system leaves behind.
  457. $cid = 'menu_item:' . hash('sha256', $_GET['q']);
  458. drupal_register_shutdown_function('_background_process_cleanup_menu', $cid);
  459. // Setup service
  460. ignore_user_abort(TRUE);
  461. @set_time_limit(variable_get('background_process_service_timeout', BACKGROUND_PROCESS_SERVICE_TIMEOUT));
  462. // Damn those slashes!
  463. $handle = rawurldecode($handle);
  464. return background_process_service_execute($handle, $return);
  465. }
  466. /**
  467. * Execute the service
  468. *
  469. * @param $handle
  470. * Handle of process
  471. * @param $return
  472. * Whether or not the function should return or exit.
  473. */
  474. function background_process_service_execute($handle, $return = FALSE) {
  475. // @todo Add static caching? We've already loaded this previously in the access handler
  476. $process = background_process_get_process($handle);
  477. if (!$process) {
  478. watchdog('bg_process', 'Process not found for handle: %handle', array('%handle' => $handle), WATCHDOG_ERROR);
  479. if ($return) {
  480. return;
  481. }
  482. else {
  483. exit;
  484. }
  485. }
  486. $process->start_stamp = microtime(TRUE);
  487. try {
  488. $old_db = db_set_active('background_process');
  489. $claimed = db_update('background_process')
  490. ->fields(array(
  491. 'start_stamp' => sprintf("%.06f", $process->start_stamp),
  492. 'exec_status' => BACKGROUND_PROCESS_STATUS_RUNNING,
  493. ))
  494. ->condition('handle', $handle)
  495. ->condition('exec_status', array(BACKGROUND_PROCESS_STATUS_LOCKED, BACKGROUND_PROCESS_STATUS_QUEUED), 'IN')
  496. ->execute();
  497. db_set_active($old_db);
  498. if ($claimed) {
  499. $process->exec_status = BACKGROUND_PROCESS_STATUS_RUNNING;
  500. $process = BackgroundProcess::load($process);
  501. $process->sendMessage('claimed');
  502. background_process_current_handle($handle);
  503. }
  504. else {
  505. if ($return) {
  506. return;
  507. }
  508. else {
  509. exit;
  510. }
  511. }
  512. }
  513. catch (Exception $e) {
  514. db_set_active($old_db);
  515. throw $e;
  516. }
  517. // Make sure the process is removed when we're done
  518. if (!$return) {
  519. drupal_register_shutdown_function('background_process_remove_process', $process->handle, $process->start_stamp);
  520. }
  521. if (is_callable($process->callback)) {
  522. $old_db = NULL;
  523. try {
  524. if (!$return) {
  525. drupal_register_shutdown_function('module_invoke_all', 'background_process_shutdown', $process);
  526. }
  527. $callback = _background_process_callback_name($process->callback);
  528. $old_db = db_set_active('background_process');
  529. progress_initialize_progress($handle, "Background process '$callback' initialized");
  530. db_set_active($old_db);
  531. call_user_func_array($process->callback, $process->args);
  532. $old_db = db_set_active('background_process');
  533. progress_end_progress($handle, "Background process '$callback' finished");
  534. db_set_active($old_db);
  535. if ($return) {
  536. background_process_remove_process($process->handle, $process->start_stamp);
  537. module_invoke_all('background_process_shutdown', $process);
  538. }
  539. }
  540. catch (Exception $e) {
  541. // Exception occurred, switch back to proper db if necessary
  542. // and inform shutdown handlers.
  543. if ($old_db) {
  544. db_set_active($old_db);
  545. }
  546. if (!$return) {
  547. module_invoke_all('background_process_shutdown', $process, (string) $e);
  548. }
  549. throw $e;
  550. }
  551. }
  552. else {
  553. // Function not found
  554. watchdog('bg_process', 'Callback: %callback not found', array('%callback' => $process->callback), WATCHDOG_ERROR);
  555. }
  556. if ($return) {
  557. return;
  558. }
  559. else {
  560. exit;
  561. }
  562. }
  563. /**
  564. * Restart the current background process
  565. *
  566. * @return
  567. * Exception on fail, otherwise exit
  568. */
  569. function background_process_restart() {
  570. $args = func_get_args();
  571. call_user_func_array('background_process_keepalive', $args);
  572. exit;
  573. }
  574. /**
  575. * Keep the current background process alive
  576. * (meaning restart it when it exits)
  577. */
  578. function background_process_keepalive() {
  579. $args = func_get_args();
  580. $handle = background_process_current_handle();
  581. if (!$handle) {
  582. throw new Exception(t('Background process handle %handle not found', array('%handle' => $handle)));
  583. }
  584. $process = background_process_get_process($handle);
  585. if (!$process) {
  586. throw new Exception(t('Background process %handle not found', array('%handle' => $handle)));
  587. }
  588. drupal_register_shutdown_function('_background_process_restart', $process, $args);
  589. }
  590. /**
  591. * Check if the background process has started
  592. *
  593. * @param $handle
  594. * Handle of openend background process
  595. * @return boolean
  596. * TRUE if started, FALSE if not
  597. */
  598. function background_process_is_started($handle) {
  599. $old_db = db_set_active('background_process');
  600. $progress = progress_get_progress($handle);
  601. db_set_active($old_db);
  602. return !empty($progress);
  603. }
  604. /**
  605. * Check if the background process has finished
  606. *
  607. * @param $handle
  608. * Handle of openend background process
  609. * @return boolean
  610. * TRUE if finished, FALSE if not
  611. */
  612. function background_process_is_finished($handle) {
  613. $old_db = db_set_active('background_process');
  614. $progress = progress_get_progress($handle);
  615. db_set_active($old_db);
  616. return (empty($progress) || $progress->end);
  617. }
  618. /**
  619. * Set background process
  620. *
  621. * @global object $user
  622. * Current logged in user
  623. *
  624. * @param $handle
  625. * Handle of background process
  626. * @param $callback
  627. * Function of background process
  628. * @return boolean
  629. * TRUE if set, FALSE if not
  630. */
  631. function background_process_set_process($handle, $callback, $uid, $args, $token) {
  632. // Setup parameters
  633. $args = serialize($args);
  634. $callback = serialize($callback);
  635. // Get user
  636. if (!isset($uid)) {
  637. global $user;
  638. $uid = $user->uid;
  639. }
  640. try {
  641. $old_db = db_set_active('background_process');
  642. $result = db_update('background_process')
  643. ->fields(array(
  644. 'callback' => $callback,
  645. 'args' => $args,
  646. 'uid' => $uid,
  647. 'token' => $token
  648. ))
  649. ->condition('handle', $handle)
  650. ->execute();
  651. db_set_active($old_db);
  652. return $result;
  653. }
  654. catch (Exception $e) {
  655. db_set_active($old_db);
  656. throw $e;
  657. }
  658. }
  659. /**
  660. * Lock process
  661. *
  662. * @param $handle
  663. * Handle of background process
  664. * @return boolean
  665. * TRUE if locked, FALSE if lock could not be obtained
  666. */
  667. function background_process_lock_process($handle, $status = BACKGROUND_PROCESS_STATUS_LOCKED) {
  668. try {
  669. $old_db = db_set_active('background_process');
  670. db_insert('background_process')
  671. ->fields(array(
  672. 'handle' => $handle,
  673. 'start_stamp' => sprintf("%.06f", microtime(TRUE)),
  674. 'exec_status' => $status,
  675. ))
  676. ->execute();
  677. db_set_active($old_db);
  678. _background_process_ensure_cleanup($handle);
  679. return TRUE;
  680. }
  681. catch (Exception $e) {
  682. db_set_active($old_db);
  683. return FALSE;
  684. }
  685. }
  686. /**
  687. * Set status for background process
  688. */
  689. function background_process_update_status($handle, $status) {
  690. db_update('background_process')
  691. ->fields(array('exec_status' => $status))
  692. ->condition('handle', $handle)
  693. ->execute();
  694. }
  695. /**
  696. * Get background process
  697. *
  698. * @param $handle
  699. * Handle of background process
  700. * @return object
  701. * Background process
  702. */
  703. function background_process_get_process($handle) {
  704. try {
  705. $old_db = db_set_active('background_process');
  706. $result = db_select('background_process', 'bp')
  707. ->fields('bp', array('handle', 'callback', 'args', 'uid', 'token', 'service_host', 'start_stamp', 'exec_status'))
  708. ->condition('handle', $handle)
  709. ->execute()
  710. ->fetchObject();
  711. db_set_active($old_db);
  712. }
  713. catch (Exception $e) {
  714. db_set_active($old_db);
  715. throw $e;
  716. }
  717. if ($result) {
  718. $result->args = unserialize($result->args);
  719. $result->callback = unserialize($result->callback);
  720. $result->start = $result->start_stamp;
  721. $result->status = $result->exec_status;
  722. return $result;
  723. }
  724. return FALSE;
  725. }
  726. /**
  727. * Get background process
  728. *
  729. * @param $handle
  730. * Handle of background process
  731. * @return object
  732. * Background process
  733. */
  734. function background_process_get_processes($status = NULL) {
  735. $old_db = db_set_active('background_process');
  736. $result = db_select('background_process', 'bp')
  737. ->fields('bp', array('handle', 'callback', 'args', 'uid', 'token', 'service_host', 'start_stamp', 'exec_status'));
  738. if (isset($status)) {
  739. $result = $result->condition('bp.status', $status);
  740. }
  741. $result = $result->execute();
  742. $processes = array();
  743. while ($process = $result->fetchObject()) {
  744. $process->args = unserialize($process->args);
  745. $process->callback = unserialize($process->callback);
  746. $process->start = $process->start_stamp;
  747. $process->status = $process->exec_status;
  748. $processes[] = $process;
  749. }
  750. db_set_active($old_db);
  751. return $processes;
  752. }
  753. /**
  754. * Remove a background process
  755. *
  756. * @param $handle
  757. * Handle of background process
  758. * @return mixed
  759. * Number of handles deleted on success, FALSE on failure.
  760. */
  761. function background_process_remove_process($handle, $start = NULL) {
  762. $old_db = db_set_active('background_process');
  763. if (isset($start)) {
  764. $result = db_delete('background_process')
  765. ->condition('handle', $handle)
  766. ->condition('start_stamp', sprintf("%.06f", $start), '=')
  767. ->execute();
  768. }
  769. else {
  770. $result = db_delete('background_process')
  771. ->condition('handle', $handle)
  772. ->execute();
  773. }
  774. db_set_active($old_db);
  775. return $result;
  776. }
  777. /**
  778. * Unlock background process.
  779. *
  780. * @param $handle
  781. * Handle of process to unlock
  782. */
  783. function background_process_unlock($handle, $msg = NULL, $start = NULL) {
  784. $process = background_process_get_process($handle);
  785. if ($process && (!isset($start) || $start === $process->start)) {
  786. // Unlock the process
  787. if (background_process_remove_process($process->handle, $process->start)) {
  788. global $user;
  789. module_invoke_all('background_process_shutdown', $process, $msg ? $msg : t('Manually unlocked by !name', array('!name' => $user->name)));
  790. return TRUE;
  791. }
  792. }
  793. return FALSE;
  794. }
  795. /**
  796. * Set a service host for a background process.
  797. *
  798. * @param $handle
  799. * Background process handle
  800. * @param $service_host
  801. * Name of service host
  802. */
  803. function background_process_set_service_host($handle, $service_host) {
  804. try {
  805. $old_db = db_set_active('background_process');
  806. $result = db_update('background_process')
  807. ->fields(array(
  808. 'service_host' => $service_host ? $service_host : '',
  809. ))
  810. ->condition('handle', $handle)
  811. ->execute();
  812. db_set_active($old_db);
  813. return $result;
  814. }
  815. catch (Exception $e) {
  816. db_set_active($old_db);
  817. throw $e;
  818. }
  819. }
  820. /**
  821. * Get service hosts defined in the system.
  822. */
  823. function background_process_get_service_hosts() {
  824. global $base_url;
  825. $service_hosts = variable_get('background_process_service_hosts', array());
  826. $service_hosts += variable_get('background_process_derived_default_host', array(
  827. 'default' => array(
  828. 'base_url' => $base_url
  829. )
  830. ));
  831. return $service_hosts;
  832. }
  833. /**
  834. * Get service hosts defined in the system.
  835. */
  836. function background_process_get_service_groups() {
  837. $service_groups = variable_get('background_process_service_groups', array());
  838. $service_groups += array(
  839. 'default' => array(
  840. 'hosts' => array(variable_get('background_process_default_service_host', 'default')),
  841. ),
  842. );
  843. foreach ($service_groups as &$service_group) {
  844. $service_group += array(
  845. 'method' => 'background_process_service_group_round_robin'
  846. );
  847. }
  848. return $service_groups;
  849. }
  850. /**
  851. * Determine host for current installation.
  852. * Host is determined in the following order:
  853. * <server name>
  854. * <localhost>
  855. * <server ip>
  856. * @return array
  857. * Array of service host parameters for the default service host. FALSE if none could be determined.
  858. */
  859. function background_process_determine_default_service_host() {
  860. $token = md5(session_id() . md5(uniqid(mt_rand(), TRUE)) . md5(uniqid(mt_rand(), TRUE)));
  861. variable_set('background_process_token', $token);
  862. global $conf;
  863. $auth = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'] . '@' : '';
  864. $scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
  865. global $base_url;
  866. $url = parse_url($base_url);
  867. $path = empty($url['path']) ? '' : $url['path'];
  868. $candidates = array(
  869. array('base_url' => $base_url),
  870. array('base_url' => $scheme . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $path, 'http_host' => $_SERVER['HTTP_HOST']),
  871. array('base_url' => $scheme . '127.0.0.1:' . $_SERVER['SERVER_PORT'] . $path, 'http_host' => $_SERVER['HTTP_HOST']),
  872. array('base_url' => $scheme . (!array_key_exists('SERVER_ADDR', $_SERVER) ? $_SERVER['LOCAL_ADDR'] : $_SERVER['SERVER_ADDR']) . ':' . $_SERVER['SERVER_PORT'] . $path, 'http_host' => $_SERVER['HTTP_HOST']),
  873. array('base_url' => $scheme . $auth . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $path, 'http_host' => $_SERVER['HTTP_HOST']),
  874. array('base_url' => $scheme . $auth . '127.0.0.1:' . $_SERVER['SERVER_PORT'] . $path, 'http_host' => $_SERVER['HTTP_HOST']),
  875. array('base_url' => $scheme . $auth . (!array_key_exists('SERVER_ADDR', $_SERVER) ? $_SERVER['LOCAL_ADDR'] : $_SERVER['SERVER_ADDR']) . ':' . $_SERVER['SERVER_PORT'] . $path, 'http_host' => $_SERVER['HTTP_HOST']),
  876. );
  877. $found = NULL;
  878. foreach ($candidates as $i => $candidate) {
  879. $conf['background_process_service_hosts']['__test'] = $candidate;
  880. list($url, $headers) = background_process_build_request('background-process/check-token', '__test');
  881. if (empty($results[$url])) {
  882. $results[$url] = background_process_http_request($url, array('headers' => $headers, 'postpone' => TRUE, 'candidate' => $i, 'method' => 'POST'));
  883. }
  884. }
  885. background_process_http_request_process($results);
  886. foreach ($results as $result) {
  887. if ($result->code == 200) {
  888. if ($token === substr($result->data, 0, strlen($token))) {
  889. $found = $candidates[$result->options['candidate']];
  890. break;
  891. }
  892. }
  893. }
  894. if ($found) {
  895. return $found;
  896. }
  897. return FALSE;
  898. }
  899. /**
  900. * Build url and headers for http request
  901. * @param $url
  902. * Relative url for the request
  903. * @param $service_hostname
  904. * Name of service host, e.g. 'default'
  905. * @return array
  906. * array(url, headers)
  907. */
  908. function background_process_build_request($url, $service_hostname = NULL, $options = array()) {
  909. $service_hosts = background_process_get_service_hosts();
  910. if (!$service_hostname || empty($service_hosts[$service_hostname])) {
  911. $service_hostname = 'default';
  912. }
  913. $service_host = $service_hosts[$service_hostname];
  914. $options += array(
  915. 'absolute' => TRUE,
  916. 'base_url' => $service_host['base_url'],
  917. );
  918. $url = url($url, $options);
  919. $parsed = parse_url($url);
  920. $host = !empty($service_host['http_host']) ? $service_host['http_host'] : (isset($parsed['host']) ? $parsed['host'] : NULL);
  921. $headers = _background_process_request_headers();
  922. $headers = _background_process_filter_headers($headers);
  923. $headers['Host'] = $host;
  924. $headers['Connection'] = 'Close';
  925. if (isset($parsed['user'])) {
  926. $headers['Authorization'] = 'Basic ' . base64_encode($parsed['user'] . ':' . $parsed['pass']);
  927. }
  928. return array($url, $headers);
  929. }
  930. /**
  931. * Transform header array from key/value to strings.
  932. * @param $headers
  933. * array of headers in key/value style
  934. * @return array
  935. * array of strings.
  936. */
  937. function background_process_build_headers($headers) {
  938. $header = array();
  939. foreach ($headers as $key => $value) {
  940. $header[] = "$key: $value";
  941. }
  942. return $header;
  943. }
  944. /**
  945. * Perform an http request.
  946. * @see drupal_http_request()
  947. */
  948. function background_process_http_request($url, array $options = array()) {
  949. // Parse the URL and make sure we can handle the schema.
  950. $result = new stdClass();
  951. $result->url = $url;
  952. $result->options = $options;
  953. $result->code = NULL;
  954. $uri = @parse_url($url);
  955. $result->uri = $uri;
  956. if ($uri == FALSE) {
  957. $result->error = 'unable to parse URL';
  958. $result->code = -1001;
  959. return _background_process_http_request_result($result);
  960. }
  961. if (!isset($uri['scheme'])) {
  962. $result->error = 'missing schema';
  963. $result->code = -1002;
  964. return _background_process_http_request_result($result);
  965. }
  966. // Merge the default options.
  967. $options += array(
  968. 'headers' => array(),
  969. 'method' => 'GET',
  970. 'data' => NULL,
  971. 'max_redirects' => 3,
  972. 'timeout' => variable_get('background_process_connection_timeout', BACKGROUND_PROCESS_CONNECTION_TIMEOUT),
  973. 'context' => NULL,
  974. 'blocking' => FALSE,
  975. 'postpone' => FALSE,
  976. );
  977. // stream_socket_client() requires timeout to be a float.
  978. $options['timeout'] = (float) $options['timeout'];
  979. $host = NULL;
  980. switch ($uri['scheme']) {
  981. case 'http':
  982. case 'feed':
  983. $port = isset($uri['port']) ? $uri['port'] : 80;
  984. $socket = 'tcp://' . $uri['host'] . ':' . $port;
  985. // RFC 2616: "non-standard ports MUST, default ports MAY be included".
  986. // We don't add the standard port to prevent from breaking rewrite rules
  987. // checking the host that do not take into account the port number.
  988. $host = $uri['host'] . ($port != 80 ? ':' . $port : '');
  989. break;
  990. case 'https':
  991. // Note: Only works when PHP is compiled with OpenSSL support.
  992. $port = isset($uri['port']) ? $uri['port'] : 443;
  993. $socket = 'ssl://' . $uri['host'] . ':' . $port;
  994. $host = $uri['host'] . ($port != 443 ? ':' . $port : '');
  995. break;
  996. default:
  997. $result->error = 'invalid schema ' . $uri['scheme'];
  998. $result->code = -1003;
  999. return _background_process_http_request_result($result);
  1000. }
  1001. if (!empty($host) && empty($options['headers']['Host'])) {
  1002. $options['headers']['Host'] = $host;
  1003. }
  1004. $result->options = $options;
  1005. $result->socket = $socket;
  1006. $result->postponed = $options['postpone'];
  1007. if ($result->postponed) {
  1008. return $result;
  1009. }
  1010. else {
  1011. return background_process_http_request_initiate($result);
  1012. }
  1013. }
  1014. /**
  1015. * Initiate the http request.
  1016. */
  1017. function background_process_http_request_initiate(&$result) {
  1018. timer_start(__FUNCTION__);
  1019. $options = $result->options;
  1020. $socket = $result->socket;
  1021. $uri = $result->uri;
  1022. $result->start = microtime(TRUE);
  1023. $result->data_ready = TRUE;
  1024. if (empty($options['context'])) {
  1025. $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout']);
  1026. }
  1027. else {
  1028. // Create a stream with context. Allows verification of a SSL certificate.
  1029. $fp = @stream_socket_client($socket, $errno, $errstr, $options['timeout'], STREAM_CLIENT_CONNECT, $options['context']);
  1030. }
  1031. // Make sure the socket opened properly.
  1032. if (!$fp) {
  1033. // When a network error occurs, we use a negative number so it does not
  1034. // clash with the HTTP status codes.
  1035. $result->code = -$errno;
  1036. $result->error = trim($errstr) ? trim($errstr) : t('Error opening socket @socket', array('@socket' => $socket));
  1037. // Mark that this request failed. This will trigger a check of the web
  1038. // server's ability to make outgoing HTTP requests the next time that
  1039. // requirements checking is performed.
  1040. // See system_requirements()
  1041. // @fixme Disabled for Background Process
  1042. // variable_set('drupal_http_request_fails', TRUE);
  1043. return _background_process_http_request_result($result);
  1044. }
  1045. $result->fp = $fp;
  1046. // Construct the path to act on.
  1047. $path = isset($uri['path']) ? $uri['path'] : '/';
  1048. if (isset($uri['query'])) {
  1049. $path .= '?' . $uri['query'];
  1050. }
  1051. // Merge the default headers.
  1052. $options['headers'] += array(
  1053. 'User-Agent' => 'Drupal (+http://drupal.org/)',
  1054. );
  1055. // Only add Content-Length if we actually have any content or if it is a POST
  1056. // or PUT request. Some non-standard servers get confused by Content-Length in
  1057. // at least HEAD/GET requests, and Squid always requires Content-Length in
  1058. // POST/PUT requests.
  1059. $content_length = strlen($options['data']);
  1060. if ($content_length > 0 || $options['method'] == 'POST' || $options['method'] == 'PUT') {
  1061. $options['headers']['Content-Length'] = $content_length;
  1062. }
  1063. // If the server URL has a user then attempt to use basic authentication.
  1064. if (isset($uri['user'])) {
  1065. $options['headers']['Authorization'] = 'Basic ' . base64_encode($uri['user'] . (isset($uri['pass']) ? ':' . $uri['pass'] : ''));
  1066. }
  1067. // If the database prefix is being used by SimpleTest to run the tests in a copied
  1068. // database then set the user-agent header to the database prefix so that any
  1069. // calls to other Drupal pages will run the SimpleTest prefixed database. The
  1070. // user-agent is used to ensure that multiple testing sessions running at the
  1071. // same time won't interfere with each other as they would if the database
  1072. // prefix were stored statically in a file or database variable.
  1073. $test_info = &$GLOBALS['drupal_test_info'];
  1074. if (!empty($test_info['test_run_id'])) {
  1075. $options['headers']['User-Agent'] = drupal_generate_test_ua($test_info['test_run_id']);
  1076. }
  1077. $request = $options['method'] . ' ' . $path . " HTTP/1.0\r\n";
  1078. foreach ($options['headers'] as $name => $value) {
  1079. $request .= $name . ': ' . trim($value) . "\r\n";
  1080. }
  1081. $request .= "\r\n" . $options['data'];
  1082. $result->request = $request;
  1083. // Calculate how much time is left of the original timeout value.
  1084. $timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
  1085. if ($timeout > 0) {
  1086. stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
  1087. fwrite($fp, $request);
  1088. stream_set_blocking($fp, 0);
  1089. }
  1090. if (!empty($options['blocking'])) {
  1091. return background_process_http_request_get_response($result);
  1092. }
  1093. return $result;
  1094. }
  1095. /**
  1096. * Get response for an http request
  1097. */
  1098. function background_process_http_request_get_response(&$result) {
  1099. if ($result->postponed) {
  1100. $result->postponed = FALSE;
  1101. return background_process_http_request_initiate($result);
  1102. }
  1103. if (isset($result->code)) {
  1104. return $result;
  1105. }
  1106. $fp = $result->fp;
  1107. $options = $result->options;
  1108. timer_start(__FUNCTION__);
  1109. if (!empty($options['blocking'])) {
  1110. stream_set_blocking($fp, 1);
  1111. }
  1112. // Fetch response. Due to PHP bugs like http://bugs.php.net/bug.php?id=43782
  1113. // and http://bugs.php.net/bug.php?id=46049 we can't rely on feof(), but
  1114. // instead must invoke stream_get_meta_data() each iteration.
  1115. $info = stream_get_meta_data($fp);
  1116. $alive = !$info['eof'] && !$info['timed_out'];
  1117. while ($alive) {
  1118. // Calculate how much time is left of the original timeout value.
  1119. $timeout = $options['timeout'] - timer_read(__FUNCTION__) / 1000;
  1120. if ($timeout <= 0) {
  1121. $info['timed_out'] = TRUE;
  1122. break;
  1123. }
  1124. stream_set_timeout($fp, floor($timeout), floor(1000000 * fmod($timeout, 1)));
  1125. $chunk = fread($fp, 1024);
  1126. $result->response .= $chunk;
  1127. $result->data_ready = empty($chunk) ? FALSE : TRUE;
  1128. $info = stream_get_meta_data($fp);
  1129. $alive = !$info['eof'] && !$info['timed_out'];
  1130. if (empty($options['blocking'])) {
  1131. break;
  1132. }
  1133. }
  1134. if ($alive) {
  1135. return $result;
  1136. }
  1137. fclose($fp);
  1138. if ($info['timed_out']) {
  1139. $result->code = HTTP_REQUEST_TIMEOUT;
  1140. $result->error = 'request timed out';
  1141. return _background_process_http_request_result($result);
  1142. }
  1143. // Parse response headers from the response body.
  1144. // Be tolerant of malformed HTTP responses that separate header and body with
  1145. // \n\n or \r\r instead of \r\n\r\n.
  1146. list($response, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $result->response, 2);
  1147. $response = preg_split("/\r\n|\n|\r/", $response);
  1148. // Parse the response status line.
  1149. list($protocol, $code, $status_message) = explode(' ', trim(array_shift($response)), 3);
  1150. $result->protocol = $protocol;
  1151. $result->status_message = $status_message;
  1152. $result->headers = array();
  1153. // Parse the response headers.
  1154. while ($line = trim(array_shift($response))) {
  1155. list($name, $value) = explode(':', $line, 2);
  1156. $name = strtolower($name);
  1157. if (isset($result->headers[$name]) && $name == 'set-cookie') {
  1158. // RFC 2109: the Set-Cookie response header comprises the token Set-
  1159. // Cookie:, followed by a comma-separated list of one or more cookies.
  1160. $result->headers[$name] .= ',' . trim($value);
  1161. }
  1162. else {
  1163. $result->headers[$name] = trim($value);
  1164. }
  1165. }
  1166. $responses = array(
  1167. 100 => 'Continue',
  1168. 101 => 'Switching Protocols',
  1169. 200 => 'OK',
  1170. 201 => 'Created',
  1171. 202 => 'Accepted',
  1172. 203 => 'Non-Authoritative Information',
  1173. 204 => 'No Content',
  1174. 205 => 'Reset Content',
  1175. 206 => 'Partial Content',
  1176. 300 => 'Multiple Choices',
  1177. 301 => 'Moved Permanently',
  1178. 302 => 'Found',
  1179. 303 => 'See Other',
  1180. 304 => 'Not Modified',
  1181. 305 => 'Use Proxy',
  1182. 307 => 'Temporary Redirect',
  1183. 400 => 'Bad Request',
  1184. 401 => 'Unauthorized',
  1185. 402 => 'Payment Required',
  1186. 403 => 'Forbidden',
  1187. 404 => 'Not Found',
  1188. 405 => 'Method Not Allowed',
  1189. 406 => 'Not Acceptable',
  1190. 407 => 'Proxy Authentication Required',
  1191. 408 => 'Request Time-out',
  1192. 409 => 'Conflict',
  1193. 410 => 'Gone',
  1194. 411 => 'Length Required',
  1195. 412 => 'Precondition Failed',
  1196. 413 => 'Request Entity Too Large',
  1197. 414 => 'Request-URI Too Large',
  1198. 415 => 'Unsupported Media Type',
  1199. 416 => 'Requested range not satisfiable',
  1200. 417 => 'Expectation Failed',
  1201. 500 => 'Internal Server Error',
  1202. 501 => 'Not Implemented',
  1203. 502 => 'Bad Gateway',
  1204. 503 => 'Service Unavailable',
  1205. 504 => 'Gateway Time-out',
  1206. 505 => 'HTTP Version not supported',
  1207. );
  1208. // RFC 2616 states that all unknown HTTP codes must be treated the same as the
  1209. // base code in their class.
  1210. if (!isset($responses[$code])) {
  1211. $code = floor($code / 100) * 100;
  1212. }
  1213. $result->code = $code;
  1214. switch ($code) {
  1215. case 200: // OK
  1216. case 304: // Not modified
  1217. break;
  1218. case 301: // Moved permanently
  1219. case 302: // Moved temporarily
  1220. case 307: // Moved temporarily
  1221. $location = $result->headers['location'];
  1222. $options['timeout'] -= timer_read(__FUNCTION__) / 1000;
  1223. if ($options['timeout'] <= 0) {
  1224. $result->code = -1;
  1225. $result->error = 'request timed out';
  1226. }
  1227. elseif ($options['max_redirects']) {
  1228. // Redirect to the new location.
  1229. $options['max_redirects']--;
  1230. $result = background_process_http_request($location, $options);
  1231. if (empty($result->error)) {
  1232. background_process_http_request_get_response($result);
  1233. }
  1234. $result->redirect_code = $code;
  1235. }
  1236. if (!isset($result->redirect_url)) {
  1237. $result->redirect_url = $location;
  1238. }
  1239. break;
  1240. default:
  1241. $result->error = $status_message;
  1242. }
  1243. return _background_process_http_request_result($result);
  1244. }
  1245. function _background_process_http_request_result($result) {
  1246. if (isset($result->code)) {
  1247. if (empty($result->end)) {
  1248. $result->end = microtime(TRUE);
  1249. }
  1250. if (!empty($result->options['callback']) && is_callable($result->options['callback'])) {
  1251. call_user_func($result->options['callback'], $result);
  1252. }
  1253. }
  1254. return $result;
  1255. }
  1256. /**
  1257. * Process multiple http requests.
  1258. */
  1259. function background_process_http_request_process(&$results, $options = array()) {
  1260. $options += array(
  1261. 'timeout' => 30,
  1262. 'interval' => 0.01,
  1263. 'limit' => 0,
  1264. );
  1265. $interval = $options['interval'] * 1000000;
  1266. $expire = time() + $options['timeout'];
  1267. while ($results && time() < $expire) {
  1268. $cnt = 0;
  1269. $data_ready = FALSE;
  1270. foreach ($results as $i => &$result) {
  1271. if (isset($result->code)) {
  1272. continue;
  1273. }
  1274. background_process_http_request_get_response($result);
  1275. $data_ready = ($data_ready || $result->data_ready) ? TRUE : FALSE;
  1276. $cnt++;
  1277. if ($options['limit'] && $cnt >= $options['limit']) {
  1278. break;
  1279. }
  1280. }
  1281. if (!$cnt) {
  1282. break;
  1283. }
  1284. if (!$data_ready) {
  1285. usleep($interval);
  1286. }
  1287. }
  1288. }
  1289. /**
  1290. * Determines the default service host and stores it in the variable storage.
  1291. */
  1292. function background_process_determine_and_save_default_service_host() {
  1293. $host = background_process_determine_default_service_host();
  1294. if ($host) {
  1295. global $base_url;
  1296. drupal_set_message(t('Default service host determined at %base_url', array('%base_url' => _background_process_secure_url($host['base_url']))));
  1297. if ($host['base_url'] === $base_url) {
  1298. variable_del('background_process_derived_default_host');
  1299. }
  1300. else {
  1301. variable_set('background_process_derived_default_host', array('default' => $host));
  1302. drupal_set_message(t('Default service host differs from base url (%base_url). If migrating database to other sites or environments, you will need to either run "Determine default service host" again, or configure the default service host manually through settings.php', array('%base_url' => $base_url)), 'warning');
  1303. }
  1304. return TRUE;
  1305. }
  1306. else {
  1307. drupal_set_message(t('Could not determine default service host. Please configure background process in your settings.php'), 'error');
  1308. return FALSE;
  1309. }
  1310. }
  1311. // ---------- INTERNAL FUNCTIONS ----------
  1312. /**
  1313. * Ensure lock is removed at end of request
  1314. * @param $handle
  1315. * Handle of background process
  1316. * @param $remove
  1317. * If TRUE, don't remove when shutting down
  1318. */
  1319. function _background_process_ensure_cleanup($handle, $remove = FALSE) {
  1320. $handles = &drupal_static('background_process_handles_locked', NULL);
  1321. if (!isset($handles)) {
  1322. $handles = array();
  1323. drupal_register_shutdown_function('_background_process_cleanup_locks');
  1324. }
  1325. if ($remove) {
  1326. unset($handles[$handle]);
  1327. }
  1328. else {
  1329. $handles[$handle] = $handle;
  1330. }
  1331. }
  1332. /**
  1333. * Shutdown handler for removing locks
  1334. */
  1335. function _background_process_cleanup_locks() {
  1336. $handles = &drupal_static('background_process_handles_locked', NULL);
  1337. if (!empty($handles)) {
  1338. foreach ($handles as $handle) {
  1339. background_process_remove_process($handle);
  1340. }
  1341. }
  1342. }
  1343. /**
  1344. * Get string name of callback.
  1345. *
  1346. * @param $callback
  1347. * Callback can be either a string or an array.
  1348. * @return string
  1349. * The name of the callback, e.g. 'myfunction', 'myclass::mystaticmethod' or 'myclass->mymethod'.
  1350. */
  1351. function _background_process_callback_name($callback) {
  1352. if (is_array($callback)) {
  1353. if (is_object($callback[0])) {
  1354. $callback = get_class($callback[0]) . '->' . $callback[1];
  1355. }
  1356. else {
  1357. $callback = $callback[0] . '::' . $callback[1];
  1358. }
  1359. }
  1360. return $callback;
  1361. }
  1362. /**
  1363. * Get request headers
  1364. *
  1365. * @return array headers
  1366. */
  1367. function _background_process_request_headers() {
  1368. foreach ($_SERVER as $key => $value) {
  1369. if (substr($key, 0, 5) == 'HTTP_') {
  1370. $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))));
  1371. if (empty($headers[$key])) {
  1372. $headers[$key] = $value;
  1373. }
  1374. else {
  1375. $headers[$key] .= "; $value";
  1376. }
  1377. }
  1378. }
  1379. return $headers;
  1380. }
  1381. /**
  1382. * Remove headers we do not wish to pass on to the next request.
  1383. *
  1384. * @param $headers
  1385. * Headers to filter
  1386. * @return array
  1387. * Filtered headers
  1388. */
  1389. function _background_process_filter_headers($headers) {
  1390. $result = array();
  1391. if (empty($headers)) {
  1392. return $result;
  1393. }
  1394. foreach ($headers as $key => $value) {
  1395. if (!preg_match('/^(Connection|Keep-Alive|Proxy-Authenticate|Proxy-Authorization|TE|Trailers|Transfer-Encoding|Upgrade|Set-Cookie|Content-Length|Host|Accept-Encoding)$/i', $key)) {
  1396. $result[$key] = $value;
  1397. }
  1398. }
  1399. return $result;
  1400. }
  1401. /**
  1402. * Secure a URL by obfuscating the password if present.
  1403. *
  1404. * @param $url
  1405. * @return string
  1406. * URL
  1407. */
  1408. function _background_process_secure_url($url) {
  1409. $url = parse_url($url);
  1410. if (!empty($url['pass'])) {
  1411. $url['pass'] = 'XXXXXXXX';
  1412. }
  1413. return _background_process_unparse_url($url);
  1414. }
  1415. /**
  1416. * Reverse logic of parse_url().
  1417. *
  1418. * @param $parsed_url
  1419. * Array from parse_url()
  1420. * @return string
  1421. * URL
  1422. */
  1423. function _background_process_unparse_url($parsed_url) {
  1424. $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
  1425. $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
  1426. $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
  1427. $user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
  1428. $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
  1429. $pass = ($user || $pass) ? "$pass@" : '';
  1430. $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
  1431. $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
  1432. $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
  1433. return "$scheme$user$pass$host$port$path$query$fragment";
  1434. }
  1435. /**
  1436. * Shutdown handler for restarting background process.
  1437. *
  1438. * @param $process
  1439. * Background process object
  1440. * @param $args
  1441. * Array of arguments for callback
  1442. */
  1443. function _background_process_restart($process, $args = array()) {
  1444. $args = empty($args) ? $process->args : $args;
  1445. $new = BackgroundProcess::load($process);
  1446. $result = $new->start($process->callback, $args);
  1447. }