mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-21 13: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
|
||||
{
|
||||
/**
|
||||
* {@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}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -26,6 +26,14 @@ use phpOMS\Validation\Base\DateTime;
|
|||
*/
|
||||
class CronJob extends TaskAbstract
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __toString() : string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -26,6 +26,14 @@ use phpOMS\Validation\Base\DateTime;
|
|||
*/
|
||||
class Schedule extends TaskAbstract
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __toString() : string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user