imdaemon.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - the distributed open-source microblogging tool
  5. * Copyright (C) 2008, 2009, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  21. $shortoptions = 'fi::a';
  22. $longoptions = array('id::', 'foreground', 'all');
  23. $helptext = <<<END_OF_IM_HELP
  24. Daemon script for receiving new notices from IM users.
  25. -i --id Identity (default none)
  26. -a --all Handle XMPP for all local sites
  27. (requires Stomp queue handler, status_network setup)
  28. -f --foreground Stay in the foreground (default background)
  29. END_OF_IM_HELP;
  30. require_once INSTALLDIR.'/scripts/commandline.inc';
  31. class ImDaemon extends SpawningDaemon
  32. {
  33. protected $allsites = false;
  34. function __construct($id=null, $daemonize=true, $threads=1, $allsites=false)
  35. {
  36. if ($threads != 1) {
  37. // This should never happen. :)
  38. throw new Exception("IMDaemon can must run single-threaded");
  39. }
  40. parent::__construct($id, $daemonize, $threads);
  41. $this->allsites = $allsites;
  42. }
  43. function runThread()
  44. {
  45. common_log(LOG_INFO, 'Waiting to listen to IM connections and queues');
  46. $master = new ImMaster($this->get_id(), $this->processManager());
  47. $master->init($this->allsites);
  48. $master->service();
  49. common_log(LOG_INFO, 'terminating normally');
  50. return $master->respawn ? self::EXIT_RESTART : self::EXIT_SHUTDOWN;
  51. }
  52. }
  53. class ImMaster extends IoMaster
  54. {
  55. protected $processManager;
  56. function __construct($id, $processManager)
  57. {
  58. parent::__construct($id);
  59. $this->processManager = $processManager;
  60. }
  61. /**
  62. * Initialize IoManagers for the currently configured site
  63. * which are appropriate to this instance.
  64. */
  65. function initManagers()
  66. {
  67. $classes = array();
  68. if (Event::handle('StartImDaemonIoManagers', array(&$classes))) {
  69. $qm = QueueManager::get();
  70. $qm->setActiveGroup('im');
  71. $classes[] = $qm;
  72. $classes[] = $this->processManager;
  73. }
  74. Event::handle('EndImDaemonIoManagers', array(&$classes));
  75. foreach ($classes as $class) {
  76. $this->instantiate($class);
  77. }
  78. }
  79. }
  80. if (have_option('i', 'id')) {
  81. $id = get_option_value('i', 'id');
  82. } else if (count($args) > 0) {
  83. $id = $args[0];
  84. } else {
  85. $id = null;
  86. }
  87. $foreground = have_option('f', 'foreground');
  88. $all = have_option('a') || have_option('--all');
  89. $daemon = new ImDaemon($id, !$foreground, 1, $all);
  90. $daemon->runOnce();