Irc_waiting_message.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Table Definition for irc_waiting_message
  4. */
  5. require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
  6. class Irc_waiting_message extends Managed_DataObject {
  7. public $__table = 'irc_waiting_message'; // table name
  8. public $id; // int primary_key not_null auto_increment
  9. public $data; // blob not_null
  10. public $prioritise; // tinyint(1) not_null
  11. public $attempts; // int not_null
  12. public $claimed; // datetime()
  13. public $created; // datetime() not_null
  14. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  15. public static function schemaDef()
  16. {
  17. return array(
  18. 'fields' => array(
  19. 'id' => array('type' => 'serial', 'not null' => true, 'description' => 'Unique ID for entry'),
  20. 'data' => array('type' => 'blob', 'not null' => true, 'description' => 'data blob'),
  21. 'prioritise' => array('type' => 'int', 'size' => 'tiny', 'description' => 'tinyint priority value'),
  22. 'attempts' => array('type' => 'int', 'not null' => true, 'description' => 'attempts count'),
  23. 'claimed' => array('type' => 'datetime', 'description' => 'date this irc message was claimed'),
  24. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  25. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  26. ),
  27. 'primary key' => array('id'),
  28. 'indexes' => array(
  29. 'irc_waiting_message_prioritise_idx' => array('prioritise'),
  30. ),
  31. );
  32. }
  33. /**
  34. * Get the next item in the queue
  35. *
  36. * @return Irc_waiting_message Next message if there is one
  37. */
  38. public static function top() {
  39. $wm = new Irc_waiting_message();
  40. $wm->orderBy('prioritise DESC, created');
  41. $wm->whereAdd('claimed is null');
  42. $wm->limit(1);
  43. $cnt = $wm->find(true);
  44. if ($cnt) {
  45. // XXX: potential race condition
  46. // can we force it to only update if claimed is still null
  47. // (or old)?
  48. common_log(LOG_INFO, 'claiming IRC waiting message id = ' . $wm->id);
  49. $orig = clone($wm);
  50. $wm->claimed = common_sql_now();
  51. $result = $wm->update($orig);
  52. if ($result) {
  53. common_log(LOG_INFO, 'claim succeeded.');
  54. return $wm;
  55. } else {
  56. common_log(LOG_INFO, 'claim failed.');
  57. }
  58. }
  59. $wm = null;
  60. return null;
  61. }
  62. /**
  63. * Increment the attempts count
  64. *
  65. * @return void
  66. * @throws Exception
  67. */
  68. public function incAttempts() {
  69. $orig = clone($this);
  70. $this->attempts++;
  71. $result = $this->update($orig);
  72. if (!$result) {
  73. // TRANS: Exception thrown when an IRC attempts count could not be updated.
  74. // TRANS: %d is the object ID for which the count could not be updated.
  75. throw new Exception(sprintf(_m('Could not increment attempts count for %d.'), $this->id));
  76. }
  77. }
  78. /**
  79. * Release a claimed item.
  80. */
  81. public function releaseClaim() {
  82. // DB_DataObject doesn't let us save nulls right now
  83. $sql = sprintf("UPDATE irc_waiting_message SET claimed=NULL WHERE id=%d", $this->id);
  84. $this->query($sql);
  85. $this->claimed = null;
  86. $this->encache();
  87. }
  88. }