WillRy /
rabbitrun
| 1 | <?php |
||
| 2 | |||
| 3 | class PdoDriver |
||
| 4 | { |
||
| 5 | protected \PDO $db; |
||
| 6 | |||
| 7 | protected string $entity = "jobs"; |
||
| 8 | |||
| 9 | protected string $consumerName; |
||
| 10 | |||
| 11 | protected string $groupName; |
||
| 12 | |||
| 13 | |||
| 14 | public function __construct( |
||
| 15 | $table = "jobs", |
||
| 16 | $consumerName = null, |
||
| 17 | $groupName = null |
||
| 18 | ) |
||
| 19 | { |
||
| 20 | $this->entity = $table; |
||
| 21 | $this->consumerName = !empty($consumerName) ? $consumerName : rand(0, 999); |
||
| 22 | $this->groupName = !empty($groupName) ? $groupName : rand(0, 999); |
||
| 23 | |||
| 24 | \WillRy\RabbitRun\Connections\ConnectPDO::config( |
||
| 25 | "mysql", "db", "env_db", "root", "root", 3306 |
||
| 26 | ); |
||
| 27 | |||
| 28 | $this->db = \WillRy\RabbitRun\Connections\ConnectPDO::getInstance(true); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function getTask(int $id) |
||
| 32 | { |
||
| 33 | $stmt = $this->db->prepare("SELECT * FROM " . $this->entity . " WHERE id = ? AND status <> 'canceled' limit 1"); |
||
| 34 | $stmt->bindValue(1, $id, \PDO::PARAM_INT); |
||
| 35 | $stmt->execute(); |
||
| 36 | return $stmt->fetch(\PDO::FETCH_ASSOC); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function hasRetry(int $id) |
||
| 40 | { |
||
| 41 | $stmt = $this->db->prepare("SELECT * FROM " . $this->entity . " WHERE id = ? limit 1"); |
||
| 42 | $stmt->bindValue(1, $id, \PDO::PARAM_INT); |
||
| 43 | $stmt->execute(); |
||
| 44 | $task = $stmt->fetch(\PDO::FETCH_ASSOC); |
||
| 45 | |||
| 46 | if(empty($task)) { |
||
| 47 | return false; |
||
| 48 | } |
||
| 49 | |||
| 50 | $retries = $task["retries"]; |
||
| 51 | $maxRetries = $task['max_retries']; |
||
| 52 | |||
| 53 | return $retries < $maxRetries; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function sumRetry(int $id) |
||
| 57 | { |
||
| 58 | $stmt = $this->db->prepare("update " . $this->entity . " set retries = retries + 1 where id = ?"); |
||
| 59 | $stmt->bindValue(1, $id, \PDO::PARAM_INT); |
||
| 60 | return $stmt->execute(); |
||
| 61 | } |
||
| 62 | |||
| 63 | |||
| 64 | public function setError(int $id, string $error) |
||
| 65 | { |
||
| 66 | $stmt = $this->db->prepare("update " . $this->entity . " set last_error=CONCAT_WS('|', last_error, ?) where id = ?"); |
||
| 67 | $stmt->bindValue(1, $error, \PDO::PARAM_INT); |
||
| 68 | $stmt->bindValue(2, $id, \PDO::PARAM_INT); |
||
| 69 | return $stmt->execute(); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function delete(int $id) |
||
| 73 | { |
||
| 74 | $stmt = $this->db->prepare("DELETE FROM " . $this->entity . " where id = ?"); |
||
| 75 | $stmt->bindValue(1, $id, \PDO::PARAM_INT); |
||
| 76 | return $stmt->execute(); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function checkDelete(int $id) |
||
| 80 | { |
||
| 81 | $stmt = $this->db->prepare("SELECT * FROM " . $this->entity . " WHERE id = ? limit 1"); |
||
| 82 | $stmt->bindValue(1, $id, \PDO::PARAM_INT); |
||
| 83 | $stmt->execute(); |
||
| 84 | |||
| 85 | $task = $stmt->fetch(\PDO::FETCH_ASSOC); |
||
| 86 | |||
| 87 | if (!empty($task['auto_delete_end'])) { |
||
| 88 | $stmt = $this->db->prepare("DELETE FROM " . $this->entity . " where id = ?"); |
||
| 89 | $stmt->bindValue(1, $id, \PDO::PARAM_INT); |
||
| 90 | return $stmt->execute(); |
||
| 91 | } |
||
| 92 | |||
| 93 | return false; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Insere um item no banco |
||
| 98 | * @param array $payload |
||
| 99 | * @param bool $requeue_on_error |
||
| 100 | * @param int $max_retries |
||
| 101 | * @param bool $auto_delete_end |
||
| 102 | * @param int|null $id_owner |
||
| 103 | * @param int|null $id_object |
||
| 104 | * @return int |
||
| 105 | */ |
||
| 106 | public function insert( |
||
| 107 | string $queue_name, |
||
| 108 | array $payload, |
||
| 109 | bool $requeue_on_error = true, |
||
| 110 | int $max_retries = 10, |
||
| 111 | bool $auto_delete_end = false, |
||
| 112 | int $id_owner = null, |
||
| 113 | int $id_object = null |
||
| 114 | ): int |
||
| 115 | { |
||
| 116 | $stmt = $this->db->prepare("INSERT INTO " . $this->entity . "(queue, payload, requeue_error, max_retries, auto_delete_end, id_owner, id_object) VALUES(?,?,?,?,?,?,?)"); |
||
| 117 | $stmt->bindValue(1, $queue_name); |
||
| 118 | $stmt->bindValue(2, json_encode($payload)); |
||
| 119 | $stmt->bindValue(3, $requeue_on_error); |
||
| 120 | $stmt->bindValue(4, $max_retries); |
||
| 121 | $stmt->bindValue(5, $auto_delete_end, \PDO::PARAM_BOOL); |
||
| 122 | $stmt->bindValue(6, $id_owner, \PDO::PARAM_INT); |
||
| 123 | $stmt->bindValue(7, $id_object, \PDO::PARAM_INT); |
||
| 124 | $stmt->execute(); |
||
| 125 | |||
| 126 | return $this->db->lastInsertId(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 127 | } |
||
| 128 | |||
| 129 | |||
| 130 | } |
||
| 131 |