Created
March 8, 2018 19:06
-
-
Save sanasol/606d6be4f405dc606d50954305d097cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require __DIR__.'/../helpers/helpers_override.php'; | |
// Insert before autoload.php | |
// require __DIR__.'/../vendor/autoload.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Core\Console\Commands; | |
use Illuminate\Console\Command; | |
use Carbon\Carbon; | |
use DB; | |
class QueueStats extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'queue:stats'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Queue jobs statistics'; | |
/** | |
* Execute the console command. | |
* | |
* @return mixed | |
*/ | |
public function handle() | |
{ | |
$active = DB::table('queue_monitor')->whereNull('finished_at')->count(); | |
$active_hour = DB::table('queue_monitor') | |
->whereNull('finished_at') | |
->where('created_at', Carbon::now()->subHours(1)) | |
->count(); | |
$failed = DB::table('queue_monitor')->where('failed', true)->count(); | |
$failed_hour = DB::table('queue_monitor') | |
->where('failed', true) | |
->where('created_at', Carbon::now()->subHours(1)) | |
->count(); | |
$done = DB::table('queue_monitor')->whereNotNull('finished_at')->where('failed', false)->count(); | |
$done_hour = DB::table('queue_monitor') | |
->whereNotNull('finished_at') | |
->where('failed', false) | |
->where('created_at', Carbon::now()->subHours(1)) | |
->count(); | |
$delay = DB::table('queue_monitor')->selectRaw('avg(started_at-created_at) as avg')->whereNotNull('started_at')->first(); | |
$delay_hour = DB::table('queue_monitor') | |
->selectRaw('avg(started_at-created_at) as avg') | |
->whereNotNull('started_at') | |
->where('created_at', Carbon::now()->subHours(1)) | |
->first(); | |
$worktime = DB::table('queue_monitor')->selectRaw('avg(finished_at-started_at) as avg')->whereNotNull('finished_at')->first(); | |
$worktime_hour = DB::table('queue_monitor') | |
->selectRaw('avg(finished_at-started_at) as avg') | |
->whereNotNull('finished_at') | |
->where('created_at', Carbon::now()->subHours(1)) | |
->first(); | |
$data[] = ['Queued', $active, $active_hour]; | |
$data[] = ['Failed', $failed, $failed_hour]; | |
$data[] = ['Done', $done, $done_hour]; | |
$data[] = ['Avg waiting time', $delay->avg, ($delay_hour->avg) ? $delay_hour->avg:'-']; | |
$data[] = ['Avg work time', $worktime->avg, ($worktime->avg) ? $worktime->avg:'-']; | |
$this->table(['Metric', 'All time', 'Last hour'], $data); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Contracts\Bus\Dispatcher; | |
use Carbon\Carbon; | |
function dispatch($job) | |
{ | |
$_job = app(Dispatcher::class)->dispatch($job); | |
DB::table('queue_monitor')->insert([ | |
'job_id' => $_job, | |
'name' => get_class($job), | |
'started_at' => Carbon::now(), | |
]); | |
return $_job; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment