Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
panlatent committed Jun 18, 2024
1 parent 86fe616 commit c1f1cec
Show file tree
Hide file tree
Showing 38 changed files with 595 additions and 187 deletions.
4 changes: 1 addition & 3 deletions abstract/ActionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace panlatent\craft\actions\abstract;

use craft\base\SavableComponentInterface;

interface ActionInterface extends SavableComponentInterface
interface ActionInterface
{
public function execute(ContextInterface $context): bool;
}
4 changes: 1 addition & 3 deletions abstract/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace panlatent\craft\actions\abstract;

use yii\base\Component;

abstract class Context extends Component implements ContextInterface
abstract class Context implements ContextInterface
{

}
3 changes: 3 additions & 0 deletions abstract/ContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace panlatent\craft\actions\abstract;

use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

interface ContextInterface
{
public function getContainer(): ContainerInterface;

public function getErrors(): array;

public function getLogger(): LoggerInterface;
Expand Down
8 changes: 8 additions & 0 deletions abstract/RunnerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace panlatent\craft\actions\abstract;

interface RunnerInterface
{
public static function run(): void;
}
2 changes: 1 addition & 1 deletion abstract/TriggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface TriggerInterface
{
public function trigger(): bool;
public function handle(): bool;
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"docs": "https://docs.panlatent.com/projects/schedule",
"issues": "https://github.com/panlatent/schedule/issues"
},
"license": "MIT",
"license": "proprietary",
"authors": [
{
"name": "[email protected]",
Expand All @@ -31,6 +31,7 @@
"nesbot/carbon": "^1.22 || ^2.10 || ^3.0",
"panlatent/craft-action-abstract": "@dev",
"panlatent/cron-expression-descriptor": "^1.1",
"psr/container": "^1.0 || ^2.0",
"psr/log": "^1.0 || ^2.0 || ^3.0",
"react/event-loop": "^1.5",
"symfony/process": "^6.0 || ^7.0"
Expand Down
10 changes: 10 additions & 0 deletions config/schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

return [
'schedules' => [
Schedule::closure(static fn() => true)->minute(),

Schedule::request('')->hourly()->onSuccess(function() {

}),

Schedule::exec('ls')->hourly()->onSuccess(function() {

}),

Schedule::console('db/backup')->hourly()->onSuccess(function() {

}),
],

];
4 changes: 2 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/
class Plugin extends \craft\base\Plugin
{
public const EDITION_STANDARD = 'standard';
public const EDITION_LITE = 'lite';
public const EDITION_PRO = 'pro';

// Properties
Expand Down Expand Up @@ -76,7 +76,7 @@ public static function config(): array
public static function editions(): array
{
return [
self::EDITION_STANDARD,
self::EDITION_LITE,
self::EDITION_PRO,
];
}
Expand Down
2 changes: 0 additions & 2 deletions src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public function listen(\Closure $callback = null): void

public function dispatch(): void
{


$timers = $this->getTriggerTimers();

foreach ($timers as $timer) {
Expand Down
17 changes: 17 additions & 0 deletions src/actions/Closure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace panlatent\schedule\actions;

use panlatent\craft\actions\abstract\Action;
use panlatent\craft\actions\abstract\ContextInterface;

class Closure extends Action
{
public ?\Closure $closure = null;

public function execute(ContextInterface $context): bool
{
return true;
}

}
28 changes: 20 additions & 8 deletions src/actions/HttpRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use craft\helpers\Json;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use panlatent\craft\actions\abstract\Action;
use panlatent\craft\actions\abstract\ActionInterface;
use panlatent\craft\actions\abstract\ContextInterface;
use panlatent\craft\actions\abstract\OutputInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

class HttpRequest extends Action
Expand Down Expand Up @@ -41,24 +44,33 @@ class HttpRequest extends Action
*/
public string $body = '';

public static function run()
{

}

public function execute(ContextInterface $context): bool
{
$client = new Client();
try {
$client = $context->getContainer()->get(ClientInterface::class);
} catch (NotFoundExceptionInterface) {
$client = new Client();
}
return $this->executeWithClient($context, $client);
}

public function executeWithClient(ContextInterface $context, ClientInterface $client): bool
{
$response = $client->request($this->method, $this->url, $this->getOptions());
$statusCode = $response->getStatusCode();

$context->setOutput(new class($response) implements OutputInterface {
public function __construct(public ResponseInterface $response) {}
});
// $context->setOutput(new class($response) implements OutputInterface {
// public function __construct(public ResponseInterface $response) {}
// });

return $statusCode >= 200 && $statusCode < 400;
}

protected function getClient(): Client
{
return new Client();
}

protected function getOptions(): array
{
Expand Down
33 changes: 1 addition & 32 deletions src/base/Timer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,11 @@ public function rules(): array
$rules = parent::rules();
$rules[] = [['scheduleId', 'enabled'], 'required'];
$rules[] = [['scheduleId', 'sortOrder'], 'integer'];
$rules[] = [['minute', 'hour', 'day', 'month', 'week'], 'string'];
$rules[] = [['enabled'], 'boolean'];
$rules[] = [['minute', 'hour', 'day', 'month', 'week'], function($property) {
if ($this->$property === null || $this->$property === '') {
$this->$property = '*';
}
}];

return $rules;
}

public function trigger(): bool
public function handle(): bool
{
return $this->getSchedule()->run();
}
Expand All @@ -81,14 +74,6 @@ public function isValid(): bool
return $this->getSchedule()->isValid() && $this->enabled;
}

/**
* @inheritdoc
*/
public function getCronExpression(): string
{
return sprintf('%s %s %s %s %s', $this->minute, $this->hour, $this->day, $this->month, $this->week);
}

/**
* @return Schedule
* @throws InvalidConfigException
Expand All @@ -113,20 +98,4 @@ public function setSchedule(Schedule $schedule): void
{
$this->_schedule = $schedule;
}

/**
* @return string
*/
public function getCronDescription(): string
{
return CronHelper::toDescription($this->getCronExpression());
}

/**
* @param string $cron
*/
public function setCronExpression(string $cron): void
{
[$this->minute, $this->hour, $this->day, $this->month, $this->week, ] = explode(' ', $cron);
}
}
7 changes: 0 additions & 7 deletions src/base/TimerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ interface TimerInterface extends TriggerInterface
*/
public function isValid(): bool;

/**
* Returns cron expression.
*
* @return string
*/
public function getCronExpression(): string;

/**
* @return Schedule
*/
Expand Down
25 changes: 0 additions & 25 deletions src/base/TimerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,6 @@ trait TimerTrait
*/
public ?int $scheduleId = null;

/**
* @var string|null
*/
public ?string $minute = null;

/**
* @var string|null
*/
public ?string $hour = null;

/**
* @var string|null
*/
public ?string $day = null;

/**
* @var string|null
*/
public ?string $month = null;

/**
* @var string|null
*/
public ?string $week = null;

/**
* @var bool|null
*/
Expand Down
20 changes: 20 additions & 0 deletions src/builder/Interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,28 @@

trait Interval
{
public function minute(): static
{
return $this;
}

public function daily(): static
{
return $this;
}

public function hourly(): static
{
return $this;
}

public function monthly(): static
{
return $this;
}

public function yearly(): static
{
return $this;
}
}
24 changes: 23 additions & 1 deletion src/builder/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace panlatent\schedule\builder;

use panlatent\craft\actions\abstract\ActionInterface;
use panlatent\schedule\actions\Closure;
use panlatent\schedule\actions\Command;
use panlatent\schedule\actions\Console;
use panlatent\schedule\actions\HttpRequest;
use panlatent\schedule\models\Schedule as ScheduleModel;

final class Schedule
{
Expand All @@ -16,16 +18,36 @@ public function __construct(protected ActionInterface $action)

}

public static function command(string $command, array $arguments = [])
public static function closure(\Closure $closure)
{
$action = new Closure();
$action->closure = $closure;
return new Schedule($action);
}

public static function exec(string $command, array $arguments = [])
{
$action = new Command();
return new Schedule($action);
}

public static function console(string $command, array $arguments = [])
{
$action = new Console();
return new Schedule($action);
}

public static function request(string $url)
{
$action = new HttpRequest();
$action->url = $url;
return new Schedule($action);
}

public function create(): ScheduleModel
{
$schedule = new ScheduleModel();
$schedule->action = $this->action;
return $schedule;
}
}
2 changes: 2 additions & 0 deletions src/console/SchedulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public function actionList(): void
$i = 1;
$rows = [];



$ungroupedSchedules = $schedules->getSchedulesByGroupId();
foreach ($ungroupedSchedules as $schedule) {
if ($schedule->static) {
Expand Down
Loading

0 comments on commit c1f1cec

Please sign in to comment.