asynctask.class.inc.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Process($iTimeLimit)
  27. {
  28. $sOQL = "SELECT AsyncTask WHERE ISNULL(started)";
  29. $oSet = new CMDBObjectSet(DBObjectSearch::FromOQL($sOQL), array('created' => true) /* order by*/, array());
  30. $iProcessed = 0;
  31. while ((time() < $iTimeLimit) && ($oTask = $oSet->Fetch()))
  32. {
  33. $oTask->Set('started', time());
  34. $oTask->DBUpdate();
  35. $oTask->Process();
  36. $iProcessed++;
  37. $oTask->DBDelete();
  38. }
  39. if ($iProcessed == $oSet->Count())
  40. {
  41. return "processed $iProcessed tasks";
  42. }
  43. else
  44. {
  45. return "processed $iProcessed tasks (remaining: ".($oSet->Count() - $iProcessed).")";
  46. }
  47. }
  48. }
  49. /**
  50. * A
  51. *
  52. * @package iTopORM
  53. */
  54. abstract class AsyncTask extends DBObject
  55. {
  56. public static function Init()
  57. {
  58. $aParams = array
  59. (
  60. "category" => "core/cmdb",
  61. "key_type" => "autoincrement",
  62. "name_attcode" => array('created'),
  63. "state_attcode" => "",
  64. "reconc_keys" => array(),
  65. "db_table" => "priv_async_task",
  66. "db_key_field" => "id",
  67. "db_finalclass_field" => "realclass",
  68. "display_template" => "",
  69. );
  70. MetaModel::Init_Params($aParams);
  71. //MetaModel::Init_InheritAttributes();
  72. // MetaModel::Init_AddAttribute(new AttributeString("name", array("allowed_values"=>null, "sql"=>"name", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  73. MetaModel::Init_AddAttribute(new AttributeDateTime("created", array("allowed_values"=>null, "sql"=>"created", "default_value"=>"", "is_null_allowed"=>false, "depends_on"=>array())));
  74. MetaModel::Init_AddAttribute(new AttributeDateTime("started", array("allowed_values"=>null, "sql"=>"started", "default_value"=>"", "is_null_allowed"=>true, "depends_on"=>array())));
  75. 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())));
  76. // Display lists
  77. // MetaModel::Init_SetZListItems('details', array()); // Attributes to be displayed for the complete details
  78. // MetaModel::Init_SetZListItems('list', array()); // Attributes to be displayed for a list
  79. // Search criteria
  80. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  81. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  82. }
  83. protected function OnInsert()
  84. {
  85. $this->Set('created', time());
  86. }
  87. public function Process()
  88. {
  89. $sStatus = $this->DoProcess();
  90. if ($this->Get('event_id') != 0)
  91. {
  92. $oEventLog = MetaModel::GetObject('Event', $this->Get('event_id'));
  93. $oEventLog->Set('message', $sStatus);
  94. $oEventLog->DBUpdate();
  95. }
  96. }
  97. abstract public function DoProcess();
  98. }
  99. /**
  100. * An email notification
  101. *
  102. * @package iTopORM
  103. */
  104. class AsyncSendEmail extends AsyncTask
  105. {
  106. public static function Init()
  107. {
  108. $aParams = array
  109. (
  110. "category" => "core/cmdb",
  111. "key_type" => "autoincrement",
  112. "name_attcode" => "created",
  113. "state_attcode" => "",
  114. "reconc_keys" => array(),
  115. "db_table" => "priv_async_send_email",
  116. "db_key_field" => "id",
  117. "db_finalclass_field" => "",
  118. "display_template" => "",
  119. );
  120. MetaModel::Init_Params($aParams);
  121. MetaModel::Init_InheritAttributes();
  122. MetaModel::Init_AddAttribute(new AttributeText("to", array("allowed_values"=>null, "sql"=>"to", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  123. MetaModel::Init_AddAttribute(new AttributeText("subject", array("allowed_values"=>null, "sql"=>"subject", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  124. MetaModel::Init_AddAttribute(new AttributeText("body", array("allowed_values"=>null, "sql"=>"body", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  125. MetaModel::Init_AddAttribute(new AttributeText("header", array("allowed_values"=>null, "sql"=>"header", "default_value"=>null, "is_null_allowed"=>false, "depends_on"=>array())));
  126. // Display lists
  127. // 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
  128. // MetaModel::Init_SetZListItems('list', array('name', 'status', 'to', 'subject')); // Attributes to be displayed for a list
  129. // Search criteria
  130. // MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form
  131. // MetaModel::Init_SetZListItems('advanced_search', array('name')); // Criteria of the advanced search form
  132. }
  133. static public function AddToQueue($sTo, $sSubject, $sBody, $aHeaders, $oLog)
  134. {
  135. $oNew = MetaModel::NewObject(__class__);
  136. if ($oLog)
  137. {
  138. $oNew->Set('event_id', $oLog->GetKey());
  139. }
  140. $oNew->Set('to', $sTo);
  141. $oNew->Set('subject', $sSubject);
  142. $oNew->Set('body', $sBody);
  143. $sHeaders = serialize($aHeaders);
  144. $oNew->Set('header', $sHeaders);
  145. $oNew->DBInsert();
  146. }
  147. public function DoProcess()
  148. {
  149. $sTo = $this->Get('to');
  150. $sSubject = $this->Get('subject');
  151. $sBody = $this->Get('body');
  152. $sHeaders = $this->Get('header');
  153. $aHeaders = unserialize($sHeaders);
  154. $oEmail = new EMail($sTo, $sSubject, $sBody, $aHeaders);
  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. ?>