Skip to content

Commit

Permalink
Merge branch '8.x' into throttles_exceptions_with_redis
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 9, 2021
2 parents 35071ab + 5da5b44 commit 0bc4977
Show file tree
Hide file tree
Showing 22 changed files with 1,496 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Collections/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ function last($array)
* @param mixed $value
* @return mixed
*/
function value($value)
function value($value, ...$args)
{
return $value instanceof Closure ? $value() : $value;
return $value instanceof Closure ? $value(...$args) : $value;
}
}
15 changes: 14 additions & 1 deletion src/Illuminate/Database/Console/Seeds/SeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Database\ConnectionResolverInterface as Resolver;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class SeedCommand extends Command
Expand Down Expand Up @@ -81,7 +82,7 @@ public function handle()
*/
protected function getSeeder()
{
$class = $this->input->getOption('class');
$class = $this->input->getArgument('class') ?? $this->input->getOption('class');

if (strpos($class, '\\') === false) {
$class = 'Database\\Seeders\\'.$class;
Expand Down Expand Up @@ -109,6 +110,18 @@ protected function getDatabase()
return $database ?: $this->laravel['config']['database.default'];
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
['class', InputArgument::OPTIONAL, 'The class name of the root seeder', null],
];
}

/**
* Get the console command options.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function discoverEvents()
->reduce(function ($discovered, $directory) {
return array_merge_recursive(
$discovered,
DiscoverEvents::within($directory, base_path())
DiscoverEvents::within($directory, $this->eventDiscoveryBasePath())
);
}, []);
}
Expand All @@ -135,4 +135,14 @@ protected function discoverEventsWithin()
$this->app->path('Listeners'),
];
}

/**
* Get the base path to be used during event discovery.
*
* @return string
*/
protected function eventDiscoveryBasePath()
{
return base_path();
}
}
4 changes: 3 additions & 1 deletion src/Illuminate/Queue/Console/WorkCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class WorkCommand extends Command
{--force : Force the worker to run even in maintenance mode}
{--memory=128 : The memory limit in megabytes}
{--sleep=3 : Number of seconds to sleep when no job is available}
{--rest=0 : Number of seconds to rest between jobs}
{--timeout=60 : The number of seconds a child process can run}
{--tries=1 : Number of times to attempt a job before logging it failed}';

Expand Down Expand Up @@ -134,7 +135,8 @@ protected function gatherWorkerOptions()
$this->option('force'),
$this->option('stop-when-empty'),
$this->option('max-jobs'),
$this->option('max-time')
$this->option('max-time'),
$this->option('rest')
);
}

Expand Down
52 changes: 38 additions & 14 deletions src/Illuminate/Queue/Middleware/ThrottlesExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

class ThrottlesExceptions
{
/**
* The developer specified key that the rate limiter should use.
*
* @var string
*/
protected $key;

/**
* The maximum number of attempts allowed before rate limiting applies.
*
Expand All @@ -27,14 +34,7 @@ class ThrottlesExceptions
*
* @var int
*/
protected $retryAfterMinutes;

/**
* The rate limiter key.
*
* @var string
*/
protected $key;
protected $retryAfterMinutes = 0;

/**
* The callback that determines if rate limiting should apply.
Expand All @@ -48,7 +48,7 @@ class ThrottlesExceptions
*
* @var string
*/
protected $prefix = 'circuit_breaker:';
protected $prefix = 'laravel_throttles_exceptions:';

/**
* The rate limiter instance.
Expand All @@ -62,15 +62,13 @@ class ThrottlesExceptions
*
* @param int $maxAttempts
* @param int $decayMinutes
* @param int $retryAfterMinutes
* @param string $key
* @return void
*/
public function __construct($maxAttempts = 10, $decayMinutes = 10, $retryAfterMinutes = 0, string $key = '')
public function __construct($maxAttempts = 10, $decayMinutes = 10)
{
$this->maxAttempts = $maxAttempts;
$this->decayMinutes = $decayMinutes;
$this->retryAfterMinutes = $retryAfterMinutes;
$this->key = $key;
}

/**
Expand Down Expand Up @@ -129,6 +127,19 @@ public function withPrefix(string $prefix)
return $this;
}

/**
* Specify the number of seconds a job should be delayed when it is released (before it has reached its max exceptions).
*
* @param int $backoff
* @return $this
*/
public function backoff($backoff)
{
$this->retryAfterMinutes = $backoff;

return $this;
}

/**
* Get the cache key associated for the rate limiter.
*
Expand All @@ -137,7 +148,20 @@ public function withPrefix(string $prefix)
*/
protected function getKey($job)
{
return $this->prefix.md5(empty($this->key) ? get_class($job) : $this->key);
return $this->key ? $this->prefix.$this->key : $this->prefix.$job->job->uuid();
}

/**
* Set the value that the rate limiter should be keyed by.
*
* @param string $key
* @return $this
*/
public function by($key)
{
$this->key = $key;

return $this;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public function daemon($connectionName, $queue, WorkerOptions $options)
$jobsProcessed++;

$this->runJob($job, $connectionName, $options);

if ($options->rest > 0) {
$this->sleep($options->rest);
}
} else {
$this->sleep($options->sleep);
}
Expand Down
11 changes: 10 additions & 1 deletion src/Illuminate/Queue/WorkerOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class WorkerOptions
*/
public $sleep;

/**
* The number of seconds to rest between jobs.
*
* @var int
*/
public $rest;

/**
* The maximum amount of times a job may be attempted.
*
Expand Down Expand Up @@ -87,14 +94,16 @@ class WorkerOptions
* @param bool $stopWhenEmpty
* @param int $maxJobs
* @param int $maxTime
* @param int $rest
* @return void
*/
public function __construct($name = 'default', $backoff = 0, $memory = 128, $timeout = 60, $sleep = 3, $maxTries = 1,
$force = false, $stopWhenEmpty = false, $maxJobs = 0, $maxTime = 0)
$force = false, $stopWhenEmpty = false, $maxJobs = 0, $maxTime = 0, $rest = 0)
{
$this->name = $name;
$this->backoff = $backoff;
$this->sleep = $sleep;
$this->rest = $rest;
$this->force = $force;
$this->memory = $memory;
$this->timeout = $timeout;
Expand Down
127 changes: 127 additions & 0 deletions src/Illuminate/Testing/Fluent/AssertableJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace Illuminate\Testing\Fluent;

use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\Traits\Tappable;
use Illuminate\Testing\AssertableJsonString;
use PHPUnit\Framework\Assert as PHPUnit;

class AssertableJson implements Arrayable
{
use Concerns\Has,
Concerns\Matching,
Concerns\Debugging,
Concerns\Interaction,
Macroable,
Tappable;

/**
* The properties in the current scope.
*
* @var array
*/
private $props;

/**
* The "dot" path to the current scope.
*
* @var string|null
*/
private $path;

/**
* Create a new fluent, assertable JSON data instance.
*
* @param array $props
* @param string|null $path
* @return void
*/
protected function __construct(array $props, string $path = null)
{
$this->path = $path;
$this->props = $props;
}

/**
* Compose the absolute "dot" path to the given key.
*
* @param string $key
* @return string
*/
protected function dotPath(string $key): string
{
if (is_null($this->path)) {
return $key;
}

return implode('.', [$this->path, $key]);
}

/**
* Retrieve a prop within the current scope using "dot" notation.
*
* @param string|null $key
* @return mixed
*/
protected function prop(string $key = null)
{
return Arr::get($this->props, $key);
}

/**
* Instantiate a new "scope" at the path of the given key.
*
* @param string $key
* @param \Closure $callback
* @return $this
*/
protected function scope(string $key, Closure $callback): self
{
$props = $this->prop($key);
$path = $this->dotPath($key);

PHPUnit::assertIsArray($props, sprintf('Property [%s] is not scopeable.', $path));

$scope = new self($props, $path);
$callback($scope);
$scope->interacted();

return $this;
}

/**
* Create a new instance from an array.
*
* @param array $data
* @return static
*/
public static function fromArray(array $data): self
{
return new self($data);
}

/**
* Create a new instance from a AssertableJsonString.
*
* @param \Illuminate\Testing\AssertableJsonString $json
* @return static
*/
public static function fromAssertableJsonString(AssertableJsonString $json): self
{
return self::fromArray($json->json());
}

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return $this->props;
}
}
38 changes: 38 additions & 0 deletions src/Illuminate/Testing/Fluent/Concerns/Debugging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Illuminate\Testing\Fluent\Concerns;

trait Debugging
{
/**
* Dumps the given props.
*
* @param string|null $prop
* @return $this
*/
public function dump(string $prop = null): self
{
dump($this->prop($prop));

return $this;
}

/**
* Dumps the given props and exits.
*
* @param string|null $prop
* @return void
*/
public function dd(string $prop = null): void
{
dd($this->prop($prop));
}

/**
* Retrieve a prop within the current scope using "dot" notation.
*
* @param string|null $key
* @return mixed
*/
abstract protected function prop(string $key = null);
}

0 comments on commit 0bc4977

Please sign in to comment.