forked from dimitri/libphp-pgq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPGQCoopConsumer.php
More file actions
167 lines (152 loc) · 4.43 KB
/
PGQCoopConsumer.php
File metadata and controls
167 lines (152 loc) · 4.43 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?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 PGQCoopConsumer extends PGQConsumer
{
/**
* Subconsumer name
*
* @var string
*/
protected $sname;
/**
* @var int
*/
protected $timeout = null;
/**
* @param string $sname Subconsumer name
* @param string $cname
* @param string $qname
* @param int $argc
* @param array $argv
* @param string $src_constr
*/
public function __construct($sname, $cname, $qname, $argc, $argv, $src_constr)
{
$this->sname = $sname;
parent::__construct($cname, $qname, $argc, $argv, $src_constr);
}
/**
* {@inheritdoc}
*/
protected function register()
{
$sql = sprintf(
"SELECT pgq_coop.register_subconsumer('%s', '%s', '%s');",
pg_escape_string($this->qname),
pg_escape_string($this->cname),
pg_escape_string($this->sname)
);
$this->log->verbose("%s", $sql);
$r = pg_query($this->pg_src_con, $sql);
if ($r === false) {
$this->log->warning(
"Could not register subconsumer '%s' of '%s' to queue '%s'",
$this->sname,
$this->cname,
$this->qname
);
return false;
}
$registered = pg_fetch_result($r, 0, 0);
if ($registered == "1") {
return true;
} else {
$this->log->fatal("Register SubConsumer failed (%d).", $registered);
return false;
}
}
/**
* {@inheritdoc}
*/
public function unregister()
{
$sql = sprintf(
"SELECT pgq_coop.unregister_subconsumer('%s', '%s', '%s', 0);",
pg_escape_string($this->qname),
pg_escape_string($this->cname),
pg_escape_string($this->sname)
);
$this->log->verbose("%s", $sql);
$r = pg_query($this->pg_src_con, $sql);
if ($r === false) {
$this->log->fatal(
"Could not unregister subconsumer '%s' of '%s' to queue '%s'",
$this->sname,
$this->cname,
$this->qname
);
return false;
}
$unregistered = pg_fetch_result($r, 0, 0);
if ($unregistered == "1") {
return true;
} else {
$this->log->fatal("Unregister SubConsumer failed (%d).", $unregistered);
return false;
}
}
/**
* {@inheritdoc}
*/
protected function next_batch()
{
if ($this->timeout !== null) {
$sql = sprintf(
"SELECT pgq_coop.next_batch('%s', '%s', '%s', '%s')",
pg_escape_string($this->qname),
pg_escape_string($this->cname),
pg_escape_string($this->sname),
$this->timeout
);
} else {
$sql = sprintf(
"SELECT pgq_coop.next_batch('%s', '%s', '%s')",
pg_escape_string($this->qname),
pg_escape_string($this->cname),
pg_escape_string($this->sname)
);
}
$this->log->verbose("%s", $sql);
if (($r = pg_query($this->pg_src_con, $sql)) === false) {
$this->log->error("Could not get next batch");
return false;
}
$batch_id = pg_fetch_result($r, 0, 0);
$this->log->debug(
"Get batch_id %s (isnull=%s)",
$batch_id,
($batch_id === null ? "True" : "False")
);
return $batch_id;
}
/**
* {@inheritdoc}
*/
protected function finish_batch($batch_id)
{
$sql = sprintf("SELECT pgq_coop.finish_batch(%d);", (int)$batch_id);
$this->log->verbose("%s", $sql);
if (pg_query($this->pg_src_con, $sql) === false) {
$this->log->error("Could not finish batch %d", (int)$batch_id);
return false;
}
return true;
}
/**
* {@inheritdoc}
*/
protected function get_consumer_info()
{
return PGQ::get_consumer_info(
$this->log,
$this->pg_src_con,
$this->qname,
$this->cname . "." . $this->sname
);
}
}