Skip to content

Instantly share code, notes, and snippets.

@macedd
Last active August 6, 2018 18:56
Show Gist options
  • Save macedd/a8b7d03a632a6c5ffb8a2ec2952adfeb to your computer and use it in GitHub Desktop.
Save macedd/a8b7d03a632a6c5ffb8a2ec2952adfeb to your computer and use it in GitHub Desktop.
Lumen Horizon integration Service Provider
<?php
/**
* Horizon Helpers required
**/
use Illuminate\Support\Str;
use Illuminate\Support\HtmlString;
if ( ! function_exists('config_path'))
{
/**
* Get the configuration path.
*
* @param string $path
* @return string
*/
function config_path($path = '')
{
return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
}
}
if ( ! function_exists('public_path'))
{
/**
* Get the public path.
*
* @param string $path
* @return string
*/
function public_path($path = '')
{
return app()->basePath() . '/public' . ($path ? '/' . $path : $path);
}
}
if (! function_exists('mix')) {
/**
* Get the path to a versioned Mix file.
*
* @param string $path
* @param string $manifestDirectory
* @return \Illuminate\Support\HtmlString|string
*
* @throws \Exception
*/
function mix($path, $manifestDirectory = '')
{
static $manifests = [];
if (! Str::startsWith($path, '/')) {
$path = "/{$path}";
}
if ($manifestDirectory && ! Str::startsWith($manifestDirectory, '/')) {
$manifestDirectory = "/{$manifestDirectory}";
}
if (file_exists(public_path($manifestDirectory.'/hot'))) {
$url = file_get_contents(public_path($manifestDirectory.'/hot'));
if (Str::startsWith($url, ['http://', 'https://'])) {
return new HtmlString(Str::after($url, ':').$path);
}
return new HtmlString("//localhost:8080{$path}");
}
$manifestPath = public_path($manifestDirectory.'/mix-manifest.json');
if (! isset($manifests[$manifestPath])) {
if (! file_exists($manifestPath)) {
throw new Exception('The Mix manifest does not exist.');
}
$manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true);
}
$manifest = $manifests[$manifestPath];
if (! isset($manifest[$path])) {
report(new Exception("Unable to locate Mix file: {$path}."));
if (! app('config')->get('app.debug')) {
return $path;
}
}
return new HtmlString($manifestDirectory.$manifest[$path]);
}
}
<?php
/**
* Horizon Routes
**/
use Illuminate\Support\Facades\Route;
Route::group([
'prefix' => 'api'
], function ($router) {
// Dashboard Routes...
Route::get('/stats', ['as' => 'horizon.stats.index', 'uses' => 'DashboardStatsController@index']);
// Workload Routes...
Route::get('/workload', ['as' => 'horizon.workload.index', 'uses' => 'WorkloadController@index']);
// Master Supervisor Routes...
Route::get('/masters', ['as' => 'horizon.masters.index', 'uses' => 'MasterSupervisorController@index']);
// Monitoring Routes...
Route::get('/monitoring', ['as' => 'horizon.monitoring.index', 'uses' => 'MonitoringController@index']);
Route::post('/monitoring', ['as' => 'horizon.monitoring.store', 'uses' => 'MonitoringController@store']);
Route::get('/monitoring/{tag}', ['as' => 'horizon.monitoring-tag.paginate', 'uses' => 'MonitoringController@paginate']);
Route::delete('/monitoring/{tag}', ['as' => 'horizon.monitoring-tag.destroy', 'uses' => 'MonitoringController@destroy']);
// Job Metric Routes...
Route::get('/metrics/jobs', ['as' => 'horizon.jobs-metrics.index', 'uses' => 'JobMetricsController@index']);
Route::get('/metrics/jobs/{id}', ['as' => 'horizon.jobs-metrics.show', 'uses' => 'JobMetricsController@show']);
// Queue Metric Routes...
Route::get('/metrics/queues', ['as' => 'horizon.queues-metrics.index', 'uses' => 'QueueMetricsController@index']);;
Route::get('/metrics/queues/{id}', ['as' => 'horizon.queues-metrics.show', 'uses' => 'QueueMetricsController@show']);
// Job Routes...
Route::get('/jobs/recent', ['as' => 'horizon.recent-jobs.index', 'uses' => 'RecentJobsController@index']);
Route::get('/jobs/failed', ['as' => 'horizon.failed-jobs.index', 'uses' => 'FailedJobsController@index']);
Route::get('/jobs/failed/{id}', ['as' => 'horizon.failed-jobs.show', 'uses' => 'FailedJobsController@show']);
Route::post('/jobs/retry/{id}', ['as' => 'horizon.retry-jobs.show', 'uses' => 'RetryController@store']);
});
// Catch-all Route...
Route::get('/', ['as' => 'horizon.index', 'uses' => 'HomeController@index']);
Route::get('/{view:.+}', ['as' => 'horizon.index', 'uses' => 'HomeController@index']);
<?php
/**
* Horizon Provider
**/
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;
use Illuminate\Contracts\Redis\Factory as RedisFactory;
use Laravel\Horizon\HorizonServiceProvider as HorizonOriginalServiceProvider;
use Laravel\Horizon\Horizon;
class LumenHorizonServiceProvider extends HorizonOriginalServiceProvider
{
public function register() {
parent::register();
$this->app->singleton(RedisFactory::class, function($app) {
return $app->make('redis');
});
}
public function boot() {
parent::boot();
Horizon::auth(function ($request) {
$user = auth()->user();
return !!$user;
});
}
/**
* Register the Horizon routes.
*
* @return void
*/
protected function registerRoutes()
{
/**
* Overwrite register for LUMEN Horizon routes.
*
* @return void
*/
$this->app->router->group([
'prefix' => config('horizon.uri', 'horizon'),
'namespace' => 'Laravel\Horizon\Http\Controllers',
// 'middleware' => config('horizon.middleware', 'web'),
], function () {
require __DIR__ . '/../../routes/horizon.php';
});
}
/**
* Register the Horizon Artisan commands.
*
* @return void
*/
protected function registerCommands()
{
if ($this->app->runningInConsole()) {
$this->commands([
\Laravel\Horizon\Console\AssetsCommand::class,
\Laravel\Horizon\Console\HorizonCommand::class,
\Laravel\Horizon\Console\ListCommand::class,
\Laravel\Horizon\Console\PurgeCommand::class,
\Laravel\Horizon\Console\PauseCommand::class,
\Laravel\Horizon\Console\ContinueCommand::class,
\Laravel\Horizon\Console\SupervisorCommand::class,
\Laravel\Horizon\Console\SupervisorsCommand::class,
\Laravel\Horizon\Console\TerminateCommand::class,
\Laravel\Horizon\Console\TimeoutCommand::class,
// \Laravel\Horizon\Console\WorkCommand::class,
]);
}
$this->commands([\Laravel\Horizon\Console\SnapshotCommand::class]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment