forked from dimitri/libphp-pgq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPGQConsumer.php
912 lines (806 loc) · 24.9 KB
/
PGQConsumer.php
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
<?php
require_once __DIR__ . '/PGQ.php';
require_once __DIR__ . '/SystemDaemon.php';
/**
* PGQConsumer is a SystemDaemon providing the PGQ SQL API for PHP
* applications, and implementing a simple Consumer model atop of it.
*
* Depending on your needs, please check more specialized classes
* PGQRemoteConsumer and PGQEventRemoteConsumer.
*
* Online references about PGQ are to be found here:
* http://skytools.projects.postgresql.org/doc/pgq-admin.html
* http://skytools.projects.postgresql.org/doc/pgq-sql.html
* http://skytools.projects.postgresql.org/doc/pgq-nodupes.html
*
* http://kaiv.wordpress.com/2007/10/19/skytools-database-scripting-framework-pgq/
*
* http://www.pgcon.org/2008/schedule/events/79.en.html
*
* When implementing a PGQConsumer, you should define following methods:
* - php_error_hook()
* - config()
* - process_event(&$event) --- $event is a PGQEvent
*
* See SystemDaemon header documentation for details about first two
* functions.
*
* The consumer model provided by PGQConsumer works by letting the
* inheriting worker define how to process a single event. The
* function process_event() will get called while in a transaction, so
* don't issue COMMIT or ROLLBACK on $this->pg_src_con.
*
* You should return one of those constants from process_event():
* PGQ_EVENT_OK, PGQ_EVENT_FAILED, PGQ_EVENT_RETRY or PGQ_ABORT_BATCH.
*
* The last will force ROLLBACK, use it when you encounter PostgreSQL
* level errors: the transactions are no more valid, you return
* PGQ_ABORT_BATCH and PGQConsumer will handle: batch is not finished,
* the error is considered not fatal.
*
* When you return PGQ_EVENT_RETRY or PGQ_EVENT_FAILED, PGQConsumer
* will call the matching pgq SQL function to tag the Event. In the
* retry case, PGQConsumer will get $event->retry_delay attribute to
* give to the SQL level API, and in the failed case,
* $event->failed_reason.
*
* PGQConsumer supports same commands as SystemDaemon, and some more:
* install, uninstall, create_queue, drop_queue, register, unregister
* failed, delete <event_id|all>, retry <event_id|all>
*
* See SystemDaemon documentation for using this class at PHP and
* command line levels.
*/
abstract class PGQConsumer extends SystemDaemon
{
/**
* @var bool
*/
protected $connected = false;
/**
* @var string
*/
protected $src_constr;
/**
* @var resource
*/
protected $pg_src_con;
/**
* Consumer name (pgq consumer id)
*
* @var string
*/
protected $cname;
/**
* Queue name
*
* @var string
*/
protected $qname;
public function __construct($cname, $qname, $argc, $argv, $src_constr)
{
$this->cname = $cname;
$this->qname = $qname;
$this->commands[] = "install";
$this->commands[] = "uninstall";
$this->commands[] = "check";
$this->commands[] = "create_queue";
$this->commands[] = "drop_queue";
$this->commands[] = "register";
$this->commands[] = "unregister";
$this->commands[] = "failed";
$this->commands[] = "delete";
$this->commands[] = "retry";
$this->src_constr = $src_constr;
parent::__construct($argc, $argv);
}
/**
* Implement those functions when inheriting from this class.
*/
public function config()
{
}
/**
* @param PGQEvent $event
*
* @return int
*/
abstract public function process_event(&$event);
/**
* Optionnal hook
*
* @param PGQEvent $event
*/
public function preprocess_event($event)
{
}
/**
* Optionnal hook
*
* @param PGQEvent $event
*/
public function postprocess_event($event)
{
}
/**
* We overload SystemDaemon::main() in order to support our specific commands too.
*
* @param int $argc
* @param array $argv
*/
public function main($argc, $argv)
{
if ($argc < 2) {
fprintf(STDERR, $this->usage($this->name));
exit(1);
}
switch ($argv[1]) {
case "install":
$pid = $this->getpid();
if ($pid !== false) {
$this->log->fatal("Can not install an already running daemon.");
} else {
$ret = $this->install();
exit($ret ? 0 : 1);
}
break;
case "uninstall":
$pid = $this->getpid();
if ($pid !== false) {
$this->log->fatal("Can not uninstall a running daemon.");
} else {
$ret = $this->uninstall();
exit($ret ? 0 : 1);
}
break;
case "check":
$pid = $this->getpid();
if ($pid !== false) {
$this->log->fatal("Daemon already running.");
$this->status();
} else {
$ret = $this->check();
exit($ret ? 0 : 1);
}
break;
case "create_queue":
$pid = $this->getpid();
if ($pid !== false) {
$this->log->fatal("Can not create queue for an already running daemon.");
} else {
if ($this->connect() === false) {
exit(3);
} else {
$ret = $this->create_queue();
$this->disconnect();
exit($ret ? 0 : 1);
}
}
break;
case "drop_queue":
$pid = $this->getpid();
if ($pid !== false) {
$this->log->fatal("Can not drop queue for a running daemon.");
} else {
if ($this->connect() === false) {
exit(3);
} else {
$ret = $this->drop_queue();
$this->disconnect();
exit($ret ? 0 : 1);
}
}
break;
case "register":
$pid = $this->getpid();
if ($pid !== false) {
$this->log->fatal("Can not register already running daemon.");
} else {
if ($this->connect() === false) {
exit(3);
} else {
$ret = $this->register();
$this->disconnect();
exit($ret ? 0 : 1);
}
}
break;
case "unregister":
$pid = $this->getpid();
if ($pid !== false) {
$this->log->fatal("Can not unregister already running daemon.");
} else {
$force = false;
if ($argc >= 3) {
$force = ($argv[2] == "--force");
if ($force != true) {
$this->log->fatal("unregister [--force]");
exit(1);
}
}
if ($this->connect() === false) {
exit(3);
} else {
if ($force) {
$ret = $this->force_unregister();
} else {
if ($this->check_unregister()) {
$ret = $this->force_unregister();
} else {
$ret = false;
}
}
$this->disconnect();
exit($ret ? 0 : 1);
}
}
break;
case "failed":
if ($this->connect() === false) {
exit(3);
} else {
$events = $this->failed_event_list();
if ($events !== false) {
foreach ($events as $event) {
echo $event . "\n";
}
} else {
$this->log->warning("Failed event list is empty.");
}
$this->disconnect();
exit(0);
}
break;
case "delete":
if ($argc < 3) {
$this->log->fatal("delete <event_id> [<event_id> ...]");
exit(1);
}
if ($this->connect() === false) {
exit(3);
} else {
if ($argv[2] == "all") {
$this->failed_event_delete_all();
} else {
for ($i = 2; $i < $argc; $i++) {
$this->failed_event_delete($argv[$i]);
}
}
$this->disconnect();
exit(0);
}
break;
case "retry":
if ($argc < 3) {
$this->log->fatal("delete <event_id> [<event_id> ...]");
exit(1);
}
if ($this->connect() === false) {
exit(3);
} else {
if ($argv[2] == "all") {
$this->failed_event_retry_all();
} else {
for ($i = 2; $i < $argc; $i++) {
$this->failed_event_retry($argv[$i]);
}
}
$this->disconnect();
exit(0);
}
break;
default:
/**
* Support daemon commands: start, stop, reload, restart, ...
*/
parent::main($argc, $argv);
break;
}
}
/**
* Check installation is ok before to run.
*/
public function run()
{
if ($this->check()) {
parent::run();
} else {
$this->stop();
}
}
/**
* Stop is called either at normal exit or when receiving an error, PHP errors included.
*/
public function stop()
{
if ($this->connected) {
$this->rollback();
$this->disconnect();
}
parent::stop();
}
/**
* Process available batches, sleeping only when next_batch() returns null.
*
* We want to consume empty batches as fast as possible to avoid lagging behind.
*
* Sleep only when next_batch() is null.
* null: no more batches awaiting us to process, we sleep
* True: successfully processed a batch, have it processed
* False: failed to get next_batch, sleep (temp. failure?)
*
* The sleeping is cared about at SystemDaemon level, in run() method, between process() calls.
*/
public function process()
{
$sleep = false;
if ($this->connect() === false) {
// Can't connect to database, transient error: return to sleep
return;
}
while (!$sleep && !$this->killed) {
$batch_id = $this->next_batch();
switch ($batch_id) {
case null:
$this->log->verbose("PGQConsumer.process: next_batch is null, sleep.");
$sleep = true;
break;
case false:
$this->log->verbose("PGQConsumer.process: failed to get batch.");
break;
case true:
$this->log->verbose("PGQConsumer.process: \$this->process_batch(%d).", $batch_id);
$this->process_batch($batch_id);
break;
}
}
$this->disconnect();
}
/**
* We want that all events of a same batch to have been either processed or postponed by the end of this loop,
* so we only COMMIT; when no error arise.
*
* @param int $batch_id
*
* @return bool
*/
public function process_batch($batch_id)
{
$events = $this->preprocess_batch($batch_id);
if ($events === false) {
$this->log->verbose("PGQConsumer.preprocess_batch got not events (False).");
return false;
}
/**
* Event processing loop!
*/
$abort_batch = false;
foreach ($events as $event) {
if ($abort_batch) {
break;
}
if ($this->preprocess_event($event) === PGQ_ABORT_BATCH) {
$abort_batch = true;
} else {
$tag = $event->tag($this->process_event($event));
$this->log->verbose(
"PGQConsumer.process_batch processed event %d of batch %d, tag %d",
$event->id,
$batch_id,
$tag
);
switch ($tag) {
case PGQ_ABORT_BATCH:
$this->log->verbose("PGQConsumer.process_batch got PGQ_ABORT_BATCH");
$abort_batch = true;
break;
case PGQ_EVENT_FAILED:
$this->event_failed($batch_id, $event);
break;
case PGQ_EVENT_RETRY:
$this->event_retry($batch_id, $event);
break;
case PGQ_EVENT_OK:
break;
}
if (!$abort_batch) {
if ($this->postprocess_event($event) === PGQ_ABORT_BATCH) {
$this->log->verbose("PGQConsumer.postprocess_event(%d) abort batches", $event->id);
$abort_batch = true;
}
}
}
}
return $this->postprocess_batch($batch_id, $abort_batch);
}
/**
* Prepare batch processing, returns PGQEvent list or False if empty.
*
* Exists as a separate function for implementers to accomodate easily, see PGQRemoteConsumer.
*
* @param int $batch_id
*
* @return PGQEvent[]
*/
public function preprocess_batch($batch_id)
{
pg_query($this->pg_src_con, "BEGIN;");
$events = $this->get_batch_events($batch_id);
if ($events === false) {
// batch with no event
$this->log->debug("PGQConsumer.preprocess_batch got no events");
$this->finish_batch($batch_id);
pg_query($this->pg_src_con, "COMMIT;");
return false;
}
$this->log->debug("PGQConsumer.preprocess_batch got %d events", count($events));
return $events;
}
/**
* Conclude batch processing, COMMIT or ROLLBACK if $abort_batch, and call finish_batch().
*
* @param int $batch_id
* @param bool $abort_batch
*
* @return bool
*/
public function postprocess_batch($batch_id, $abort_batch = false)
{
if ($abort_batch) {
$this->log->warning("PGQConsumer.postprocess_batch aborts: ROLLBACK");
$this->rollback();
return false;
}
if ($this->finish_batch($batch_id) === false) {
$this->log->warning("Could not mark batch id %d as finished", $batch_id);
}
pg_query($this->pg_src_con, "COMMIT;");
return true;
}
/**
* print out deamon status, and detailed queue informations
*/
public function status()
{
$pid = $this->getpid();
if ($pid === false) {
printf("PGQConsumer %s is not running.\n", $this->name);
} else {
printf("PGQConsumer %s is running with pid %d\n", $this->name, $pid);
if ($this->connect() === false) {
return;
}
$status = $this->get_consumer_info();
foreach ($status as $k => $v) {
printf("%s: %s\n", $k, $v);
}
$this->disconnect();
}
}
/**
* Consumer installation: create the queue and register consumer.
*
* @return bool
*/
protected function install()
{
if ($this->connect() === false) {
return false;
}
pg_query($this->pg_src_con, "BEGIN;");
$ret = $this->create_queue();
if ($ret) {
$ret = $this->register();
}
pg_query($this->pg_src_con, "COMMIT;");
$this->disconnect();
return $ret;
}
/**
* Consumer uninstall: unregister the consumer and drop the queue
*
* @return bool
*/
protected function uninstall()
{
if ($this->connect() === false) {
return false;
}
pg_query($this->pg_src_con, "BEGIN;");
$ret = $this->unregister();
if ($ret) {
$ret = $this->drop_queue();
}
pg_query($this->pg_src_con, "COMMIT;");
$this->disconnect();
return $ret;
}
/**
* Consumer check: check the queue exists and the consumer is registered.
*
* @return bool
*/
protected function check()
{
if ($this->connect() === false) {
return false;
}
$ret = $this->queue_exists();
if ($ret) {
$ret = $this->is_registered();
}
$this->disconnect();
return $ret;
}
/**
* check_unregister returns True when we can unregister without loosing data.
*
* This now means there's some other consumer registered.
* TODO: check for trigger absence when only 1 consumer is registered (us).
*
* @return bool
*/
protected function check_unregister()
{
$consumers = $this->get_consumers();
if ($consumers === false) {
$this->log->error("Couldn't get consumer list for %s", $this->qname);
return false;
}
if (count($consumers) > 1) {
$this->log->notice("Some other consumers are registered, unregistering %s.", $this->cname);
return true;
}
// We are the only one consumer
$this->log->warning("This %s consumer is the only one registered, use --force to unregister it.", $this->cname);
return false;
}
/**
* Unregister without bothering.
*
* @return bool
*/
protected function force_unregister()
{
return $this->unregister();
}
/**
* TODO.
*
* Drop triggers on the provider
*
* @return bool
*/
protected function drop_trigger()
{
return false;
}
/**
* TODO.
*
* Keep data job is to go find non consumed events in the pgq queue
* and insert them again in the main table.
*
* @return bool
*/
protected function keep_data()
{
if ($this->drop_trigger()) {
return false;
}
return false;
}
/**
* Connects to the conw & conp connection strings.
*
* @param bool $force
*
* @return bool
*/
public function connect($force = false)
{
if ($this->connected && !$force) {
$this->log->notice("connect called when connected is True");
return true;
}
if ($this->src_constr != "") {
$this->log->verbose("Opening pg_src connexion '%s'.", $this->src_constr);
$this->pg_src_con = pg_connect($this->src_constr);
if ($this->pg_src_con === false) {
$this->log->error("Could not open pg_src connection '%s'.", $this->src_constr);
// $this->stop();
return false;
}
} else {
$this->log->error("Connexion string is empty, please fix.");
return false;
}
$this->connected = true;
return true;
}
/**
* Disconnect from databases
*/
public function disconnect()
{
if (!$this->connected) {
$this->log->notice("disconnect called when $this->connected is False");
return;
}
if ($this->pg_src_con != null && $this->pg_src_con !== false) {
$this->log->verbose("Closing pg_src connection '%s'.", $this->src_constr);
pg_close($this->pg_src_con);
$this->pg_src_con = null;
}
$this->connected = false;
}
/**
* ROLLBACK ongoing transactions on src & dst connections
*/
protected function rollback()
{
if ($this->pg_src_con != null && $this->pg_src_con !== false) {
$this->log->notice("ROLLBACK pg_src connection '%s'.", $this->src_constr);
pg_query($this->pg_src_con, "ROLLBACK;");
}
}
/**
* This hook is called when a PHP error at level E_USER_ERROR or E_ERROR is raised.
*
* Those errors are not considered fatal: abort current batch processing,
* but don't have the PGQConsumer daemon quit.
*/
protected function php_error_hook()
{
$this->rollback();
}
/**
* @return bool
*/
protected function create_queue()
{
return PGQ::create_queue($this->log, $this->pg_src_con, $this->qname);
}
/**
* @return bool
*/
protected function drop_queue()
{
return PGQ::drop_queue($this->log, $this->pg_src_con, $this->qname);
}
/**
* @return bool
*/
protected function queue_exists()
{
return PGQ::queue_exists($this->log, $this->pg_src_con, $this->qname);
}
/**
* @return bool
*/
protected function register()
{
return PGQ::register($this->log, $this->pg_src_con, $this->qname, $this->cname);
}
/**
* Unregister PGQ Consumer. Called from stop().
*
* @return bool
*/
protected function unregister()
{
return PGQ::unregister($this->log, $this->pg_src_con, $this->qname, $this->cname);
}
/**
* @return bool
*/
protected function is_registered()
{
return PGQ::is_registered($this->log, $this->pg_src_con, $this->qname, $this->cname);
}
/**
* @return array|bool
*/
protected function get_consumer_info()
{
return PGQ::get_consumer_info($this->log, $this->pg_src_con, $this->qname, $this->cname);
}
/**
* @return array|bool
*/
public function get_consumers()
{
return PGQ::get_consumers($this->log, $this->pg_src_con, $this->qname);
}
/**
* @return bool|string
*/
protected function next_batch()
{
return PGQ::next_batch($this->log, $this->pg_src_con, $this->qname, $this->cname);
}
/**
* @param int $batch_id
*
* @return bool
*/
protected function finish_batch($batch_id)
{
return PGQ::finish_batch($this->log, $this->pg_src_con, $batch_id);
}
/**
* @param int $batch_id
*
* @return PGQEvent[]
*/
protected function get_batch_events($batch_id)
{
return PGQ::get_batch_events($this->log, $this->pg_src_con, $batch_id);
}
/**
* @param int $batch_id
* @param PGQEvent $event
*
* @return bool
*/
protected function event_failed($batch_id, $event)
{
return PGQ::event_failed($this->log, $this->pg_src_con, $batch_id, $event);
}
/**
* @param int $batch_id
* @param PGQEvent $event
*
* @return bool
*/
protected function event_retry($batch_id, $event)
{
return PGQ::event_retry($this->log, $this->pg_src_con, $batch_id, $event);
}
/**
* @return bool|string
*/
protected function maint_retry_events()
{
return PGQ::maint_retry_events($this->log, $this->pg_src_con);
}
/**
* @return bool|PGQEvent[]
*/
protected function failed_event_list()
{
return PGQ::failed_event_list($this->log, $this->pg_src_con, $this->qname, $this->cname);
}
/**
* @return bool
*/
protected function failed_event_delete_all()
{
return PGQ::failed_event_delete_all($this->log, $this->pg_src_con, $this->qname, $this->cname);
}
/**
* @param int $event_id
*
* @return bool
*/
protected function failed_event_delete($event_id)
{
return PGQ::failed_event_delete($this->log, $this->pg_src_con, $this->qname, $this->cname, $event_id);
}
/**
* @return bool
*/
protected function failed_event_retry_all()
{
return PGQ::failed_event_retry_all($this->log, $this->pg_src_con, $this->qname, $this->cname);
}
/**
* @param int $event_id
*
* @return bool
*/
protected function failed_event_retry($event_id)
{
return PGQ::failed_event_retry($this->log, $this->pg_src_con, $this->qname, $this->cname, $event_id);
}
}