Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.ExecProcessus.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Method for processes family
8  *
9  */
10 namespace Dcp\Core;
12 {
13  private $execuserid;
14  /**
15  * execute the action describe in the object
16  * @apiExpose
17  * @param string $comment
18  * @return int shell status (0 means OK).
19  */
20  function bgExecute($comment = "")
21  {
22  /**
23  * @var \Action $action
24  */
25  global $action;
26 
27  if (!$this->canExecuteAction()) {
28  AddWarningMsg(sprintf(_("Error : need edit privilege to execute")));
29  } else {
30  return $this->_execute($action, $comment);
31  }
32  return -2;
33  }
34  /**
35  * cancel next execution
36  * @apiExpose
37  * @return string
38  */
39  function resetExecute()
40  {
41  $this->clearValue("exec_status");
42  $this->clearValue("exec_statusdate");
43  $err = $this->modify();
44  return $err;
45  }
46 
47  function isInprogress()
48  {
49  if ($this->canEdit() == "") {
50  if ($this->getRawValue("exec_status") == "progressing") return MENU_ACTIVE;
51  }
52  return MENU_INVISIBLE;
53  }
54 
55  function postStore()
56  {
57  $this->setValue("exec_nextdate", $this->getNextExecDate());
58  }
59  /**
60  * return the wsh command which be send
61  */
62  function bgCommand($masteruserid = false)
63  {
64  $bgapp = $this->getRawValue("exec_application");
65  $bgact = $this->getRawValue("exec_action");
66  $bgapi = $this->getRawValue("exec_api");
67 
68  $tp = $this->getArrayRawValues("exec_t_parameters");
69 
70  $cmd = getWshCmd(true);
71  if ($masteruserid) {
72  $fuid = $this->getRawValue("exec_iduser");
73  $fu = getTDoc($this->dbaccess, $fuid);
74  $wuid = $fu["us_whatid"];
75  $this->execuserid = $fuid;
76  } else {
77  $wuid = $this->userid;
78  $this->execuserid = $this->getUserId();
79  }
80  $cmd.= " --userid=$wuid";
81  if (!$bgapi) $cmd.= sprintf(" --app=%s --action=%s", escapeshellarg($bgapp) , escapeshellarg($bgact));
82  else $cmd.= sprintf(" --api=%s", escapeshellarg($bgapi));
83 
84  foreach ($tp as $k => $v) {
85  $b = sprintf(" --%s=%s", escapeshellarg($v["exec_idvar"]) , escapeshellarg($v["exec_valuevar"]));
86  $cmd.= $b;
87  }
88  return $cmd;
89  }
90  /**
91  * return the document user id for the next execution
92  * @return string
93  */
94  function getExecUserID()
95  {
96  return $this->execuserid;
97  }
98  /**
99  * return the next date to execute process
100  * @return string date
101  */
102  function getNextExecDate()
103  {
104  $ndh = $this->getRawValue("exec_handnextdate");
105  if ($ndh == "") {
106  $nday = intval($this->getRawValue("exec_periodday", 0));
107  $nhour = intval($this->getRawValue("exec_periodhour", 0));
108  $nmin = intval($this->getRawValue("exec_periodmin", 0));
109  if (($nday + $nhour + $nmin) > 0) {
110  $ndh = $this->getDate($nday, $nhour, $nmin);
111  } else {
112  $ndh = " ";
113  }
114  }
115 
116  return $ndh;
117  }
118 
119  function getPrevExecDate()
120  {
121  if ($this->revision > 0) {
122  $pid = $this->getLatestId(true);
123  $td = getTDoc($this->dbaccess, $pid);
124  $ndh = getv($td, "exec_date");
125 
126  return $ndh;
127  }
128  return '';
129  }
130 
131  function isLatestExec()
132  {
133  if ($this->locked == - 1) return MENU_INVISIBLE;
134  if (!$this->canExecuteAction()) return MENU_INACTIVE;
135  return MENU_ACTIVE;
136  }
137 
138  function canExecuteAction()
139  {
140  $err = $this->control('edit');
141  return ($err == "");
142  }
143 
144  public function executeNow()
145  {
146  /**
147  * Logging in bgexecute
148  */
149  $status = $this->bgExecute(_("dynacase cron try execute"));
150  $del = new_Doc($this->dbaccess, $this->getLatestId(false, true));
151  /**
152  * @var \Dcp\Family\EXEC $del
153  */
154  $del->clearValue("exec_status");
155  $del->clearValue("exec_handnextdate");
156  $err = $del->store();
157 
158  if ($status == 0) {
159  print sprintf("Execute %s [%d] (%s) : %s\n", $del->title, $del->id, $del->getRawValue("exec_handnextdate") , $err);
160  } else {
161  print sprintf("Error executing %s [%d] (%s) : %s (%s)\n", $del->title, $del->id, $del->getRawValue("exec_handnextdate") , $err, $status);
162  }
163  }
164 
165  public function _execute(\Action & $action, $comment = '')
166  {
167  setMaxExecutionTimeTo(3600);
168  /*
169  $cmd = getWshCmd(true);
170  $cmd.= " --api=fdl_execute";
171  $cmd.= " --docid=" . $this->id;
172 
173  $cmd.= " --userid=" . $this->userid;
174  if ($comment != "") $cmd.= " --comment=" . base64_encode($comment); // prevent hack
175  */
176  $time_start = microtime(true);
177  // system($cmd, $status);
178  $status = $this->__execute($action, $this->id, $comment);
179  $time_end = microtime(true);
180  $time = $time_end - $time_start;
181  if ($status == 0) {
182  AddWarningMsg(sprintf(_("Process %s [%d] executed") , $this->title, $this->id));
183  $action->log->info(sprintf(_("Process %s [%d] executed in %.03f seconds") , $this->title, $this->id, $time));
184  } else {
185  AddWarningMsg(sprintf(_("Error : Process %s [%d]: status %d") , $this->title, $this->id, $status));
186  $action->log->error(sprintf(_("Error : Process %s [%d]: status %d in %.03f seconds") , $this->title, $this->id, $status, $time));
187  }
188  return $status;
189  }
190 
191  public function __execute(\Action & $action, $docid, $comment = '')
192  {
193  $doc = new_Doc($action->dbaccess, $docid);
194  /**
195  * @var \Dcp\Family\EXEC $doc
196  */
197  if ($doc->locked == - 1) { // it is revised document
198  $doc = new_Doc($action->dbaccess, $doc->getLatestId());
199  }
200 
201  $doc->setValue("exec_status", "progressing");
202  $doc->setValue("exec_statusdate", $doc->getTimeDate());
203  $doc->modify(true, array(
204  "exec_status",
205  "exec_statusdate"
206  ) , true);
207  $cmd = $doc->bgCommand($action->user->id == 1);
208  $f = uniqid(getTmpDir() . "/fexe");
209  $fout = "$f.out";
210  $ferr = "$f.err";
211  $cmd.= ">$fout 2>$ferr";
212  $m1 = microtime();
213  system($cmd, $statut);
214  $m2 = microtime_diff(microtime() , $m1);
215  $ms = gmstrftime("%H:%M:%S", $m2);
216 
217  if (file_exists($fout)) {
218  $doc->setValue("exec_detail", file_get_contents($fout));
219  unlink($fout);
220  }
221  if (file_exists($ferr)) {
222  $doc->setValue("exec_detaillog", file_get_contents($ferr));
223  unlink($ferr);
224  }
225 
226  $doc->clearValue("exec_nextdate");
227  $doc->setValue("exec_elapsed", $ms);
228  $doc->setValue("exec_date", date("d/m/Y H:i "));
229  $doc->clearValue("exec_status");
230  $doc->clearValue("exec_statusdate");
231  $doc->setValue("exec_state", (($statut == 0) ? "OK" : $statut));
232  $puserid = $doc->getRawValue("exec_iduser"); // default exec user
233  $doc->setValue("exec_iduser", $doc->getExecUserID());
234  $doc->refresh();
235  $err = $doc->modify();
236  if ($err == "") {
237  if ($comment != "") $doc->addHistoryEntry($comment);
238  $err = $doc->revise(sprintf(_("execution by %s done %s") , $doc->getTitle($doc->getExecUserID()) , $statut));
239  if ($err == "") {
240  $doc->clearValue("exec_elapsed");
241  $doc->clearValue("exec_detail");
242  $doc->clearValue("exec_detaillog");
243  $doc->clearValue("exec_date");
244  $doc->clearValue("exec_state");
245  $doc->setValue("exec_iduser", $puserid);
246  $doc->refresh();
247  $err = $doc->modify();
248  }
249  } else {
250  $doc->addHistoryEntry($err, HISTO_ERROR);
251  }
252 
253  if ($err != "") return 1;
254  return 0;
255  }
256 }
static getUserId()
Definition: Class.Doc.php:8960
$status
Definition: index.php:30
getTDoc($dbaccess, $id, $sqlfilters=array(), $result=array())
global $action
static getDate($daydelta=0, $dayhour="", $daymin="", $getlocale=false)
Definition: Class.Doc.php:8783
const MENU_INVISIBLE
Definition: Class.Doc.php:37
const MENU_INACTIVE
Definition: Class.Doc.php:38
getLatestId($fixed=false, $forcequery=false)
Definition: Class.Doc.php:2045
info($msg)
getv(&$t, $k, $d="")
canEdit($verifyDomain=true)
Definition: Class.Doc.php:1364
clearValue($attrid)
Definition: Class.Doc.php:4409
bgCommand($masteruserid=false)
getArrayRawValues($idAttr, $index=-1)
Definition: Class.Doc.php:3292
const MENU_ACTIVE
Definition: Class.Doc.php:36
modify($nopost=false, $sfields="", $nopre=false)
foreach($argv as $arg) $cmd
const HISTO_ERROR
error($msg)
$docid
Definition: cleanFamily.php:13
setValue($attrid, $value, $index=-1, &$kvalue=null)
Definition: Class.Doc.php:3528
getWshCmd($nice=false, $userid=0, $sudo=false)
Definition: Lib.Common.php:594
revision(Action &$action)
Definition: revision.php:22
_execute(\Action &$action, $comment= '')
microtime_diff($a, $b)
Definition: Lib.Common.php:302
print
Definition: checklist.php:49
getTmpDir($def= '/tmp')
Definition: Lib.Common.php:150
new_Doc($dbaccess, $id= '', $latest=false)
setMaxExecutionTimeTo($limit)
Definition: Lib.Common.php:121
$comment
Definition: Class.Doc.php:441
if($file) if($subject==""&&$file) if($subject=="") $err
getRawValue($idAttr, $def="")
Definition: Class.Doc.php:3117
control($aclname, $strict=false)
Definition: Class.Doc.php:6593
← centre documentaire © anakeen