Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Queue\Failed;
4
 
5
use Aws\DynamoDb\DynamoDbClient;
6
use DateTimeInterface;
7
use Exception;
8
use Illuminate\Support\Carbon;
9
use Illuminate\Support\Facades\Date;
10
 
11
class DynamoDbFailedJobProvider implements FailedJobProviderInterface
12
{
13
    /**
14
     * The DynamoDB client instance.
15
     *
16
     * @var \Aws\DynamoDb\DynamoDbClient
17
     */
18
    protected $dynamo;
19
 
20
    /**
21
     * The application name.
22
     *
23
     * @var string
24
     */
25
    protected $applicationName;
26
 
27
    /**
28
     * The table name.
29
     *
30
     * @var string
31
     */
32
    protected $table;
33
 
34
    /**
35
     * Create a new DynamoDb failed job provider.
36
     *
37
     * @param  \Aws\DynamoDb\DynamoDbClient  $dynamo
38
     * @param  string  $applicationName
39
     * @param  string  $table
40
     * @return void
41
     */
42
    public function __construct(DynamoDbClient $dynamo, $applicationName, $table)
43
    {
44
        $this->table = $table;
45
        $this->dynamo = $dynamo;
46
        $this->applicationName = $applicationName;
47
    }
48
 
49
    /**
50
     * Log a failed job into storage.
51
     *
52
     * @param  string  $connection
53
     * @param  string  $queue
54
     * @param  string  $payload
55
     * @param  \Throwable  $exception
56
     * @return string|int|null
57
     */
58
    public function log($connection, $queue, $payload, $exception)
59
    {
60
        $id = json_decode($payload, true)['uuid'];
61
 
62
        $failedAt = Date::now();
63
 
64
        $this->dynamo->putItem([
65
            'TableName' => $this->table,
66
            'Item' => [
67
                'application' => ['S' => $this->applicationName],
68
                'uuid' => ['S' => $id],
69
                'connection' => ['S' => $connection],
70
                'queue' => ['S' => $queue],
71
                'payload' => ['S' => $payload],
72
                'exception' => ['S' => (string) $exception],
73
                'failed_at' => ['N' => (string) $failedAt->getTimestamp()],
74
                'expires_at' => ['N' => (string) $failedAt->addDays(3)->getTimestamp()],
75
            ],
76
        ]);
77
 
78
        return $id;
79
    }
80
 
81
    /**
82
     * Get a list of all of the failed jobs.
83
     *
84
     * @return array
85
     */
86
    public function all()
87
    {
88
        $results = $this->dynamo->query([
89
            'TableName' => $this->table,
90
            'Select' => 'ALL_ATTRIBUTES',
91
            'KeyConditionExpression' => 'application = :application',
92
            'ExpressionAttributeValues' => [
93
                ':application' => ['S' => $this->applicationName],
94
            ],
95
            'ScanIndexForward' => false,
96
        ]);
97
 
98
        return collect($results['Items'])->sortByDesc(function ($result) {
99
            return (int) $result['failed_at']['N'];
100
        })->map(function ($result) {
101
            return (object) [
102
                'id' => $result['uuid']['S'],
103
                'connection' => $result['connection']['S'],
104
                'queue' => $result['queue']['S'],
105
                'payload' => $result['payload']['S'],
106
                'exception' => $result['exception']['S'],
107
                'failed_at' => Carbon::createFromTimestamp(
108
                    (int) $result['failed_at']['N']
109
                )->format(DateTimeInterface::ISO8601),
110
            ];
111
        })->all();
112
    }
113
 
114
    /**
115
     * Get a single failed job.
116
     *
117
     * @param  mixed  $id
118
     * @return object|null
119
     */
120
    public function find($id)
121
    {
122
        $result = $this->dynamo->getItem([
123
            'TableName' => $this->table,
124
            'Key' => [
125
                'application' => ['S' => $this->applicationName],
126
                'uuid' => ['S' => $id],
127
            ],
128
        ]);
129
 
130
        if (! isset($result['Item'])) {
131
            return;
132
        }
133
 
134
        return (object) [
135
            'id' => $result['Item']['uuid']['S'],
136
            'connection' => $result['Item']['connection']['S'],
137
            'queue' => $result['Item']['queue']['S'],
138
            'payload' => $result['Item']['payload']['S'],
139
            'exception' => $result['Item']['exception']['S'],
140
            'failed_at' => Carbon::createFromTimestamp(
141
                (int) $result['Item']['failed_at']['N']
142
            )->format(DateTimeInterface::ISO8601),
143
        ];
144
    }
145
 
146
    /**
147
     * Delete a single failed job from storage.
148
     *
149
     * @param  mixed  $id
150
     * @return bool
151
     */
152
    public function forget($id)
153
    {
154
        $this->dynamo->deleteItem([
155
            'TableName' => $this->table,
156
            'Key' => [
157
                'application' => ['S' => $this->applicationName],
158
                'uuid' => ['S' => $id],
159
            ],
160
        ]);
161
 
162
        return true;
163
    }
164
 
165
    /**
166
     * Flush all of the failed jobs from storage.
167
     *
168
     * @param  int|null  $hours
169
     * @return void
170
     *
171
     * @throws \Exception
172
     */
173
    public function flush($hours = null)
174
    {
175
        throw new Exception("DynamoDb failed job storage may not be flushed. Please use DynamoDb's TTL features on your expires_at attribute.");
176
    }
177
}