PHP 守护进程向 Facebook、Twitter 等提交过时状态更新

发布于 2024-09-16 09:28:48 字数 601 浏览 18 评论 0原文

我正在构建一个应用程序,允许用户将他们的 facebook、twitter、linkedin 帐户链接到我的应用程序,然后通过我的应用程序向所有这些网站发布更新。此外,用户还可以“发布”状态更新。他们可以设置更新的日期和时间,这将导致更新仅在指定日期而不是立即发布到请求的站点。

我已经通过 cron 作业正常工作,该作业提取尚未发布的所有更新(有一个标志指示数据库中是否已发布更新),然后发布它。我使用 cron 每分钟运行这个过程。 现在我的问题是,如果同时设置足够的更新,我将遇到 PHP 最大化执行时间的问题。我立即解决的问题是获取所有具有 post_timestamp post_timestamp 的更新。 current_timestamp 并尝试发布它们;这样,如果在特定的一分钟内,某些更新尚未发送,则下一次 cron 运行将尝试发送它(如果它仍在最大执行时间限制内)。

我知道这是实现我想要的目标的一种非常蹩脚的方式。因此,我计划有某种守护进程,它继续运行并发布所有过时的更新。 实现这一目标的最佳方法是什么?我正在从事的另一个项目也需要这种守护进程方法。在这方面的任何帮助将不胜感激...:P

提前致谢。

I am building an app that lets users link their facebook, twitter, linkedin accounts to my app and than post updates to all these sites through my app. Additionally user can 'post-date' status updates. They can set a date and time to their update, and which will cause the update to be posted to the requested sites only at the specified date and not immediately.

I have got this working fine with a cron job that pulls out all updates that have not been posted yet (there's a flag indicating whether an update has been posted or not in the DB) and then posting it. I run this process every minute using cron.
Now my issue is that, with enough updates set to the same time, I'll have problems with PHP maxing out the execution time. My immediate work around has been to get all updates which have a post_timestamp < current_timestamp and try to post them; so that if for a particular minute, some updates have not been sent, the next cron run will try to send it if it is still inside the max execution time limit.

I know this is a very lame ass way of achieving what I want. Hence I plan to have some kind of a daemon process which keeps on running and posts all post-dated updates.
What's the best way of achieving this? I also need this daemon approach for another project I am working on. any help in this regard would be greatly appreciated... :P

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

是你 2024-09-23 09:28:48

Gearman 可能就是您正在寻找的:http://gearman.org/

Gearman is probably what you are looking for: http://gearman.org/

雨后彩虹 2024-09-23 09:28:48

我编写了许多基于 PHP 的系统,它们执行类似的操作 - 长时间运行的进程无法从频繁的 Cron 作业中运行,因为它不能同时运行。

这是一个三个阶段的过程。

  1. 初始脚本按需运行,或者在服务器重新启动时运行(请参阅 vixie-cron @reboot )。这只是第二部分。
  2. 一个循环脚本,调用 PHP 脚本来发送所有到期但尚未发送的操作,
  3. 以从数据库发送内容。

第二部分非常简单。

#!/bin/sh
# this file is runSocialSend.sh

BASEURL=http://alister.example.com
URL=$BASE/path/actions/send-to-social-sites.php
# don't store the output
WGET="/usr/bin/wget  -O /dev/null -- "
# wait this many seconds between runs
SLEEP=20

# could also just run it from the CLI - php -f ..../send-to-social-sites.php
$WGET $URL
sleep $SLEEP
# and loop round again to restart...
exec $0

正确运行上述内容的第一部分甚至更简单。

#!/bin/sh

killall runSocialSend.sh
nohup ./runSocialSend.sh > /dev/null &

从字面上看,杀死 runSocialSend.sh 脚本的任何其他实例,然后重新启动它,断开与“控制台”的连接,在后台运行它。

I've written a number of PHP-based systems that do similar things - long running processes that can't be run from a frequent Cron-job as it cannot be allowed to run concurrently.

It's a three-stage process.

  1. An initial script is run on demand, or when a server is restarted (see vixie-cron @reboot). This just calls the 2nd part.
  2. A looping script that calls your PHP script to send all actions who are due, but not sent yet
  3. the script to send the content from the database.

The 2nd part of that is quite easy.

#!/bin/sh
# this file is runSocialSend.sh

BASEURL=http://alister.example.com
URL=$BASE/path/actions/send-to-social-sites.php
# don't store the output
WGET="/usr/bin/wget  -O /dev/null -- "
# wait this many seconds between runs
SLEEP=20

# could also just run it from the CLI - php -f ..../send-to-social-sites.php
$WGET $URL
sleep $SLEEP
# and loop round again to restart...
exec $0

The first part to run the above properly is even simpler.

#!/bin/sh

killall runSocialSend.sh
nohup ./runSocialSend.sh > /dev/null &

Literally, kill any other instances of the runSocialSend.sh script, and then restart it, disconnecting from the 'console', running it in the background.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文