asynctask.class.inc.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Persistent classes (internal): user defined actions
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. class ExecAsyncTask implements iBackgroundProcess
  25. {
  26. public function GetPeriodicity()
  27. {
  28. return 2; // seconds
  29. }
  30. public function Process($iTimeLimit)
  31. {
  32. $sOQL = "SELECT AsyncTask WHERE ISNULL(started) AND (ISNULL(planned) OR (planned < NOW()))";
  33. $oSet = new CMDBObjectSet(DBObjectSearch::FromOQL($sOQL), array('created' => true) /* order by*/, array());
  34. $iProcessed = 0;
  35. while ((time() < $iTimeLimit) && ($oTask = $oSet->Fetch()))
  36. {
  37. $oTask->Set('started', time());
  38. $oTask->DBUpdate();
  39. $oTask->Process();
  40. $iProcessed++;
  41. $oTask->DBDelete();
  42. }
  43. if ($iProcessed == $oSet->Count())
  44. {
  45. return "processed $iProcessed tasks";
  46. }
  47. else
  48. {
  49. return "processed $iProcessed tasks (remaining: ".($oSet->Count() - $iProcessed).")";
  50. }
  51. }
  52. }
  53. /**
  54. * A
  55. *
  56. * @package iTopORM
  57. */
  58. abstract class AsyncTask extends DBObject
  59. {
  60. public static function Init()
  61. {
  62. $aParams = array
  63. (
  64. "category" => "core/cmdb",
  65. "key_type" => "autoincrement",
  66. "name_attcode" => array('created'),
  67. "state_attcode" => "",
  68. "reconc_keys" => array(),
  69. "db_table" => "priv_async_task",
  70. "db_key_field" => "id",
  71. "db_finalclass_field" => "realclass",
  72. "display_template" => "",
  73. );
  74. MetaModel::Init_Params($aParams);
  75. //MetaModel::Init_InheritAttributes();
  76. // MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  77. MetaModel::Init_AddAttribute(new AttributeDateTime("created", array("allowed_values"=>null, "sql"=>"created", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  78. MetaModel::Init_AddAttribute(new AttributeDateTime("started", array("allowed_values"=>null, "sql"=>"started", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  79. // planned... still not used - reserved for timer management
  80. MetaModel::Init_AddAttribute(new AttributeDateTime("planned", array("allowed_values"=>null, "sql"=>"planned", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  81. MetaModel::Init_AddAttribute(new AttributeExternalKey("event_id", array("targetclass"=>"Event", "jointype"=> "", "allowed_values"=>null, "sql"=>"event_id", "is_null_allowed"=>true, "on_target_delete"=>DEL_SILENT, "depends_on"=>array())));
  82. // Display lists
  83. // MetaModel::Init_SetZListItems('details', array()); // Attributes to be displayed for the complete details
  84. // MetaModel::Init_SetZListItems('list', array()); // Attributes to be displayed for a list
  85. // Search criteria
  86. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  87. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  88. }
  89. protected function OnInsert()
  90. {
  91. $this->Set('created', time());
  92. }
  93. public function Process()
  94. {
  95. $sStatus = $this->DoProcess();
  96. if ($this->Get('event_id') != 0)
  97. {
  98. $oEventLog = MetaModel::GetObject('Event', $this->Get('event_id'));
  99. $oEventLog->Set('message', $sStatus);
  100. $oEventLog->DBUpdate();
  101. }
  102. }
  103. abstract public function DoProcess();
  104. }
  105. /**
  106. * An email notification
  107. *
  108. * @package iTopORM
  109. */
  110. class AsyncSendEmail extends AsyncTask
  111. {
  112. public static function Init()
  113. {
  114. $aParams = array
  115. (
  116. "category" => "core/cmdb",
  117. "key_type" => "autoincrement",
  118. "name_attcode" => "created",
  119. "state_attcode" => "",
  120. "reconc_keys" => array(),
  121. "db_table" => "priv_async_send_email",
  122. "db_key_field" => "id",
  123. "db_finalclass_field" => "",
  124. "display_template" => "",
  125. );
  126. MetaModel::Init_Params($aParams);
  127. MetaModel::Init_InheritAttributes();
  128. MetaModel::Init_AddAttribute(new AttributeText("to", array("allowed_values"=>null, "sql"=>"to", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  129. MetaModel::Init_AddAttribute(new AttributeText("subject", array("allowed_values"=>null, "sql"=>"subject", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  130. MetaModel::Init_AddAttribute(new AttributeText("message", array("allowed_values"=>null, "sql"=>"message", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  131. // Display lists
  132. // MetaModel::Init_SetZListItems('details', array('name', 'description', 'status', 'test_recipient', 'from', 'reply_to', 'to', 'cc', 'bcc', 'subject', 'body', 'importance', 'trigger_list')); // Attributes to be displayed for the complete details
  133. // MetaModel::Init_SetZListItems('list', array('name', 'status', 'to', 'subject')); // Attributes to be displayed for a list
  134. // Search criteria
  135. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  136. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  137. }
  138. static public function AddToQueue(EMail $oEMail, $oLog)
  139. {
  140. $oNew = MetaModel::NewObject(__class__);
  141. if ($oLog)
  142. {
  143. $oNew->Set('event_id', $oLog->GetKey());
  144. }
  145. $oNew->Set('to', $oEMail->GetRecipientTO(true /* string */));
  146. $oNew->Set('subject', $oEMail->GetSubject());
  147. $sMessage = serialize($oEMail);
  148. $oNew->Set('message', $sMessage);
  149. $oNew->DBInsert();
  150. }
  151. public function DoProcess()
  152. {
  153. $sMessage = $this->Get('message');
  154. $oEMail = unserialize($sMessage);
  155. $iRes = $oEMail->Send($aIssues, true /* force synchro !!!!! */);
  156. switch ($iRes)
  157. {
  158. case EMAIL_SEND_OK:
  159. return "Sent";
  160. case EMAIL_SEND_PENDING:
  161. return "Bug - the email should be sent in synchronous mode";
  162. case EMAIL_SEND_ERROR:
  163. return "Failed: ".implode(', ', $aIssues);
  164. }
  165. }
  166. }
  167. ?>