|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scheduler\ActionInspector; |
|
4
|
|
|
|
|
5
|
|
|
use Scheduler\Action\ActionInterface; |
|
6
|
|
|
use Scheduler\Exception\SchedulerException; |
|
7
|
|
|
use Doctrine\DBAL\DBALException; |
|
8
|
|
|
use Doctrine\DBAL\Types\Type; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class RdsActionInspector |
|
12
|
|
|
* @package Scheduler\ActionInspector |
|
13
|
|
|
* @author Aleh Hutnikau, <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class RdsActionInspector extends AbstractActionInspector |
|
16
|
|
|
{ |
|
17
|
|
|
const TABLE_NAME = 'scheduler_action_inspector'; |
|
18
|
|
|
const COLUMN_ID = 'id'; |
|
19
|
|
|
const COLUMN_REPORT = 'report'; |
|
20
|
|
|
const COLUMN_STATE = 'state'; |
|
21
|
|
|
const COLUMN_CREATED_AT = 'created_at'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var \Doctrine\DBAL\Connection */ |
|
24
|
|
|
private $connection; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* RdsActionInspector constructor. |
|
28
|
|
|
* @param \Doctrine\DBAL\Connection $connection |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function __construct(\Doctrine\DBAL\Connection $connection) |
|
31
|
|
|
{ |
|
32
|
1 |
|
$this->connection = $connection; |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param ActionInterface $action |
|
37
|
|
|
* @return boolean returns `false` if action is already exists in this state in the log |
|
38
|
|
|
* @throws SchedulerException |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function update(ActionInterface $action) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$actionId = $action->getId(); |
|
43
|
1 |
|
$actionState = $action->getState(); |
|
44
|
1 |
|
$result = false; |
|
45
|
1 |
|
$previousState = null; |
|
46
|
|
|
try { |
|
47
|
1 |
|
$selectQb = $this->getSelectQuery($actionId); |
|
48
|
|
|
|
|
49
|
1 |
|
if ($dbResult = $selectQb->execute()->fetch(\PDO::FETCH_ASSOC)) { |
|
50
|
1 |
|
$previousState = $dbResult[self::COLUMN_STATE]; |
|
51
|
1 |
|
$qb = $this->getUpdateQuery($actionState, $actionId); |
|
52
|
1 |
|
} else { |
|
53
|
1 |
|
$qb = $this->getInsertQuery($actionState, $actionId); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
if ($actionState === ActionInterface::STATE_FINISHED) { |
|
57
|
1 |
|
$qb->set(self::COLUMN_REPORT, ':report') |
|
58
|
1 |
|
->setParameter(self::COLUMN_REPORT, $action->getReport()); |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
if ($this->isStateAllowed($action, $previousState)) { |
|
62
|
1 |
|
$result = $qb->execute() === 1; |
|
63
|
1 |
|
} |
|
64
|
1 |
|
} catch (DBALException $e) { |
|
65
|
1 |
|
$result = false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
return $result; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return \Doctrine\DBAL\Query\QueryBuilder |
|
73
|
|
|
*/ |
|
74
|
1 |
|
protected function getQueryBuilder() |
|
75
|
|
|
{ |
|
76
|
1 |
|
return $this->getConnection()->createQueryBuilder(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param $actionId |
|
81
|
|
|
* @return \Doctrine\DBAL\Query\QueryBuilder |
|
82
|
|
|
*/ |
|
83
|
1 |
|
protected function getSelectQuery($actionId) |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->getQueryBuilder() |
|
86
|
1 |
|
->select(implode(',', [self::COLUMN_REPORT, self::COLUMN_STATE, self::COLUMN_ID])) |
|
87
|
1 |
|
->from(self::TABLE_NAME) |
|
88
|
1 |
|
->andWhere(self::COLUMN_ID . ' = ?') |
|
89
|
1 |
|
->setParameter(0, $actionId); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param $actionState |
|
94
|
|
|
* @param $actionId |
|
95
|
|
|
* @return \Doctrine\DBAL\Query\QueryBuilder |
|
96
|
|
|
*/ |
|
97
|
1 |
|
protected function getUpdateQuery($actionState, $actionId) |
|
98
|
|
|
{ |
|
99
|
1 |
|
return $this->getQueryBuilder() |
|
100
|
1 |
|
->update(self::TABLE_NAME) |
|
101
|
1 |
|
->set(self::COLUMN_STATE, ':state') |
|
102
|
1 |
|
->set(self::COLUMN_CREATED_AT, ':created_at') |
|
103
|
1 |
|
->where(self::COLUMN_ID . ' = :id') |
|
104
|
1 |
|
->setParameters([ |
|
105
|
1 |
|
self::COLUMN_STATE => $actionState, |
|
106
|
1 |
|
self::COLUMN_ID => $actionId, |
|
107
|
1 |
|
self::COLUMN_CREATED_AT => (new \DateTime)->format('Y-m-d H:i:s') |
|
108
|
1 |
|
]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param $actionState |
|
113
|
|
|
* @param $actionId |
|
114
|
|
|
* @return \Doctrine\DBAL\Query\QueryBuilder |
|
115
|
|
|
*/ |
|
116
|
1 |
|
protected function getInsertQuery($actionState, $actionId) |
|
117
|
|
|
{ |
|
118
|
1 |
|
return $this->getQueryBuilder() |
|
119
|
1 |
|
->insert(self::TABLE_NAME) |
|
120
|
1 |
|
->values([ |
|
121
|
1 |
|
self::COLUMN_ID => ':id', |
|
122
|
1 |
|
self::COLUMN_STATE => ':state', |
|
123
|
1 |
|
self::COLUMN_CREATED_AT => ':created_at', |
|
124
|
1 |
|
]) |
|
125
|
1 |
|
->setParameters([ |
|
126
|
1 |
|
self::COLUMN_ID => $actionId, |
|
127
|
1 |
|
self::COLUMN_STATE => $actionState, |
|
128
|
1 |
|
self::COLUMN_CREATED_AT => (new \DateTime)->format('Y-m-d H:i:s'), |
|
129
|
1 |
|
]); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return \Doctrine\DBAL\Connection |
|
134
|
|
|
*/ |
|
135
|
1 |
|
private function getConnection() |
|
136
|
|
|
{ |
|
137
|
1 |
|
return $this->connection; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param \Doctrine\DBAL\Connection $connection |
|
142
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
143
|
|
|
*/ |
|
144
|
3 |
|
public static function initDb(\Doctrine\DBAL\Connection $connection) |
|
145
|
|
|
{ |
|
146
|
3 |
|
$schemaManager = $connection->getSchemaManager(); |
|
147
|
3 |
|
$schema = $schemaManager->createSchema(); |
|
148
|
3 |
|
$fromSchema = clone $schema; |
|
149
|
3 |
|
$table = $schema->createTable(self::TABLE_NAME); |
|
150
|
3 |
|
$table->addColumn(static::COLUMN_ID, TYPE::STRING, ['length' => 255, 'notnull' => true]); |
|
151
|
3 |
|
$table->addColumn(static::COLUMN_STATE, TYPE::STRING, ['length' => 255, 'notnull' => true]); |
|
152
|
3 |
|
$table->addColumn(static::COLUMN_REPORT, TYPE::TEXT, ['notnull' => false]); |
|
153
|
3 |
|
$table->addColumn(static::COLUMN_CREATED_AT, TYPE::DATETIME, ['notnull' => true]); |
|
154
|
3 |
|
$table->setPrimaryKey([static::COLUMN_ID]); |
|
155
|
3 |
|
$table->addIndex([static::COLUMN_ID], 'IDX_' . static::TABLE_NAME . '_' . static::COLUMN_ID); |
|
156
|
3 |
|
$queries = $fromSchema->getMigrateToSql($schema, $connection->getDatabasePlatform()); |
|
157
|
3 |
|
foreach ($queries as $query) { |
|
158
|
3 |
|
$connection->exec($query); |
|
159
|
3 |
|
} |
|
160
|
3 |
|
} |
|
161
|
|
|
|
|
162
|
3 |
|
public static function dropDb(\Doctrine\DBAL\Connection $connection) |
|
163
|
|
|
{ |
|
164
|
3 |
|
$schemaManager = $connection->getSchemaManager(); |
|
165
|
3 |
|
$schema = $schemaManager->createSchema(); |
|
166
|
3 |
|
$fromSchema = clone $schema; |
|
167
|
3 |
|
$schema->dropTable(self::TABLE_NAME); |
|
168
|
3 |
|
$queries = $fromSchema->getMigrateToSql($schema, $connection->getDatabasePlatform()); |
|
169
|
3 |
|
foreach ($queries as $query) { |
|
170
|
3 |
|
$connection->exec($query); |
|
171
|
3 |
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |