[英]How to use a path in cron jobs on a server running multiple Rails applications with different Ruby versions
I have a server running two Ruby on Rails applications, and each of these applications runs under different Ruby versions.我有一台运行两个 Ruby on Rails 应用程序的服务器,每个应用程序都在不同的 Ruby 版本下运行。 I am using RVM as my Ruby version manager.我使用 RVM 作为我的 Ruby 版本管理器。
Both of the applications have cron tasks scheduled and use the Whenever gem to generate the cron configuration file, shown below.这两个应用程序都安排了 cron 任务,并使用每当 gem 生成 cron 配置文件,如下所示。
When generating the cron file, each application writes a different path, with its particular path to the Ruby executable, on cron file.生成 cron 文件时,每个应用程序都会在 cron 文件上写入不同的路径,其中包含指向 Ruby 可执行文件的特定路径。
Can this cause a conflict?这会引起冲突吗? If so what is the best approach to correctly run the cron tasks of both projects?如果是这样,正确运行两个项目的 cron 任务的最佳方法是什么?
# Begin Whenever generated tasks for: astego at: 2020-02-20 19:09:55 +0000
PATH=/var/www/astego/shared/bundle/ruby/2.4.0/bin:/home/deploy/.rvm/gems/ruby-2.4.2/bin:/home/deploy/.rvm/gems/ruby-2.4.2@global/bin:/home/deploy/.rvm/rubies/ruby-2.4.2/bin:/home/deploy/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
50 6 * * * /bin/bash -l -c 'cd /var/www/astego/releases/20200220190804 && bundle exec bin/rails runner -e production '\''User.send_birthday_message'\'''
0 4 1 * * /bin/bash -l -c 'cd /var/www/astego/releases/20200220190804 && bundle exec bin/rails runner -e production '\''User.send_last_three_months_unpaid'\'''
# End Whenever generated tasks for: astego at: 2020-02-20 19:09:55 +0000
# Begin Whenever generated tasks for: importador_atual at: 2020-02-19 21:12:31 +0000
PATH=/var/www/importador_atual/shared/bundle/ruby/2.7.0/bin:/home/deploy/.rvm/gems/ruby-2.7.0/bin:/home/deploy/.rvm/gems/ruby-2.7.0@global/bin:/home/deploy/.rvm/rubies/ruby-2.7.0/bin:/home/deploy/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
0 23 * * * /bin/bash -l -c 'cd /var/www/importador_atual/releases/20200219211048 && bundle exec bin/rails runner -e production '\''ImportarService.importar'\'''
# End Whenever generated tasks for: importador_atual at: 2020-02-19 21:12:31 +0000
A job running in a crontab is an entirely separate task, running in its own environment.在 crontab 中运行的作业是一个完全独立的任务,在其自己的环境中运行。 It's possible to wire things up so they talk, or conflict, but general applications aren't aware of each other.有可能将事物连接起来以便它们交谈或发生冲突,但一般应用程序并不知道彼此。
As long as your Rails applications use separate server ports and write to separate files you should be fine.只要您的 Rails 应用程序使用单独的服务器端口并写入单独的文件,您就应该没问题。
The actual tasks you're running aren't what we'd normally include in a cron script;您正在运行的实际任务不是我们通常会包含在 cron 脚本中的; Normally Rails is a long-running task that isn't started on a repeated schedule.通常情况下,Rails 是一个长时间运行的任务,不会按重复的计划启动。 Running a small scheduled application that is part of a Rails site would normally be done in a crontab using rails runner
or as a Rake task.运行作为 Rails 站点一部分的小型预定应用程序通常会在 crontab 中使用rails runner
或作为 Rake 任务来完成。 (I recommend the first but most people seem to not know it exists and use Rake.) I'd recommend researching this and decide if you're going about it the right way. (我推荐第一个,但大多数人似乎不知道它存在并使用 Rake。)我建议研究这个并决定你是否以正确的方式去做。 rails runner
makes it trivial to access the DB and all the Rails models just as in your normal Rails environment, it's just headless, which results in lighter-weight code and faster startup. rails runner
使得访问数据库和所有 Rails 模型变得很简单,就像在正常的 Rails 环境中一样,它只是无头的,这导致代码更轻,启动速度更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.