Implement events for cron expenses; add processing of daily/monthly expenses #136
This commit is contained in:
@@ -3,13 +3,30 @@
|
||||
namespace App\Console;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class BaseCommand extends Command
|
||||
{
|
||||
/**
|
||||
* Splice the logger and replace the handler to direct
|
||||
* everything to stdout as well as the log
|
||||
*/
|
||||
public function redirectLoggingToStdout()
|
||||
{
|
||||
$logger = app(\Illuminate\Log\Logger::class);
|
||||
$handlers = $logger->getHandlers();
|
||||
|
||||
try {
|
||||
$handlers[] = new StreamHandler('php://stdout');
|
||||
$logger->setHandlers($handlers);
|
||||
} catch (\Exception $e) {
|
||||
$this->error('Couldn\'t splice the logger');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Streaming file read
|
||||
* Streaming file reader
|
||||
* @param $filename
|
||||
* @return \Generator
|
||||
*/
|
||||
@@ -33,6 +50,8 @@ class BaseCommand extends Command
|
||||
* @param $cmd
|
||||
* @param bool $return
|
||||
* @return string
|
||||
* @throws \Symfony\Component\Process\Exception\RuntimeException
|
||||
* @throws \Symfony\Component\Process\Exception\LogicException
|
||||
*/
|
||||
public function runCommand($cmd, $return=false, $verbose=true)
|
||||
{
|
||||
@@ -46,18 +65,12 @@ class BaseCommand extends Command
|
||||
|
||||
$val = '';
|
||||
$process = new Process($cmd);
|
||||
$process->run(function ($type, $buffer) use ($return, $val) {
|
||||
$process->run(function ($type, $buffer) use ($return, &$val) {
|
||||
if ($return) {
|
||||
$val .= $buffer;
|
||||
} else {
|
||||
echo $buffer;
|
||||
}
|
||||
|
||||
/*if (Process::ERR === $type) {
|
||||
echo $buffer;
|
||||
} else {
|
||||
echo $buffer;
|
||||
}*/
|
||||
});
|
||||
|
||||
return $val;
|
||||
|
||||
24
app/Console/Cron/Monthly.php
Normal file
24
app/Console/Cron/Monthly.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Cron;
|
||||
|
||||
use App\Console\BaseCommand;
|
||||
use App\Events\CronMonthly;
|
||||
|
||||
/**
|
||||
* This just calls the CronMonthly event, so all of the
|
||||
* listeners, etc can just be called to run those tasks
|
||||
* @package App\Console\Cron
|
||||
*/
|
||||
class Monthly extends BaseCommand
|
||||
{
|
||||
protected $signature = 'cron:monthly';
|
||||
protected $description = 'Run the monthly cron tasks';
|
||||
protected $schedule;
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$this->redirectLoggingToStdout();
|
||||
event(new CronMonthly());
|
||||
}
|
||||
}
|
||||
24
app/Console/Cron/Nightly.php
Normal file
24
app/Console/Cron/Nightly.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Cron;
|
||||
|
||||
use App\Console\BaseCommand;
|
||||
use App\Events\CronNightly;
|
||||
|
||||
/**
|
||||
* This just calls the CronNightly event, so all of the
|
||||
* listeners, etc can just be called to run those tasks
|
||||
* @package App\Console\Cron
|
||||
*/
|
||||
class Nightly extends BaseCommand
|
||||
{
|
||||
protected $signature = 'cron:nightly';
|
||||
protected $description = 'Run the nightly cron tasks';
|
||||
protected $schedule;
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$this->redirectLoggingToStdout();
|
||||
event(new CronNightly());
|
||||
}
|
||||
}
|
||||
24
app/Console/Cron/Weekly.php
Normal file
24
app/Console/Cron/Weekly.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Cron;
|
||||
|
||||
use App\Console\BaseCommand;
|
||||
use App\Events\CronMonthly;
|
||||
|
||||
/**
|
||||
* This just calls the CronWeekly event, so all of the
|
||||
* listeners, etc can just be called to run those tasks
|
||||
* @package App\Console\Cron
|
||||
*/
|
||||
class Weekly extends BaseCommand
|
||||
{
|
||||
protected $signature = 'cron:monthly';
|
||||
protected $description = 'Run the monthly cron tasks';
|
||||
protected $schedule;
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$this->redirectLoggingToStdout();
|
||||
event(new CronMonthly());
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Console\Cron\Monthly;
|
||||
use App\Console\Cron\Nightly;
|
||||
use App\Console\Cron\Weekly;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
@@ -25,24 +28,24 @@ class Kernel extends ConsoleKernel
|
||||
|
||||
/**
|
||||
* Define the application's command schedule.
|
||||
*
|
||||
* @param \Illuminate\Console\Scheduling\Schedule $schedule
|
||||
* @return void
|
||||
*/
|
||||
protected function schedule(Schedule $schedule)
|
||||
protected function schedule(Schedule $schedule): void
|
||||
{
|
||||
// $schedule->command('inspire')
|
||||
// ->hourly();
|
||||
$schedule->command(Nightly::class)->dailyAt('01:00');
|
||||
$schedule->command(Weekly::class)->weeklyOn(0);
|
||||
$schedule->command(Monthly::class)->monthlyOn(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the Closure based commands for the application.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function commands()
|
||||
protected function commands(): void
|
||||
{
|
||||
require app_path('Routes/console.php');
|
||||
$this->load(__DIR__ . '/Commands');
|
||||
$this->load(__DIR__ . '/Cron');
|
||||
}
|
||||
}
|
||||
|
||||
23
app/Console/Logger.php
Normal file
23
app/Console/Logger.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use Monolog\Handler\StreamHandler;
|
||||
|
||||
/**
|
||||
* Just a simple custom logger that dumps to the console
|
||||
* @package App\Console
|
||||
*/
|
||||
class Logger
|
||||
{
|
||||
public function __invoke(array $config)
|
||||
{
|
||||
$logger = new \Monolog\Logger('console');
|
||||
try {
|
||||
$logger->pushHandler(new StreamHandler('php://stdout'));
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
|
||||
return $logger;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user