mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-24 15:18:40 +00:00
Adjust windows scheduler
This commit is contained in:
parent
ba2a8d9f62
commit
a6a56883a8
40
Utils/ImageUtils.php
Normal file
40
Utils/ImageUtils.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
/**
|
||||
* Orange Management
|
||||
*
|
||||
* PHP Version 7.0
|
||||
*
|
||||
* @category TBD
|
||||
* @package TBD
|
||||
* @author OMS Development Team <dev@oms.com>
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
* @copyright 2013 Dennis Eichhorn
|
||||
* @license OMS License 1.0
|
||||
* @version 1.0.0
|
||||
* @link http://orange-management.com
|
||||
*/
|
||||
namespace phpOMS\Utils;
|
||||
|
||||
/**
|
||||
* Image utils class.
|
||||
*
|
||||
* This class provides static helper functionalities for images.
|
||||
*
|
||||
* @category Framework
|
||||
* @package phpOMS\Utils
|
||||
* @author OMS Development Team <dev@oms.com>
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
* @license OMS License 1.0
|
||||
* @link http://orange-management.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class ImageUtils
|
||||
{
|
||||
public static function decodeBase64Image(string $img) : string
|
||||
{
|
||||
$img = str_replace('data:image/png;base64,', '', $img);
|
||||
$img = str_replace(' ', '+', $img);
|
||||
|
||||
return base64_decode($img);
|
||||
}
|
||||
}
|
||||
|
|
@ -37,15 +37,9 @@ class Schedule extends TaskAbstract implements \Serializable
|
|||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function __construct(Interval $interval = null, string $cmd = '')
|
||||
public function __construct(string $name, string $cmd = '')
|
||||
{
|
||||
if (!isset($interval)) {
|
||||
$this->interval = new Interval();
|
||||
} else {
|
||||
$this->interval = $interval;
|
||||
}
|
||||
|
||||
$this->command = $cmd;
|
||||
parent::__construct($name, $cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ final class SchedulerFactory
|
|||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public static function create() : ScheduleInterface
|
||||
public static function create() : SchedulerAbstract
|
||||
{
|
||||
switch (OperatingSystem::getSystem()) {
|
||||
case SystemType::WIN:
|
||||
|
|
|
|||
|
|
@ -37,21 +37,42 @@ abstract class TaskAbstract
|
|||
protected $id = '';
|
||||
|
||||
/**
|
||||
* Interval.
|
||||
*
|
||||
* @var Interval
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected $interval = null;
|
||||
|
||||
/**
|
||||
* Command.
|
||||
* Command used for creating the task
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected $command = '';
|
||||
|
||||
/**
|
||||
* Command/script to run.
|
||||
*
|
||||
* @var string
|
||||
* @since 1.0.0
|
||||
*/
|
||||
protected $run = '';
|
||||
|
||||
protected $status = '';
|
||||
|
||||
protected $nextRunTime = null;
|
||||
|
||||
protected $lastRunTime = null;
|
||||
|
||||
protected $start = null;
|
||||
|
||||
protected $end = null;
|
||||
|
||||
protected $comment = '';
|
||||
|
||||
protected $results = [];
|
||||
|
||||
protected $author = '';
|
||||
|
||||
public function __construct(string $name, string $cmd = '') {
|
||||
$this->id = $name;
|
||||
$this->command = $cmd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get id.
|
||||
*
|
||||
|
|
@ -65,34 +86,6 @@ abstract class TaskAbstract
|
|||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get interval.
|
||||
*
|
||||
* @return Interval
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function getInterval() : Interval
|
||||
{
|
||||
return $this->interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set interval.
|
||||
*
|
||||
* @param Interval $interval Interval
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setInterval(Interval $interval)
|
||||
{
|
||||
$this->interval = $interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get command.
|
||||
*
|
||||
|
|
@ -120,4 +113,241 @@ abstract class TaskAbstract
|
|||
{
|
||||
$this->command = $command;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get run.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function getRun() : string
|
||||
{
|
||||
return $this->run;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set run.
|
||||
*
|
||||
* @param string $run Command/script to run
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setRun(string $run)
|
||||
{
|
||||
$this->run = $run;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get status.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function getStatus() : string
|
||||
{
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set status.
|
||||
*
|
||||
* @param string $status Status
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setStatus(string $status)
|
||||
{
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next run time.
|
||||
*
|
||||
* @return \DateTime
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function getNextRunTime()
|
||||
{
|
||||
return $this->nextRunTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set next run time.
|
||||
*
|
||||
* @param \DateTime $nextRunTime Next run time
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setNextRunTime(\DateTime $nextRunTime)
|
||||
{
|
||||
$this->nextRuntime = $nextRunTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last run time.
|
||||
*
|
||||
* @return \DateTime
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function getLastRuntime()
|
||||
{
|
||||
return $this->lastRunTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set last run time.
|
||||
*
|
||||
* @param \DateTime $lastRunTime Last run time
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setLastRuntime(\DateTime $lastRunTime)
|
||||
{
|
||||
$this->lastRunTime = $lastRunTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get start.
|
||||
*
|
||||
* @return \DateTime
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function getStart()
|
||||
{
|
||||
return $this->start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set start.
|
||||
*
|
||||
* @param \DateTime $start Start
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setStart(\DateTime $start)
|
||||
{
|
||||
$this->start = $start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get end.
|
||||
*
|
||||
* @return \DateTime
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function getEnd()
|
||||
{
|
||||
return $this->end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set end.
|
||||
*
|
||||
* @param \DateTime $end End
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setEnd(\DateTime $end)
|
||||
{
|
||||
$this->end = $end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get author.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function getAuthor() : string
|
||||
{
|
||||
return $this->author;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set author.
|
||||
*
|
||||
* @param string $author Author
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setAuthor(string $author)
|
||||
{
|
||||
$this->author = $author;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get comment.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function getComment() : string
|
||||
{
|
||||
return $this->comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set comment.
|
||||
*
|
||||
* @param string $comment Comment
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function setComment(string $comment)
|
||||
{
|
||||
$this->comment = $comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get comment.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public function addResult(string $result)
|
||||
{
|
||||
$this->results[] = $result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ final class TaskFactory
|
|||
/**
|
||||
* Create task instance.
|
||||
*
|
||||
* @param Interval $interval Task interval
|
||||
* @param string $id Task id
|
||||
* @param string $cmd Command to run
|
||||
*
|
||||
* @return TaskInterface
|
||||
|
|
@ -45,13 +45,13 @@ final class TaskFactory
|
|||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public static function create(Interval $interval = null, string $cmd = '') : TaskInterface
|
||||
public static function create(string $id = null, string $cmd = '') : TaskAbstract
|
||||
{
|
||||
switch (OperatingSystem::getSystem()) {
|
||||
case SystemType::WIN:
|
||||
return new Schedule($interval, $cmd);
|
||||
return new Schedule($id, $cmd);
|
||||
case SystemType::LINUX:
|
||||
return new CronJob($interval, $cmd);
|
||||
return new CronJob($id, $cmd);
|
||||
default:
|
||||
throw new \Exception('Unsupported system.');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@
|
|||
*/
|
||||
namespace phpOMS\Utils\TaskSchedule;
|
||||
|
||||
use phpOMS\Validation\Base\DateTime;
|
||||
|
||||
/**
|
||||
* Task scheduler class.
|
||||
*
|
||||
|
|
@ -29,7 +31,7 @@ namespace phpOMS\Utils\TaskSchedule;
|
|||
class TaskScheduler extends SchedulerAbstract
|
||||
{
|
||||
|
||||
const BIN = '';
|
||||
protected static $bin = 'c:/WINDOWS/system32/schtasks.exe';
|
||||
|
||||
/**
|
||||
* Env variables.
|
||||
|
|
@ -39,6 +41,29 @@ class TaskScheduler extends SchedulerAbstract
|
|||
*/
|
||||
private $envOptions = [];
|
||||
|
||||
/**
|
||||
* Test git.
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||
*/
|
||||
public static function test() : bool
|
||||
{
|
||||
$pipes = [];
|
||||
$resource = proc_open(escapeshellarg(self::$bin), [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes);
|
||||
|
||||
$stdout = stream_get_contents($pipes[1]);
|
||||
$stderr = stream_get_contents($pipes[2]);
|
||||
|
||||
foreach ($pipes as $pipe) {
|
||||
fclose($pipe);
|
||||
}
|
||||
|
||||
return trim(proc_close($resource)) !== 127;
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
|
||||
|
|
@ -56,9 +81,9 @@ class TaskScheduler extends SchedulerAbstract
|
|||
* @since 1.0.0
|
||||
* @author Dennis Eichhorn
|
||||
*/
|
||||
public function run(string $cmd) : array
|
||||
private function run(string $cmd) : string
|
||||
{
|
||||
$cmd = 'cd ' . escapeshellarg(dirname(self::BIN)) . ' && ' . basename(self::BIN) . ' -C ' . escapeshellarg(__DIR__) . ' ' . $cmd;
|
||||
$cmd = 'cd ' . escapeshellarg(dirname(self::$bin)) . ' && ' . basename(self::$bin) . ' ' . $cmd;
|
||||
|
||||
$pipes = [];
|
||||
$desc = [
|
||||
|
|
@ -92,9 +117,52 @@ class TaskScheduler extends SchedulerAbstract
|
|||
return trim($stdout);
|
||||
}
|
||||
|
||||
private function normalize(string $raw) : string
|
||||
{
|
||||
return str_replace("\r\n", "\n", $raw);
|
||||
}
|
||||
|
||||
private function parseJobList(array $jobData) {
|
||||
$job = TaskFactory::create($jobData[1], '');
|
||||
|
||||
$job->setRun($jobData[8]);
|
||||
$job->setStatus($jobData[3]);
|
||||
|
||||
if(DateTime::isValid($jobData[2])) {
|
||||
$job->setNextRunTime(new \DateTime($jobData[2]));
|
||||
}
|
||||
|
||||
if(DateTime::isValid($jobData[5])) {
|
||||
$job->setLastRunTime(new \DateTime($jobData[5]));
|
||||
}
|
||||
|
||||
$job->setAuthor($jobData[7]);
|
||||
$job->setComment($jobData[10]);
|
||||
|
||||
if(DateTime::isValid($jobData[20])) {
|
||||
$job->setStart(new \DateTime($jobData[20]));
|
||||
}
|
||||
|
||||
if(DateTime::isValid($jobData[21])) {
|
||||
$job->setEnd(new \DateTime($jobData[21]));
|
||||
}
|
||||
|
||||
$job->addResult($jobData[6]);
|
||||
|
||||
return $job;
|
||||
}
|
||||
|
||||
public function getAll() : array
|
||||
{
|
||||
return str_getcsv($this->run('/query /v /fo CSV'));
|
||||
$lines = explode("\n", $this->normalize($this->run('/query /v /fo CSV')));
|
||||
unset($lines[0]);
|
||||
|
||||
$jobs = [];
|
||||
foreach($lines as $line) {
|
||||
$jobs[] = $this->parseJobList(str_getcsv($line));
|
||||
}
|
||||
|
||||
return $jobs;
|
||||
}
|
||||
|
||||
public function get(string $id)
|
||||
|
|
@ -107,9 +175,32 @@ class TaskScheduler extends SchedulerAbstract
|
|||
|
||||
}
|
||||
|
||||
public function getAllByName(string $name) : array
|
||||
public function getAllByName(string $name, bool $exact = true) : array
|
||||
{
|
||||
return str_getcsv($this->run('/query /v /fo CSV /tn ' . escapeshellarg($name)));
|
||||
if($exact) {
|
||||
$lines = $this->run('/query /v /fo CSV /tn ' . escapeshellarg($name));
|
||||
unset($lines[0]);
|
||||
|
||||
$jobs = [];
|
||||
foreach($lines as $line) {
|
||||
$jobs[] = $this->parseJobList(str_getcsv($line));
|
||||
}
|
||||
} else {
|
||||
$lines = explode("\n", $this->normalize($this->run('/query /v /fo CSV')));
|
||||
$jobs = [];
|
||||
|
||||
unset($lines[0]);
|
||||
|
||||
foreach($lines as $key => $line) {
|
||||
$line = str_getcsv($line);
|
||||
|
||||
if(strpos($line[1], $name) !== false) {
|
||||
$jobs[] = $this->parseJobList($line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $jobs;
|
||||
}
|
||||
|
||||
public function create(Schedule $task)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user