Last active
November 13, 2017 04:22
-
-
Save pascal08/64e1521bfcf257308e2d7f8635c44afb to your computer and use it in GitHub Desktop.
Laravel 5.4 Queue Standalone
This file contains hidden or 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_once 'vendor/autoload.php'; | |
require_once 'container.php'; | |
$app = Container::getInstance(); | |
$app->bind('database', function($app) { | |
$database = new Illuminate\Database\Capsule\Manager($app); | |
$database->addConnection([ | |
'driver' => 'mysql', | |
'host' => 'localhost', | |
'database' => 'homestead', | |
'username' => 'homestead', | |
'password' => 'secret' | |
]); | |
$database->setAsGlobal(); | |
return $database; | |
}); | |
$app->bind('queue', function($app) { | |
$queue = new Illuminate\Queue\Capsule\Manager($app); | |
$connection = $app['database']::schema()->getConnection(); | |
$queue->getContainer()->bind('encrypter', function() { | |
return new Illuminate\Encryption\Encrypter('foobar'); | |
}); | |
$queue->getContainer()->bind('request', function() { | |
return new Illuminate\Http\Request(); | |
}); | |
$queue->addConnection([ | |
'driver' => 'database', | |
'table' => 'jobs', | |
'connection' => 'default', | |
'host' => '127.0.0.1', | |
'queue' => 'default', | |
], 'default'); | |
$manager = $queue->getQueueManager(); | |
$resolver = new \Illuminate\Database\ConnectionResolver(['default' => $connection]); | |
$manager->addConnector('database', function () use ($resolver) { | |
return new Illuminate\Queue\Connectors\DatabaseConnector($resolver); | |
}); | |
$queue->setAsGlobal(); | |
return $queue; | |
}); | |
$app->bind('queue.worker', function($app) { | |
$manager = $app['queue']->getQueueManager(); | |
// var_dump($manager->connection('default')->pop()); | |
$dispatcher = new \Illuminate\Events\Dispatcher(); | |
$handler = new Handler(); | |
$worker = new \Illuminate\Queue\Worker($manager, $dispatcher, $handler); | |
return $worker; | |
}); | |
$app->singleton(\Illuminate\Contracts\Bus\Dispatcher::class, function ($app) { | |
return new \Illuminate\Bus\Dispatcher($app, function ($connection = null) use ($app) { | |
return $app['queue']->connection($connection); | |
}); | |
}); | |
$app->alias(\Illuminate\Contracts\Bus\Dispatcher::class, \Illuminate\Bus\Dispatcher::class); | |
if (! function_exists('dispatch')) { | |
/** | |
* Dispatch a job to its appropriate handler. | |
* | |
* @param mixed $job | |
* @return mixed | |
*/ | |
function dispatch($job) | |
{ | |
return app(\Illuminate\Bus\Dispatcher::class)->dispatch($job); | |
} | |
} | |
if (! function_exists('app')) { | |
/** | |
* Get the available container instance. | |
* | |
* @param string $abstract | |
* @param array $parameters | |
* @return mixed|\Illuminate\Foundation\Application | |
*/ | |
function app($abstract = null, array $parameters = []) | |
{ | |
if (is_null($abstract)) { | |
return Container::getInstance(); | |
} | |
return empty($parameters) | |
? Container::getInstance()->make($abstract) | |
: Container::getInstance()->makeWith($abstract, $parameters); | |
} | |
} |
This file contains hidden or 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
{ | |
"require": { | |
"illuminate/queue": "^5.4", | |
"illuminate/encryption": "^5.4", | |
"illuminate/database": "^5.4", | |
"illuminate/events": "^5.4", | |
"illuminate/http": "^5.4", | |
"illuminate/contracts": "^5.4", | |
"illuminate/bus": "^5.4" | |
} | |
} |
This file contains hidden or 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\Container\Container as IlluminateContainer; | |
class Container extends IlluminateContainer | |
{ | |
/** | |
* Determine if the application is in maintenance mode. | |
* @return bool | |
*/ | |
public function isDownForMaintenance() { | |
return false; | |
} | |
} |
This file contains hidden or 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\Debug\ExceptionHandler; | |
class Handler implements ExceptionHandler | |
{ | |
public function report(Exception $exception) | |
{ | |
echo $exception->getMessage(); | |
} | |
public function render($request, Exception $exception) | |
{ | |
echo $exception->getMessage(); | |
} | |
public function renderForConsole($output, Exception $exception) | |
{ | |
echo $exception->getMessage(); | |
} | |
} |
This file contains hidden or 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_once 'bootstrap.php'; | |
require_once 'sendemailjob.php'; | |
$queue = $app['queue']; | |
dispatch(new SendEmail()); |
This file contains hidden or 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\Queue\ShouldQueue; | |
use Illuminate\Queue\SerializesModels; | |
use Illuminate\Queue\InteractsWithQueue; | |
use Illuminate\Queue\Jobs\DatabaseJob; | |
class SendEmailJob implements ShouldQueue | |
{ | |
use InteractsWithQueue, SerializesModels; | |
public function handle() | |
{ | |
echo "Sending email..."; | |
} | |
} |
This file contains hidden or 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_once 'bootstrap.php'; | |
require_once 'sendemailjob.php'; | |
require_once 'handler.php'; | |
use Illuminate\Queue\WorkerOptions; | |
$worker = $app['queue.worker']; | |
$delay = 0; | |
$memory = 128; | |
$timeout = 60; | |
$sleep = 3; | |
$maxTries = 0; | |
$force = false; | |
$options = new WorkerOptions($delay, $memory, $timeout, $sleep, $maxTries, $force); | |
$worker->daemon('default', 'default', $options); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment