Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for phpredis #1

Merged
merged 3 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 51 additions & 39 deletions src/Repositories/RedisJobRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ public function getJobs(array $ids, $indexFrom = 0)
});

return $this->indexJobs(collect($jobs)->filter(function ($job) {
$job = is_array($job) ? array_values($job) : null;

return is_array($job) && ! is_null($job[0]);
}), $indexFrom);
}
Expand Down Expand Up @@ -201,15 +203,16 @@ public function pushed($connection, $queue, JobPayload $payload)
$this->storeJobReferences($pipe, $payload->id());

$pipe->hmset(
$payload->id(),
'id', $payload->id(),
'connection', $connection,
'queue', $queue,
'name', $payload->decoded['displayName'],
'status', 'pending',
'payload', $payload->value,
'created_at', time(),
'updated_at', time()
$payload->id(), [
'id' => $payload->id(),
'connection' => $connection,
'queue' => $queue,
'name' => $payload->decoded['displayName'],
'status' => 'pending',
'payload' => $payload->value,
'created_at' => time(),
'updated_at' => time()
]
);

$pipe->expireat(
Expand Down Expand Up @@ -241,11 +244,12 @@ protected function storeJobReferences($pipe, $id)
public function reserved($connection, $queue, JobPayload $payload)
{
$this->connection()->hmset(
$payload->id(),
'status', 'reserved',
'payload', $payload->value,
'updated_at', time(),
'reserved_at', time()
$payload->id(), [
'status' => 'reserved',
'payload' => $payload->value,
'updated_at' => time(),
'reserved_at' => time()
]
);
}

Expand All @@ -260,8 +264,11 @@ public function reserved($connection, $queue, JobPayload $payload)
public function released($connection, $queue, JobPayload $payload)
{
$this->connection()->hmset(
$payload->id(), 'status', 'pending',
'payload', $payload->value, 'updated_at', time()
$payload->id(), [
'status' => 'pending',
'payload' => $payload->value,
'updated_at' => time()
]
);
}

Expand All @@ -277,14 +284,15 @@ public function remember($connection, $queue, JobPayload $payload)
{
$this->connection()->pipeline(function ($pipe) use ($connection, $queue, $payload) {
$pipe->hmset(
$payload->id(),
'id', $payload->id(),
'connection', $connection,
'queue', $queue,
'name', $payload->decoded['displayName'],
'status', 'completed',
'payload', $payload->value,
'completed_at', time()
$payload->id(), [
'id' => $payload->id(),
'connection' => $connection,
'queue' => $queue,
'name' => $payload->decoded['displayName'],
'status' => 'completed',
'payload' => $payload->value,
'completed_at' => time()
]
);

$pipe->persist($payload->id());
Expand All @@ -304,8 +312,11 @@ public function migrated($connection, $queue, Collection $payloads)
$this->connection()->pipeline(function ($pipe) use ($payloads) {
foreach ($payloads as $payload) {
$pipe->hmset(
$payload->id(), 'status', 'pending',
'payload', $payload->value, 'updated_at', time()
$payload->id(), [
'status' => 'pending',
'payload' => $payload->value,
'updated_at' => time()
]
);
}
});
Expand Down Expand Up @@ -340,8 +351,8 @@ public function completed(JobPayload $payload, $failed = false)
protected function markJobAsCompleted($pipe, $id, $failed)
{
$failed
? $pipe->hmset($id, 'status', 'failed')
: $pipe->hmset($id, 'status', 'completed', 'completed_at', time());
? $pipe->hmset($id, ['status'=> 'failed'])
: $pipe->hmset($id, ['status' => 'completed', 'completed_at' => time()]);

$pipe->expireat($id, Chronos::now()->addHours(1)->getTimestamp());
}
Expand Down Expand Up @@ -435,7 +446,7 @@ public function trimFailedJobs()
public function findFailed($id)
{
$attributes = $this->connection()->hmget(
$id, ...$this->keys
$id, $this->keys
);

return is_array($attributes) && ! is_null($attributes[0])
Expand All @@ -457,15 +468,16 @@ public function failed($exception, $connection, $queue, JobPayload $payload)
$this->storeFailedJobReferences($pipe, $payload->id());

$pipe->hmset(
$payload->id(),
'id', $payload->id(),
'connection', $connection,
'queue', $queue,
'name', $payload->decoded['displayName'],
'status', 'failed',
'payload', $payload->value,
'exception', (string) $exception,
'failed_at', time()
$payload->id(), [
'id' => $payload->id(),
'connection' => $connection,
'queue' => $queue,
'name' => $payload->decoded['displayName'],
'status' => 'failed',
'payload' => $payload->value,
'exception' => (string) $exception,
'failed_at' => time()
]
);

$pipe->expireat(
Expand Down Expand Up @@ -507,7 +519,7 @@ public function storeRetryReference($id, $retryId)
'retried_at' => Chronos::now()->getTimestamp()
];

$this->connection()->hmset($id, 'retried_by', json_encode($retries));
$this->connection()->hmset($id, ['retried_by' => json_encode($retries)]);
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/Repositories/RedisMasterSupervisorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ public function get(array $names)
{
$records = $this->connection()->pipeline(function ($pipe) use ($names) {
foreach ($names as $name) {
$pipe->hmget('master:'.$name, 'name', 'pid', 'status', 'supervisors');
$pipe->hmget('master:'.$name, ['name', 'pid', 'status', 'supervisors']);
}
});

return collect($records)->map(function ($record) {
return is_null($record[0]) ? null : (object) [
$record = array_values($record);

return ! $record[0] ? null : (object) [
'name' => $record[0],
'pid' => $record[1],
'status' => $record[2],
Expand All @@ -96,11 +98,12 @@ public function update(MasterSupervisor $master)
$supervisors = $master->supervisors->map->name->all();

$this->connection()->hmset(
'master:'.$master->name,
'name', $master->name,
'pid', $master->pid(),
'status', $master->working ? 'running' : 'paused',
'supervisors', json_encode($supervisors)
'master:'.$master->name,[
'name'=> $master->name,
'pid'=> $master->pid(),
'status'=> $master->working ? 'running' : 'paused',
'supervisors'=> json_encode($supervisors)
]
);

$this->connection()->expire(
Expand Down
14 changes: 10 additions & 4 deletions src/Repositories/RedisMetricsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ protected function increment($key, $runtime)
$this->connection()->pipeline(function ($pipe) use ($key, $runtime) {
$pipe->hsetnx($key, 'throughput', 0);

$pipe->eval(LuaScripts::updateJobMetrics(), 1, $key, $runtime);
if (config('database.redis.client') == 'phpredis') {
$pipe->eval(LuaScripts::updateJobMetrics(), [$key, $runtime], 1);
} else {
$pipe->eval(LuaScripts::updateJobMetrics(), 1, $key, $runtime);
}
});
}

Expand Down Expand Up @@ -324,14 +328,16 @@ protected function storeSnapshotForQueue($queue)
protected function baseSnapshotData($key)
{
$responses = $this->connection()->transaction(function ($trans) use ($key) {
$trans->hmget($key, 'throughput', 'runtime');
$trans->hmget($key, ['throughput', 'runtime']);

$trans->del($key);
});

$snapshot = array_values($responses[0]);

return [
'throughput' => $responses[0][0],
'runtime' => $responses[0][1],
'throughput' => $snapshot[0],
'runtime' => $snapshot[1],
];
}

Expand Down
21 changes: 12 additions & 9 deletions src/Repositories/RedisSupervisorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ public function get(array $names)
{
$records = $this->connection()->pipeline(function ($pipe) use ($names) {
foreach ($names as $name) {
$pipe->hmget('supervisor:'.$name, 'name', 'master', 'pid', 'status', 'processes', 'options');
$pipe->hmget('supervisor:'.$name, ['name', 'master', 'pid', 'status', 'processes', 'options']);
}
});

return collect($records)->filter()->map(function ($record) {
return is_null($record[0]) ? null : (object) [
$record = array_values($record);

return ! $record[0] ? null : (object) [
'name' => $record[0],
'master' => $record[1],
'pid' => $record[2],
Expand Down Expand Up @@ -111,13 +113,14 @@ public function update(Supervisor $supervisor)
})->toJson();

$this->connection()->hmset(
'supervisor:'.$supervisor->name,
'name', $supervisor->name,
'master', explode(':', $supervisor->name)[0],
'pid', $supervisor->pid(),
'status', $supervisor->working ? 'running' : 'paused',
'processes', $processes,
'options', $supervisor->options->toJson()
'supervisor:'.$supervisor->name, [
'name' => $supervisor->name,
'master' => explode(':', $supervisor->name)[0],
'pid' => $supervisor->pid(),
'status' => $supervisor->working ? 'running' : 'paused',
'processes' => $processes,
'options' => $supervisor->options->toJson()
]
);

$this->connection()->expire(
Expand Down