Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sandbox task loading #243

Merged
merged 6 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed

- [#244] Fix AppVeyor CI
- [#243] Sandbox task loading

## [v1.12.1] - 2019-05-01

Expand Down Expand Up @@ -184,8 +185,8 @@ In `v2` this will result in exception.

- [#77] Fix high cpu usage


[#244]: https://github.com/lavary/crunz/pull/244
[#243]: https://github.com/lavary/crunz/pull/243
[#229]: https://github.com/lavary/crunz/pull/229
[#217]: https://github.com/lavary/crunz/pull/217
[#210]: https://github.com/lavary/crunz/pull/210
Expand Down
5 changes: 5 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use Crunz\Output\OutputFactory;
use Crunz\Schedule\ScheduleFactory;
use Crunz\Task\Collection;
use Crunz\Task\Loader;
use Crunz\Task\LoaderInterface;
use Crunz\Task\Timezone;
use Crunz\Timezone\Provider;
use Crunz\Timezone\ProviderInterface;
Expand All @@ -53,6 +55,7 @@
CurlHttpClient::class,
FilesystemInterface::class => CrunzFilesystem::class,
FinderInterface::class => Finder::class,
LoaderInterface::class => Loader::class,
];

$container
Expand All @@ -65,6 +68,7 @@
new Reference(EventRunner::class),
new Reference(Timezone::class),
new Reference(ScheduleFactory::class),
new Reference(LoaderInterface::class),
]
)
;
Expand All @@ -90,6 +94,7 @@
[
new Reference(Configuration::class),
new Reference(Collection::class),
new Reference(LoaderInterface::class),
]
)
;
Expand Down
29 changes: 14 additions & 15 deletions src/Console/Command/ScheduleListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Crunz\Console\Command;

use Crunz\Configuration\Configuration;
use Crunz\Schedule;
use Crunz\Task\Collection;
use Crunz\Task\LoaderInterface;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -16,11 +16,17 @@ class ScheduleListCommand extends Command
private $configuration;
/** @var Collection */
private $taskCollection;

public function __construct(Configuration $configuration, Collection $taskCollection)
{
/** @var LoaderInterface */
private $taskLoader;

public function __construct(
Configuration $configuration,
Collection $taskCollection,
LoaderInterface $taskLoader
) {
$this->configuration = $configuration;
$this->taskCollection = $taskCollection;
$this->taskLoader = $taskLoader;

parent::__construct();
}
Expand Down Expand Up @@ -73,18 +79,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
);
$row = 0;

foreach ($tasks as $taskFile) {
$schedule = require $taskFile->getRealPath();
if (!$schedule instanceof Schedule) {
// @TODO throw exception in v2
@\trigger_error(
"File '{$taskFile->getRealPath()}' didn't return '\Crunz\Schedule' instance, this behavior is deprecated since v1.12 and will result in exception in v2.0+",
E_USER_DEPRECATED
);

continue;
}
$schedules = $this->taskLoader
->load(...\array_values($tasks))
;

foreach ($schedules as $schedule) {
$events = $schedule->events();
foreach ($events as $event) {
$table->addRow(
Expand Down
34 changes: 9 additions & 25 deletions src/Console/Command/ScheduleRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Crunz\EventRunner;
use Crunz\Schedule;
use Crunz\Task\Collection;
use Crunz\Task\LoaderInterface;
use Crunz\Task\TaskNumber;
use Crunz\Task\Timezone;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -15,12 +16,6 @@

class ScheduleRunCommand extends Command
{
/**
* Running tasks.
*
* @var array
*/
protected $runningEvents = [];
/** @var Collection */
private $taskCollection;
/** @var Configuration */
Expand All @@ -31,19 +26,23 @@ class ScheduleRunCommand extends Command
private $taskTimezone;
/** @var Schedule\ScheduleFactory */
private $scheduleFactory;
/** @var LoaderInterface */
private $taskLoader;

public function __construct(
Collection $taskCollection,
Configuration $configuration,
EventRunner $eventRunner,
Timezone $taskTimezone,
Schedule\ScheduleFactory $scheduleFactory
Schedule\ScheduleFactory $scheduleFactory,
LoaderInterface $taskLoader
) {
$this->taskCollection = $taskCollection;
$this->configuration = $configuration;
$this->eventRunner = $eventRunner;
$this->taskTimezone = $taskTimezone;
$this->scheduleFactory = $scheduleFactory;
$this->taskLoader = $taskLoader;

parent::__construct();
}
Expand Down Expand Up @@ -102,28 +101,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

// List of schedules
$schedules = [];
$schedules = $this->taskLoader
->load(...\array_values($files))
;
$tasksTimezone = $this->taskTimezone
->timezoneForComparisons()
;

foreach ($files as $file) {
$schedule = require $file->getRealPath();
if (!$schedule instanceof Schedule) {
// @TODO throw exception in v2
@\trigger_error(
"File '{$file->getRealPath()}' didn't return '\Crunz\Schedule' instance, this behavior is deprecated since v1.12 and will result in exception in v2.0+",
E_USER_DEPRECATED
);

continue;
}

if (\count($schedule->events())) {
$schedules[] = $schedule;
}
}

// Is specified task should be invoked?
if (\is_string($task)) {
$schedules = $this->scheduleFactory
Expand Down
41 changes: 41 additions & 0 deletions src/Task/Loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Crunz\Task;

use Crunz\Schedule;

final class Loader implements LoaderInterface
{
/** @return Schedule[] */
public function load(\SplFileInfo ...$files)
{
$schedules = [];
foreach ($files as $file) {
/**
* Actual "require" is in separated method to make sure
* local variables are not overwritten by required file
* See: https://github.com/lavary/crunz/issues/242 for more information.
*/
$schedule = $this->loadSchedule($file);
if (!$schedule instanceof Schedule) {
// @TODO throw exception in v2
@\trigger_error(
"File '{$file->getRealPath()}' didn't return '\Crunz\Schedule' instance, this behavior is deprecated since v1.12 and will result in exception in v2.0+",
E_USER_DEPRECATED
);

continue;
}

$schedules[] = $schedule;
}

return $schedules;
}

/** @return Schedule */
private function loadSchedule(\SplFileInfo $file)
{
return require $file->getRealPath();
}
}
11 changes: 11 additions & 0 deletions src/Task/LoaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Crunz\Task;

use Crunz\Schedule;

interface LoaderInterface
{
/** @return Schedule[] */
public function load(\SplFileInfo ...$files);
}
12 changes: 10 additions & 2 deletions tests/Unit/Console/Command/ScheduleRunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Crunz\EventRunner;
use Crunz\Schedule;
use Crunz\Task\Collection;
use Crunz\Task\Loader;
use Crunz\Task\Timezone;
use Crunz\Tests\TestCase\TemporaryFile;
use PHPUnit\Framework\TestCase;
Expand All @@ -33,7 +34,8 @@ public function forceRunAllTasks()
$this->createMock(Configuration::class),
$mockEventRunner,
$this->createMock(Timezone::class),
$this->createMock(Schedule\ScheduleFactory::class)
$this->createMock(Schedule\ScheduleFactory::class),
$this->createTaskLoader()
);

$command->run(
Expand All @@ -60,7 +62,8 @@ public function runSpecificTask()
$this->createMock(Configuration::class),
$mockEventRunner,
$this->mockTimezoneProvider(),
$this->mockScheduleFactory()
$this->mockScheduleFactory(),
$this->createTaskLoader()
);

$command->run(
Expand Down Expand Up @@ -199,4 +202,9 @@ private function phpVersionTaskContent()
return $schedule;
PHP;
}

private function createTaskLoader()
{
return new Loader();
}
}