Skip to content

Commit

Permalink
#20 - add scheduler (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHDOLEK authored Nov 22, 2023
1 parent 6f30adf commit bb102cd
Show file tree
Hide file tree
Showing 25 changed files with 321 additions and 135 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ junit.xml
clover.xml
tests/*.txt
/test/*/*Success__1.json
**/__snapshots__/
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,29 @@ class CreateUserConsoleCommand extends Command
}
```

### Scheduling Commands

To schedule a command, we use the GO\Scheduler class. This class allows us to define the timing and frequency of command execution. Here's an example of how to schedule a command to run daily:

```php
$scheduler = new GO\Scheduler();
$scheduler->php('/path/to/command app:user:create')->daily();
$scheduler->run();
```
In this example, the app:user:create command is scheduled to run every day.

#### Running the Scheduler
The scheduler should be triggered by a system cron job to ensure it runs at regular intervals. Typically, you would set up a cron job to execute a PHP script that initializes and runs the scheduler.

For instance, a cron job running every minute might look like this:

```bash
* * * * * ./bin/console.php schedule
```

This setup ensures that your scheduled commands are executed reliably and on time.


### Domain event and event handlers

The framework implements the amqp protocol with handlers that allow events to be easily pushed onto the queue.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"fakerphp/faker": "^1.23",
"lcobucci/clock": "^3.1",
"monolog/monolog": "^2.8",
"peppeocchi/php-cron-scheduler": "^4.0",
"php-amqplib/php-amqplib": "^3.2",
"php-di/php-di": "^7.0",
"php-di/slim-bridge": "^3.4",
Expand Down
240 changes: 180 additions & 60 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ deptrac:
- App\Infrastructure\AMQP\Queue\QueueContainer
- App\Infrastructure\AMQP\Consumer
- App\Infrastructure\AMQP\Queue\QueueContainer
App\Application\Console\ScheduleConsoleCommand:
- App\Infrastructure\Environment\Settings
4 changes: 3 additions & 1 deletion src/Application/Console/AmqpConsumeConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: "app:amqp:consume", description: "Start consuming a given AMQP queue")]
class AmqpConsumeConsoleCommand extends Command implements SignalableCommandInterface
class AmqpConsumeConsoleCommand extends ConsoleCommand implements SignalableCommandInterface
{
public static string $signature;

public function __construct(
private readonly QueueContainer $queueContainer,
private readonly Consumer $consumer,
Expand Down
8 changes: 7 additions & 1 deletion src/Application/Console/CacheClearConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: "app:cache:clear", description: "Clear all caches")]
class CacheClearConsoleCommand extends Command
class CacheClearConsoleCommand extends ConsoleCommand
{
public static string $signature;

public function __construct(
private readonly Settings $settings,
) {
Expand All @@ -21,6 +23,8 @@ public function __construct(

protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln("<info>Start clear cache</info>");

$cacheDirs = [$this->settings->get("doctrine.cache_dir"), $this->settings->get("slim.cache_dir")];

foreach ($cacheDirs as $cacheDir) {
Expand All @@ -31,6 +35,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->removeDirectory($cacheDir);
}

$output->writeln("<info>Done clear cache</info>");

return Command::SUCCESS;
}

Expand Down
Loading

0 comments on commit bb102cd

Please sign in to comment.