简体   繁体   English

如何在运行具有不同 Ruby 版本的多个 Rails 应用程序的服务器上的 cron 作业中使用路径

[英]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.

相关问题 为多个Ruby-on-Rails应用程序运行多种版本的Passenger - Running Multiple Versions of Passenger for Multiple Ruby-on-Rails Applications 如何使用自定义ruby和rails用于不同的应用程序? - How to use custom ruby and rails for different applications? 管理许多不同版本的Ruby on Rails应用程序 - Managing many Ruby on Rails applications of different versions 在 Rails 中,如何通过 ruby​​-install 在不同版本的 Ruby 上运行多个版本的 puma-dev? - In Rails, how do you have multiple versions of puma-dev running on different versions of Ruby via ruby-install? 同一服务器上不同Ruby版本上的多个Rails应用 - Multiple Rails apps on different Ruby versions on the same server Ruby on Rails,Capistrano,无论何时:Cron作业未在生产服务器上执行 - Ruby on Rails, Capistrano, Whenever: Cron jobs not getting executed at production server 使用Passenger运行多个版本的Ruby / Rails - Running multiple versions of Ruby/Rails using Passenger 在本地运行Rails应用程序上的ruby-不同版本的ruby - Running a ruby on rails app locally - Different versions of ruby 如何在不同 Ruby 版本的轨道上远程协作? - How to collaborate remotely in rails with different Ruby versions? Ruby on Rails的多个应用程序 - Multiple applications with Ruby on Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM