Some kind of Kernel commands Listener
Hello guys,
As we all have lot's of CRON commands that sit in the app/Console/Commands.
Now quite recently we had a request to log start and end of the Commands.
Since this seems to me at once like something to be abstracted. I was wondering how?
Now I could use the Trait and hide the function of storing the payload into the proper table.
But then I'll need to go to each and every Console Command and add boilerplate code.
Doesn't that sound to you too funky and old school?
Is there any way to do it more elegantly or am I wrong?
How it is currently implemented.
app/Console/Commands/PoolRepayment.php
<?php
namespace App\Console\Commands;
use App\DataTransferObjects\CronRunLogData;
...
class PoolRepayment extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'pool:repayment';
/**
* The console command description.
*
* @var string
*/
protected $description = 'This command is use for pool full repayment when it has expired date';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct(
private readonlyPoolService $poolService,
private readonlyUpdatePoolInvestmentAction $updatePoolInvestmentAction,
...
) {
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
/* @covers \Tests\Http\PoolInvestTest::pool_repayment_on_expired_date */
public function handle(): bool
{
try {
$payload = new CronRunLogData(
command_name:$this->signature,
status:CronRunLogEnum::STATUS_STARTED->value,
start_time:Carbon::now(),
);
$cronLogObject = app(CreateCronRunLogAction::class)->do($payload);
// ..... IN THE END of handle()
$payload = new CronRunLogData(
status:CronRunLogEnum::STATUS_COMPLETED->value,
end_time:Carbon::now(),
cron_run_log:$cronLogObject['instance'],
);
app(UpdateCronRunLogAction::class)->do($payload);
return true;
}
}
}
Please advise.
Thank you.
Please or to participate in this conversation.