1 | <?php
|
---|
2 |
|
---|
3 | include_once(dirname(__FILE__).'/../../Base/Model.php');
|
---|
4 |
|
---|
5 | class Task extends Model
|
---|
6 | {
|
---|
7 | var $TempScript = 'temp/wowhosting_script.sh';
|
---|
8 |
|
---|
9 | function __construct($System)
|
---|
10 | {
|
---|
11 | parent::__construct($System);
|
---|
12 | }
|
---|
13 |
|
---|
14 | function Add($Title, $Task)
|
---|
15 | {
|
---|
16 | $CommandList = implode("\n", $Task)."\n";
|
---|
17 | $this->Database->insert('Task', array('User' => $this->System->Modules['User']->User['Id'], 'Title' => $Title, 'TimeCreate' => 'NOW()', 'CommandList' => $CommandList));
|
---|
18 | }
|
---|
19 |
|
---|
20 | function ProcessAll()
|
---|
21 | {
|
---|
22 | chdir($this->Config['BaseDir']);
|
---|
23 | $DbResult = $this->Database->query('SELECT * FROM Task WHERE State = 0 ORDER BY Id,TimeCreate ASC');
|
---|
24 | while($Task = $DbResult->fetch_assoc())
|
---|
25 | {
|
---|
26 | $this->Database->query('UPDATE Task SET TimeStart=NOW(), State=1 WHERE Id='.$Task['Id']);
|
---|
27 | echo('Provádím úlohu '.$Task['Id'].': '.$Task['Title']."...\n");
|
---|
28 | $Task['CommandList'] = "#!/bin/sh\n".str_replace("\r", '', $Task['CommandList']);
|
---|
29 | file_put_contents($this->TempScript, $Task['CommandList']);
|
---|
30 | chmod($this->TempScript, 0755);
|
---|
31 | exec($this->TempScript.' >'.$this->TempScript.'.log 2>'.$this->TempScript.'.err');
|
---|
32 | $StdOut = file_get_contents($this->TempScript.'.log');
|
---|
33 | unlink($this->TempScript.'.log');
|
---|
34 | $StdErr = file_get_contents($this->TempScript.'.err');
|
---|
35 | unlink($this->TempScript.'.err');
|
---|
36 | echo("Hotovo\n");
|
---|
37 | $this->Database->query('UPDATE `Task` SET `TimeEnd`=NOW(), `State`=2, `Output`="'.addslashes($StdOut).'", `Error`="'.addslashes($StdErr).'" WHERE `Id`='.$Task['Id']);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | function ProcessAllCycle()
|
---|
42 | {
|
---|
43 | $this->Database->query('UPDATE Task SET State=2 WHERE State=1'); // Change not finished running tasks to finished
|
---|
44 | $this->Database->query('UPDATE Task SET TimeEnd=NOW() WHERE State=2 AND TimeEnd = NULL'); // Set end time to finished tasks
|
---|
45 | while(1)
|
---|
46 | {
|
---|
47 | $this->ProcessAll();
|
---|
48 | sleep(1);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | function Abort($Id)
|
---|
53 | {
|
---|
54 | $this->Database->query('DELETE FROM Task WHERE Id='.$Id);
|
---|
55 | }
|
---|
56 | }
|
---|