/templates/hooks/shared.php

https://github.com/mrsinguyen/drupalorg-git · PHP · 60 lines · 40 code · 6 blank · 14 comment · 2 complexity · acd9a3c916d8f6f8f95218f47bf81cc4 MD5 · raw file

  1. <?php
  2. /**
  3. * @file shared.php
  4. *
  5. * Shared functionality needed by all our git hooks.
  6. */
  7. // Provide a constant representing the null object name.
  8. define('GIT_NULL_REV', '0000000000000000000000000000000000000000');
  9. global $repo_json, $pusher_uid, $repo_id;
  10. $repo_json = getenv('VERSION_CONTROL_VCS_AUTH_DATA');
  11. $pusher_uid = getenv('VERSION_CONTROL_GIT_UID');
  12. $repo_id = getenv('VERSION_CONTROL_GIT_REPO_ID');
  13. // Queues we may want to communicate with.
  14. global $queues;
  15. $queues = array(
  16. 'versioncontrol_git_repo_parsing' => array(
  17. 'beanstalkd host' => 'localhost',
  18. 'beanstalkd port' => 11300,
  19. ),
  20. 'versioncontrol_repomgr' => array(
  21. 'beanstalkd host' => 'localhost',
  22. 'beanstalkd port' => 11300,
  23. ),
  24. );
  25. // Path to error log.
  26. global $git_hook_error_log;
  27. $git_hook_error_log = '/var/log/git/githook-err.log';
  28. /**
  29. * Init our pheanstalk connection. We don't do this by default so that hooks
  30. * which don't need it can be as fast as possible.
  31. */
  32. function _githooks_init_pheanstalk() {
  33. // We're assuming the pheanstalk library lives in php's include_path.
  34. require_once 'pheanstalk/pheanstalk_init.php';
  35. }
  36. function _githooks_enqueue_job($queue_name, $payload, $priority = 1024) {
  37. _githooks_init_pheanstalk();
  38. global $git_hook_error_log, $queues;
  39. try {
  40. $pheanstalk = new Pheanstalk($queues[$queue_name]['beanstalkd host'], $queues[$queue_name]['beanstalkd port']);
  41. // Build a stdClass item wrapper to make beanstalkd.module happy
  42. $item = new stdClass();
  43. $item->name = $queue_name;
  44. $item->data = $payload;
  45. $pheanstalk->useTube($queue_name);
  46. $pheanstalk->put(serialize($item), $priority);
  47. }
  48. catch (Exception $e) {
  49. if (file_exists($git_hook_error_log) && is_writable($git_hook_error_log)) {
  50. $message = date('Y-m-d:H-i-s') . ": Git 'post-recieve' error, queue '$queue_name': " . $e->getMessage() . print_r($data, TRUE);
  51. file_put_contents($git_hook_error_log, $message, FILE_APPEND);
  52. }
  53. }
  54. }