Skip to content

Instantly share code, notes, and snippets.

@sanasol
Created March 8, 2018 19:06
Show Gist options
  • Save sanasol/606d6be4f405dc606d50954305d097cc to your computer and use it in GitHub Desktop.
Save sanasol/606d6be4f405dc606d50954305d097cc to your computer and use it in GitHub Desktop.
<?php
require __DIR__.'/../helpers/helpers_override.php';
// Insert before autoload.php
// require __DIR__.'/../vendor/autoload.php';
<?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);
}
}
<?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