Skip to content

Commit

Permalink
Merge pull request #11 from snower/1.0
Browse files Browse the repository at this point in the history
add support 5.5 5.6 5.7 5.8
  • Loading branch information
snower authored May 24, 2019
2 parents 8519f7f + c7a76dd commit c654eb9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 42 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"leric/php-thrift": "0.9.*"
},
"require-dev": {
"laravel/framework": "5.4.*"
"laravel/framework": ">=5.4"
},
"autoload": {
"psr-4": {
Expand Down
46 changes: 20 additions & 26 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Console\Application;
use Illuminate\Container\Container;
use Symfony\Component\Process\ProcessUtils;
Expand Down Expand Up @@ -131,12 +132,8 @@ public function command($command, array $parameters = [])
*/
public function job($job)
{
$job = is_string($job) ? resolve($job) : clone $job;

$this->initAction();
$this->createPayload(JobDispatchHandler::class, 'handle', [
$job
]);
$this->createPayload(new JobDispatchHandler($job));
return $this->schedule();
}

Expand All @@ -149,22 +146,18 @@ public function job($job)
*/
public function exec($command, array $parameters = [])
{
$this->initAction();
if (count($parameters)) {
$command .= ' '.$this->compileParameters($parameters);
}

$this->initAction();
$this->createPayload(CommandRunHandler::class, 'handle', [
$command
]);
$this->createPayload(new CommandRunHandler($command));
return $this->schedule();
}

public function fire($event, $payload = [], $halt = false){
$this->initAction();
$this->createPayload(EventFireHandler::class, 'handle', [
$event, $payload, $halt
]);
$this->createPayload(new EventFireHandler($event, $payload, $halt));
return $this->schedule();
}

Expand All @@ -190,12 +183,6 @@ public function http($url, $method = "GET", $body = '', $headers = [], $options
return $this->schedule();
}

/**
* Compile parameters for a command.
*
* @param array $parameters
* @return string
*/
protected function compileParameters(array $parameters)
{
return collect($parameters)->map(function ($value, $key) {
Expand All @@ -211,16 +198,23 @@ protected function compileParameters(array $parameters)
})->implode(' ');
}

protected function createPayload($class, $method, $data){
$data = json_encode([
"job" => "Illuminate\\Events\\CallQueuedHandler@call",
"attempts" => 0,
protected function createPayload($command){
$data = [
"job" => "Illuminate\\Queue\\CallQueuedHandler@call",
'displayName' => "Illuminate\\Queue\\CallQueuedHandler",
'maxTries' => null,
'timeout' => null,
"data" => [
"class" => $class,
"method" => $method,
"data" => serialize($data),
"commandName" => get_class($command),
"command" => serialize($command)
]
]);
];

if($this->action == 'redis') {
$data['id'] = Str::random(32);
$data['attempts'] = 0;
}
$data = json_encode($data);

if($this->action === 'mysql') {
$now = Carbon::now()->getTimestamp();
Expand Down
29 changes: 18 additions & 11 deletions src/Jobs/CommandRunHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,30 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\Mutex;
use Illuminate\Console\Scheduling\CacheMutex;

class CommandRunHandler implements ShouldQueue
{
protected $mutex;
public $command;

public function __construct()
public function __construct($command)
{
$container = Container::getInstance();

$this->mutex = $container->bound(Mutex::class)
? $container->make(Mutex::class)
: $container->make(CacheMutex::class);
$this->command = $command;
}

public function handle($command){
$event = new Event($this->mutex, $command);
$event->run(Container::getInstance());
public function handle(){
$laravel = Container::getInstance();

if(class_exists('Illuminate\Console\Scheduling\CacheMutex')){
$cachemutex_class = 'Illuminate\Console\Scheduling\CacheMutex';
} else {
$cachemutex_class = 'Illuminate\Console\Scheduling\CacheEventMutex';
}

$mutex = $laravel->bound(Mutex::class)
? $laravel->make(Mutex::class)
: $laravel->make($cachemutex_class);

$event = new Event($mutex, $this->command);
$event->run($laravel);
}
}
15 changes: 13 additions & 2 deletions src/Jobs/EventFireHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@

class EventFireHandler implements ShouldQueue
{
public function handle($event, $payload = [], $halt = false){
Container::getInstance()->make('events')->fire($event, $payload, $halt);
public $event;
public $payload;
public $halt;

public function __construct($event, $payload = [], $halt = false)
{
$this->event = $event;
$this->payload = $payload;
$this->halt = $halt;
}

public function handle(){
Container::getInstance()->make('events')->dispatch($this->event, $this->payload, $this->halt);
}
}
11 changes: 9 additions & 2 deletions src/Jobs/JobDispatchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@

class JobDispatchHandler implements ShouldQueue
{
public function handle($job){
dispatch(is_string($job) ? resolve($job) : $job);
public $job;

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

public function handle(){
dispatch(is_string($this->job) ? resolve($this->job) : $this->job);
}
}

0 comments on commit c654eb9

Please sign in to comment.