Postgres触发器使用PHP进行通知 [英] Postgres trigger notify with PHP

查看:68
本文介绍了Postgres触发器使用PHP进行通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Postgre触发器有问题或误解->执行通知->捕获到PHP流中.

I have a problem or misunderstanding with Postgre trigger -> perform notify -> capture into PHP flow.

我的平台是带有Postgres的centos中的PHP(5.6).

My Platform is PHP(5.6) in centos with Postgres.

我必须添加带有通知表的触发器,并且每当向该通知中添加新通知时, SMS就必须向该用户发送.

所以这里添加了这样的触发器

So here added trigger like this

CREATE FUNCTION xxx_sms_trigger() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
DECLARE
BEGIN
  PERFORM pg_notify('sms', NEW.id||'' );
  RETURN new;
END;

和在php中插入新通知的工作正常.

and in php the inserting new notifications work fine.

现在我有一个单独的文件,在其中添加了此通过"pg_get_notify"捕获pg_notify触发,在这里,我无法完全像Postgres如何触发某些未知的php脚本而不以它那样运行的方式来获取此流程服务或如何使它正常工作?

Now I have a separate file where added this capturing pg_notify triggering by "pg_get_notify", here I couldn't get this flow totally like how Postgres can trigger some unknown php script without its being running as service or how I can make it work?

推荐答案

您确实需要一个作为服务运行的php脚本.如果那将是接收您提供的通知的语言.正如@FelipeRosa所说,该脚本将需要连接到数据库,然后发出至少一个命令:

You do need a php script running as a service. If that is going to be the language that receives the notification you provide. As @FelipeRosa says, that script will need to connect to the database, then issue at least one command:

listen sms;

主站点上有一个很好的监听示例( http://www.php.net/manual/zh/function.pg-get-notify.php )

There is a good example of the listen on the main site (http://www.php.net/manual/en/function.pg-get-notify.php)

几年来我没有用php编码.最近,我已经在python中实现了此逻辑,但是应该差不多.我做了一些研究,可以在php中找到select(),但是似乎postgres套接字描述符在php中不可用,因此除非您可以找到postgres套接字描述符,否则您不能在php中使用select()

I haven't coded in php in a few years. Recently I have implemented this logic in python, but it should be about the same. I did a little research, and I can find select() in php, but it seems that the postgres socket descriptor is not available in php, so you can't use the select() in php unless you can find the postgres socket descriptor.

无论如何,该线程在这里(

Anyway, that thread is here (http://postgresql.1045698.n5.nabble.com/Is-there-any-way-to-listen-to-NOTIFY-in-php-without-polling-td5749888.html). There is a polling example in there for your php script side down near the bottom. You can do the listen as previous selected (once), then put your pg_get_notify() in a loop with a sleep in there for the amount of time you are willing to queue notifications.

首先,在python中我不轮询,我选择了select.select(pg_conn,...),当数据到达postgres连接时,我检查它是否有通知,因此没有轮询".如果您能找到一种在php中使用select()而不是循环的方法,那就太好了.

Just fwiw, in python I don't poll, I select.select(pg_conn,...), when data arrives on the postgres connection I check it for notifications, so there is no 'polling'. It would be nice if you could find a way to use select() in php instead of looping.

-g

这篇关于Postgres触发器使用PHP进行通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆