1

I'd like to infrequently open a Twitter streaming connection with TweetStream and listen for new statuses for about an hour.

How should I go about opening the connection, keeping it open for an hour, and then closing it gracefully?

Normally for background processes I would use Resque or Sidekiq, but from my understanding those are for completing tasks as quickly as possible, not chilling and keeping a connection open.

I thought about using a global variable like $twitter_client but that wouldn't horizontally scale.

I also thought about building a second application that runs on one box to handle this functionality, but that seems excessive if it can be integrated into the main app somehow.

To clarify, I have no trouble starting a process, capturing tweets, and using them appropriately. I'm just not sure what I should be starting. A new app? A daemon of some sort?

I've never encountered a problem like this, and am completely lost. Any direction would be much appreciated!

4

1 回答 1

0

虽然不是直接修复,但这是我要看的:


时间

你正在处理时间,所以我会看看哪些以时间为中心的过程可以用来诱导一个小时的连接

具体来说,我会考虑在服务器上运行某种作业,您可以在特定时间触发(如果需要,以编程方式)来打开和关闭连接。我只有resque的经验,但正如你所说,它可能不适合这份工作。如果我找到任何更好的解决方案,我一定会更新答案


贮存

连接到 TweetStream 后,您将希望了解如何捕获该时间段的推文。为工作创建数据表似乎是一种浪费,所以我倾向于使用Redis之类的东西来存储你需要的推文

然后可以使用它来输出您需要的推文,允许您模拟存储/捕获它们,然后在小时窗口过去后删除它们


送货

我不知道您在什么上下文中使用此功能,所以我只会给您尽可能通用的流程想法

为了显示推文,我会亲自在数据库中创建某种记录,以显示您当天 ping TweetStream 的时间(如果它发生变化;如果它是常量,只需在初始化程序中设置一个常量),然后只包含尝试从 Redis 获取推文的一些逻辑。如果您能够收集它们,请按照您的意愿展示它们,否则不要打印任何东西

希望能给你更广泛的想法?

于 2013-11-21T10:00:15.160 回答