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 DateTimeInterface;
6
use Illuminate\Database\ConnectionResolverInterface;
7
use Illuminate\Support\Facades\Date;
8
 
9
class DatabaseUuidFailedJobProvider implements FailedJobProviderInterface, PrunableFailedJobProvider
10
{
11
    /**
12
     * The connection resolver implementation.
13
     *
14
     * @var \Illuminate\Database\ConnectionResolverInterface
15
     */
16
    protected $resolver;
17
 
18
    /**
19
     * The database connection name.
20
     *
21
     * @var string
22
     */
23
    protected $database;
24
 
25
    /**
26
     * The database table.
27
     *
28
     * @var string
29
     */
30
    protected $table;
31
 
32
    /**
33
     * Create a new database failed job provider.
34
     *
35
     * @param  \Illuminate\Database\ConnectionResolverInterface  $resolver
36
     * @param  string  $database
37
     * @param  string  $table
38
     * @return void
39
     */
40
    public function __construct(ConnectionResolverInterface $resolver, $database, $table)
41
    {
42
        $this->table = $table;
43
        $this->resolver = $resolver;
44
        $this->database = $database;
45
    }
46
 
47
    /**
48
     * Log a failed job into storage.
49
     *
50
     * @param  string  $connection
51
     * @param  string  $queue
52
     * @param  string  $payload
53
     * @param  \Throwable  $exception
54
     * @return string|null
55
     */
56
    public function log($connection, $queue, $payload, $exception)
57
    {
58
        $this->getTable()->insert([
59
            'uuid' => $uuid = json_decode($payload, true)['uuid'],
60
            'connection' => $connection,
61
            'queue' => $queue,
62
            'payload' => $payload,
63
            'exception' => (string) mb_convert_encoding($exception, 'UTF-8'),
64
            'failed_at' => Date::now(),
65
        ]);
66
 
67
        return $uuid;
68
    }
69
 
70
    /**
71
     * Get a list of all of the failed jobs.
72
     *
73
     * @return array
74
     */
75
    public function all()
76
    {
77
        return $this->getTable()->orderBy('id', 'desc')->get()->map(function ($record) {
78
            $record->id = $record->uuid;
79
            unset($record->uuid);
80
 
81
            return $record;
82
        })->all();
83
    }
84
 
85
    /**
86
     * Get a single failed job.
87
     *
88
     * @param  mixed  $id
89
     * @return object|null
90
     */
91
    public function find($id)
92
    {
93
        if ($record = $this->getTable()->where('uuid', $id)->first()) {
94
            $record->id = $record->uuid;
95
            unset($record->uuid);
96
        }
97
 
98
        return $record;
99
    }
100
 
101
    /**
102
     * Delete a single failed job from storage.
103
     *
104
     * @param  mixed  $id
105
     * @return bool
106
     */
107
    public function forget($id)
108
    {
109
        return $this->getTable()->where('uuid', $id)->delete() > 0;
110
    }
111
 
112
    /**
113
     * Flush all of the failed jobs from storage.
114
     *
115
     * @param  int|null  $hours
116
     * @return void
117
     */
118
    public function flush($hours = null)
119
    {
120
        $this->getTable()->when($hours, function ($query, $hours) {
121
            $query->where('failed_at', '<=', Date::now()->subHours($hours));
122
        })->delete();
123
    }
124
 
125
    /**
126
     * Prune all of the entries older than the given date.
127
     *
128
     * @param  \DateTimeInterface  $before
129
     * @return int
130
     */
131
    public function prune(DateTimeInterface $before)
132
    {
133
        $query = $this->getTable()->where('failed_at', '<', $before);
134
 
135
        $totalDeleted = 0;
136
 
137
        do {
138
            $deleted = $query->take(1000)->delete();
139
 
140
            $totalDeleted += $deleted;
141
        } while ($deleted !== 0);
142
 
143
        return $totalDeleted;
144
    }
145
 
146
    /**
147
     * Get a new query builder instance for the table.
148
     *
149
     * @return \Illuminate\Database\Query\Builder
150
     */
151
    protected function getTable()
152
    {
153
        return $this->resolver->connection($this->database)->table($this->table);
154
    }
155
}