This commit is contained in:
Dennis Eichhorn 2018-08-02 19:18:34 +02:00
parent ed3fc48e34
commit a33a7be954
8 changed files with 159 additions and 19 deletions

View File

@ -25,6 +25,74 @@ namespace phpOMS\Utils\TaskSchedule;
*/
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}
*/

View File

@ -26,6 +26,14 @@ use phpOMS\Validation\Base\DateTime;
*/
class CronJob extends TaskAbstract
{
/**
* {@inheritdoc}
*/
public function __toString() : string
{
return '';
}
/**
* {@inheritdoc}
*/

View File

@ -26,6 +26,14 @@ use phpOMS\Validation\Base\DateTime;
*/
class Schedule extends TaskAbstract
{
/**
* {@inheritdoc}
*/
public function __toString() : string
{
return '';
}
/**
* {@inheritdoc}
*/

View File

@ -177,10 +177,7 @@ abstract class SchedulerAbstract
*
* @since 1.0.0
*/
public function create(TaskAbstract $task) : void
{
$this->run($task->getCommand());
}
abstract public function create(TaskAbstract $task) : void;
/**
* Update task

View File

@ -111,6 +111,15 @@ abstract class TaskAbstract
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
*

View File

@ -25,6 +25,30 @@ namespace phpOMS\Utils\TaskSchedule;
*/
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}
*/

View File

@ -25,23 +25,25 @@ class CronTest extends \PHPUnit\Framework\TestCase
public function testCRUD()
{
$cron = new Cron();
if (\stristr(PHP_OS, 'LINUX')) {
$cron = new Cron();
self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false));
$cron->create(
new CronJob('testCronJob', 'testFile')
);
self::assertEquals('testFile', $cron->getRun());
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->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));
$cron->delete(
new CronJob('testCronJob', 'testFile2')
);
self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false));
}
}
}

View File

@ -21,4 +21,28 @@ class TaskSchedulerTest extends \PHPUnit\Framework\TestCase
{
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));
}
}
}