Skip to content

Commit

Permalink
Implement events for cron expenses; add processing of daily/monthly e…
Browse files Browse the repository at this point in the history
…xpenses #136
  • Loading branch information
nabeelio committed Mar 17, 2018
1 parent d34de09 commit 067fb0f
Show file tree
Hide file tree
Showing 24 changed files with 623 additions and 118 deletions.
29 changes: 21 additions & 8 deletions app/Console/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -33,6 +50,8 @@ public function readFile($filename)
* @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)
{
Expand All @@ -46,18 +65,12 @@ public function runCommand($cmd, $return=false, $verbose=true)

$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;
Expand Down
24 changes: 24 additions & 0 deletions app/Console/Cron/Monthly.php
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions app/Console/Cron/Nightly.php
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions app/Console/Cron/Weekly.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
15 changes: 9 additions & 6 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 changes: 23 additions & 0 deletions app/Console/Logger.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function up()
$table->string('ref_class', 50)->nullable();
$table->string('ref_class_id', 36)->nullable();
$table->timestamps();
$table->dateTime('post_date');
$table->date('post_date');

$table->primary('id');
$table->index('journal_id');
Expand Down
20 changes: 18 additions & 2 deletions app/Database/seeds/sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ expenses:
ref_class_id: 1
created_at: now
updated_at: now
- name: Catering Staff
amount: 1000
type: 1
active: 1
ref_class: App\Models\Subfleet
ref_class_id: 1
created_at: now
updated_at: now
- name: Maintenance
amount: 1000
type: 1
active: 1
ref_class: App\Models\Aircraft
ref_class_id: 1
created_at: now
updated_at: now

fares:
- id: 1
Expand Down Expand Up @@ -448,7 +464,7 @@ journals:
id: 1
ledger_id: null
type: 0
balance: 23710000
balance: 7870000
currency: USD
morphed_type: App\Models\Airline
morphed_id: 1
Expand All @@ -458,7 +474,7 @@ journals:
id: 2
ledger_id: null
type: 1
balance: 45000
balance: 15000
currency: USD
morphed_type: App\Models\User
morphed_id: 1
Expand Down
21 changes: 21 additions & 0 deletions app/Events/CronMonthly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

/**
* This event is dispatched when the monthly cron is run
* It happens after all of the default nightly tasks
* @package App\Events
*/
class CronMonthly
{
use Dispatchable, SerializesModels;

public function __construct()
{

}
}
21 changes: 21 additions & 0 deletions app/Events/CronNightly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

/**
* This event is dispatched when the daily cron is run
* It happens after all of the default nightly tasks
* @package App\Events
*/
class CronNightly
{
use Dispatchable, SerializesModels;

public function __construct()
{

}
}
21 changes: 21 additions & 0 deletions app/Events/CronWeekly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

/**
* This event is dispatched when the weekly cron is run
* It happens after all of the default nightly tasks
* @package App\Events
*/
class CronWeekly
{
use Dispatchable, SerializesModels;

public function __construct()
{

}
}
33 changes: 33 additions & 0 deletions app/Listeners/Cron/Monthly/ApplyExpenses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Listeners\Cron\Monthly;

use App\Events\CronMonthly;
use App\Models\Enums\ExpenseType;
use App\Services\Finance\RecurringFinanceService;

/**
* Go through and apply any finances that are daily
* @package App\Listeners\Cron\Nightly
*/
class ApplyExpenses
{
private $dfSvc;

public function __construct(RecurringFinanceService $dfSvc)
{
$this->dfSvc = $dfSvc;
}

/**
* Apply all of the expenses for a month
* @param CronMonthly $event
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function handle(CronMonthly $event)
{
$this->dfSvc->processExpenses(ExpenseType::MONTHLY);
}
}
33 changes: 33 additions & 0 deletions app/Listeners/Cron/Nightly/ApplyExpenses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Listeners\Cron\Nightly;

use App\Events\CronNightly;
use App\Models\Enums\ExpenseType;
use App\Services\Finance\RecurringFinanceService;

/**
* Go through and apply any finances that are daily
* @package App\Listeners\Cron\Nightly
*/
class ApplyExpenses
{
private $dfSvc;

public function __construct(RecurringFinanceService $dfSvc)
{
$this->dfSvc = $dfSvc;
}

/**
* Apply all of the expenses for a day
* @param CronNightly $event
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function handle(CronNightly $event): void
{
$this->dfSvc->processExpenses(ExpenseType::DAILY);
}
}
Loading

0 comments on commit 067fb0f

Please sign in to comment.