mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-02-12 14:58:42 +00:00
fixes #26
This commit is contained in:
parent
ed3fc48e34
commit
a33a7be954
|
|
@ -25,6 +25,74 @@ namespace phpOMS\Utils\TaskSchedule;
|
||||||
*/
|
*/
|
||||||
class Cron extends SchedulerAbstract
|
class Cron extends SchedulerAbstract
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function create(TaskAbstract $task) : void
|
||||||
|
{
|
||||||
|
$this->run('-l > tmpcron');
|
||||||
|
\file_put_contents('tmpcron', "\n" . $task->__toString(), FILE_APPEND);
|
||||||
|
$this->run('tmpcron');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function update(TaskAbstract $task) : void
|
||||||
|
{
|
||||||
|
$this->run('-l > tmpcron');
|
||||||
|
|
||||||
|
$new = '';
|
||||||
|
$fp = \fopen('tmpcron', 'r+');
|
||||||
|
|
||||||
|
if ($fp) {
|
||||||
|
$line = \fgets($fp);
|
||||||
|
while ($line !== false) {
|
||||||
|
if ($line[0] !== '#' && \stripos($line, '/tn = ' . $task->getId()) !== false) {
|
||||||
|
$new .= $task->__toString();
|
||||||
|
} else {
|
||||||
|
$new .= $line . "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
$line = \fgets($fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
\fwrite($fp, $new);
|
||||||
|
\fclose($fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->run('tmpcron');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function delete(TaskAbstract $task) : void
|
||||||
|
{
|
||||||
|
$this->run('-l > tmpcron');
|
||||||
|
|
||||||
|
$new = '';
|
||||||
|
$fp = \fopen('tmpcron', 'r+');
|
||||||
|
|
||||||
|
if ($fp) {
|
||||||
|
$line = \fgets($fp);
|
||||||
|
while ($line !== false) {
|
||||||
|
if ($line[0] !== '#' && \stripos($line, '/tn = ' . $task->getId()) !== false) {
|
||||||
|
$line = \fgets($fp);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$new .= $line . "\n";
|
||||||
|
$line = \fgets($fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
\fwrite($fp, $new);
|
||||||
|
\fclose($fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->run('tmpcron');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,14 @@ use phpOMS\Validation\Base\DateTime;
|
||||||
*/
|
*/
|
||||||
class CronJob extends TaskAbstract
|
class CronJob extends TaskAbstract
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function __toString() : string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,14 @@ use phpOMS\Validation\Base\DateTime;
|
||||||
*/
|
*/
|
||||||
class Schedule extends TaskAbstract
|
class Schedule extends TaskAbstract
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function __toString() : string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -177,10 +177,7 @@ abstract class SchedulerAbstract
|
||||||
*
|
*
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
public function create(TaskAbstract $task) : void
|
abstract public function create(TaskAbstract $task) : void;
|
||||||
{
|
|
||||||
$this->run($task->getCommand());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update task
|
* Update task
|
||||||
|
|
|
||||||
|
|
@ -111,6 +111,15 @@ abstract class TaskAbstract
|
||||||
return $this->id;
|
return $this->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stringify task for direct handling
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*
|
||||||
|
* @since 1.0.0
|
||||||
|
*/
|
||||||
|
abstract public function __toString() : string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get command to create the task
|
* Get command to create the task
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,30 @@ namespace phpOMS\Utils\TaskSchedule;
|
||||||
*/
|
*/
|
||||||
class TaskScheduler extends SchedulerAbstract
|
class TaskScheduler extends SchedulerAbstract
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function create(TaskAbstract $task) : void
|
||||||
|
{
|
||||||
|
$this->run('/Create ' . $task->__toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function update(TaskAbstract $task) : void
|
||||||
|
{
|
||||||
|
$this->run('/Change ' . $task->__toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function delete(TaskAbstract $task) : void
|
||||||
|
{
|
||||||
|
$this->run('/Delete /TN ' . $task->getId());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -25,23 +25,25 @@ class CronTest extends \PHPUnit\Framework\TestCase
|
||||||
|
|
||||||
public function testCRUD()
|
public function testCRUD()
|
||||||
{
|
{
|
||||||
$cron = new Cron();
|
if (\stristr(PHP_OS, 'LINUX')) {
|
||||||
|
$cron = new Cron();
|
||||||
|
|
||||||
self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false));
|
self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false));
|
||||||
|
|
||||||
$cron->create(
|
$cron->create(
|
||||||
new CronJob('testCronJob', 'testFile')
|
new CronJob('testCronJob', 'testFile')
|
||||||
);
|
);
|
||||||
self::assertEquals('testFile', $cron->getRun());
|
self::assertEquals('testFile', $cron->getRun());
|
||||||
|
|
||||||
$cron->update(
|
$cron->update(
|
||||||
new CronJob('testCronJob', 'testFile2')
|
new CronJob('testCronJob', 'testFile2')
|
||||||
);
|
);
|
||||||
self::assertEquals('testFile2', $cron->getRun());
|
self::assertEquals('testFile2', $cron->getRun());
|
||||||
|
|
||||||
$cron->delete(
|
$cron->delete(
|
||||||
new CronJob('testCronJob', 'testFile2')
|
new CronJob('testCronJob', 'testFile2')
|
||||||
);
|
);
|
||||||
self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false));
|
self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,4 +21,28 @@ class TaskSchedulerTest extends \PHPUnit\Framework\TestCase
|
||||||
{
|
{
|
||||||
self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\SchedulerAbstract', new TaskScheduler());
|
self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\SchedulerAbstract', new TaskScheduler());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCRUD()
|
||||||
|
{
|
||||||
|
if (\stristr(PHP_OS, 'WIN')) {
|
||||||
|
$cron = new Cron();
|
||||||
|
|
||||||
|
self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false));
|
||||||
|
|
||||||
|
$cron->create(
|
||||||
|
new CronJob('testCronJob', 'testFile')
|
||||||
|
);
|
||||||
|
self::assertEquals('testFile', $cron->getRun());
|
||||||
|
|
||||||
|
$cron->update(
|
||||||
|
new CronJob('testCronJob', 'testFile2')
|
||||||
|
);
|
||||||
|
self::assertEquals('testFile2', $cron->getRun());
|
||||||
|
|
||||||
|
$cron->delete(
|
||||||
|
new CronJob('testCronJob', 'testFile2')
|
||||||
|
);
|
||||||
|
self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user