2016-07-29 93 views
0

我在AWS上使用用Rails编写的Elastic Beanstalk/RDS/EC2/etc上的应用程序。Rails中的AWS/EBS后台作业

该应用程序有一些数据库条目需要在特定时间后删除,并且正在考虑使用gem创建后台任务。

但是,我看到的所有宝石只是以编程方式创建cron任务的方式。使用Elastic Beanstalk时,正在使用的服务器实例将进行缩放/更改,因此cron任务将丢失。有没有另一种方法来在Rails中设置后台作业?

+0

不熟悉Rails,但看看这可能是一些帮助https://github.com/collectiveidea/delayed_job – error2007s

+0

这让我接近,但我需要我的任务设置为在一定的时间间隔运行(每分钟,每天晚上11点等) – jfefes

+0

你没有提到RDS的一个版本,但如果它是MySQL或MariaDB,那么内置的[事件调度器](https://dev.mysql.com/doc/refman /5.6/en/event-scheduler.html)... –

回答

0

我通常通过使用container_commands来解决此问题,您可以在应用程序的.ebextensions文件夹中的.config文件中配置该问题。 The idea is that the code is executed on so-called 'leader' instance in your webservers pool. It is not considered as a bulletproof solution but does the job and mostly works (it will fail when autoscaling comes into play though). 使用分布式互斥锁(redis-mutex或数据库支持)来阻止除锁定之外的所有进程。

#create config file, use numbers/names that suit your needs: 
#.ebextensions/02_container_commands.config 
container_commands: 
    01_cron_tasks: 
    command: "cat .ebextensions/cron_tasks.txt > /etc/cron.d/yourapp && chmod 644 /etc/cron.d/yourapp" 
    leader_only: true 

cron将会挑这个,只是把crontab中定义.ebextensions/cron_tasks.txt文件,这也是一个添加到您的Git回购。

0 5 * * * root bash -l -c "su -m webapp; cd /var/app/current && rake your_rake_task" 

这是可悲的,但它似乎是亚马逊AWS团队仍然没有拿出对Elastic魔豆的环境中使用Rails调度后台作业合适,方便的解决方案。他们的Worker Tier要求您在应用程序中添加一个专用的端点,以监听来自其cron实现的消息。这很糟糕,因为它迫使您将特定于主机的代码添加到核心应用程序逻辑(路由,控制器),并且需要运行更多的服务器。

与Heroku等平台相比,AWS的另一个缺点是缺少一键支持Redis支持的排队系统,如Sidekiq和可以通过类似cron的调度程序产生的按需工作人员。