DatabaseFailedJobProvider.php 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Illuminate\Queue\Failed;
  3. use Illuminate\Support\Carbon;
  4. use Illuminate\Database\ConnectionResolverInterface;
  5. class DatabaseFailedJobProvider implements FailedJobProviderInterface
  6. {
  7. /**
  8. * The connection resolver implementation.
  9. *
  10. * @var \Illuminate\Database\ConnectionResolverInterface
  11. */
  12. protected $resolver;
  13. /**
  14. * The database connection name.
  15. *
  16. * @var string
  17. */
  18. protected $database;
  19. /**
  20. * The database table.
  21. *
  22. * @var string
  23. */
  24. protected $table;
  25. /**
  26. * Create a new database failed job provider.
  27. *
  28. * @param \Illuminate\Database\ConnectionResolverInterface $resolver
  29. * @param string $database
  30. * @param string $table
  31. * @return void
  32. */
  33. public function __construct(ConnectionResolverInterface $resolver, $database, $table)
  34. {
  35. $this->table = $table;
  36. $this->resolver = $resolver;
  37. $this->database = $database;
  38. }
  39. /**
  40. * Log a failed job into storage.
  41. *
  42. * @param string $connection
  43. * @param string $queue
  44. * @param string $payload
  45. * @param \Exception $exception
  46. * @return int|null
  47. */
  48. public function log($connection, $queue, $payload, $exception)
  49. {
  50. $failed_at = Carbon::now();
  51. $exception = (string) $exception;
  52. return $this->getTable()->insertGetId(compact(
  53. 'connection', 'queue', 'payload', 'exception', 'failed_at'
  54. ));
  55. }
  56. /**
  57. * Get a list of all of the failed jobs.
  58. *
  59. * @return array
  60. */
  61. public function all()
  62. {
  63. return $this->getTable()->orderBy('id', 'desc')->get()->all();
  64. }
  65. /**
  66. * Get a single failed job.
  67. *
  68. * @param mixed $id
  69. * @return object|null
  70. */
  71. public function find($id)
  72. {
  73. return $this->getTable()->find($id);
  74. }
  75. /**
  76. * Delete a single failed job from storage.
  77. *
  78. * @param mixed $id
  79. * @return bool
  80. */
  81. public function forget($id)
  82. {
  83. return $this->getTable()->where('id', $id)->delete() > 0;
  84. }
  85. /**
  86. * Flush all of the failed jobs from storage.
  87. *
  88. * @return void
  89. */
  90. public function flush()
  91. {
  92. $this->getTable()->delete();
  93. }
  94. /**
  95. * Get a new query builder instance for the table.
  96. *
  97. * @return \Illuminate\Database\Query\Builder
  98. */
  99. protected function getTable()
  100. {
  101. return $this->resolver->connection($this->database)->table($this->table);
  102. }
  103. }