forked from dimitri/libphp-pgq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPGQEventRemoteConsumer.php
More file actions
82 lines (72 loc) · 2.52 KB
/
PGQEventRemoteConsumer.php
File metadata and controls
82 lines (72 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
require_once __DIR__ . '/PGQRemoteConsumer.php';
/**
* PGQEventRemoteConsumer is a PGQRemoteConsumer which handles nested
* transactions for event management, allowing the remote processing
* to be commited or rollbacked at event level.
*/
abstract class PGQEventRemoteConsumer extends PGQRemoteConsumer
{
/**
* @param PGQEvent $event
*
* @return string
*/
public function get_savepoint($event)
{
return sprintf("pgq_event_%d", $event->id);
}
/**
* {@inheritdoc}
*/
public function preprocess_event($event)
{
$sql_savepoint = sprintf("SAVEPOINT %s", $this->get_savepoint($event));
$this->log->debug($sql_savepoint);
if (pg_query($this->pg_dst_con, $sql_savepoint) === false) {
$this->log->warning(
"PGQEventRemoteConsumer.preprocess_event could not place SAVEPOINT for event %d",
$event->id
);
return PGQ_ABORT_BATCH;
}
return true;
}
/**
* {@inheritdoc}
*/
public function postprocess_event($event)
{
$savepoint = $this->get_savepoint($event);
switch ($event->tag) {
case PGQ_EVENT_OK:
$sql_release = sprintf("RELEASE SAVEPOINT %s", $savepoint);
$this->log->debug($sql_release);
$result = pg_query($this->pg_dst_con, $sql_release);
if ($result === false) {
$this->log->notice("Could not release savepoint %s", $savepoint);
return PGQ_ABORT_BATCH;
}
break;
case PGQ_EVENT_FAILED:
$sql_rollback = sprintf("ROLLBACK TO SAVEPOINT %s", $savepoint);
$this->log->debug($sql_rollback);
$result = pg_query($this->pg_dst_con, $sql_rollback);
if ($result === false) {
$this->log->notice("Could not rollback to savepoint %s", $savepoint);
return PGQ_ABORT_BATCH;
}
break;
case PGQ_EVENT_RETRY:
$sql_rollback = sprintf("ROLLBACK TO SAVEPOINT %s", $savepoint);
$this->log->debug($sql_rollback);
$result = pg_query($this->pg_dst_con, $sql_rollback);
if ($result === false) {
$this->log->notice("Could not tollback to savepoint %s", $savepoint);
return PGQ_ABORT_BATCH;
}
break;
}
return true;
}
}