gpt4 book ai didi

ruby-on-rails - 在 Windows 中执行 cron 时运行

转载 作者:太空宇宙 更新时间:2023-11-03 17:59:44 24 4
gpt4 key购买 nike

在 cronjobs 和类似的东西上完全是新手,以前从未做过,所以现在我在 RailsCasts 上看到它后,尝试将 whenever 作为插件使用。所以我正在尝试为我的 Ruby 应用程序运行一个 cron 作业,但它似乎无法正常工作,可能是因为代码原因,也可能是因为我使用的是 Windows 7?

这是我的服务器模型的代码

def self.ping
Server.all.each do |t|
if t.name.serverUp?
@response = 'Up'
else
@response = 'Down'
end
self.update_attribute(:serverStatus, @response.to_s)
end
end


def serverUp?
if system 'ping '+name.to_s+' -n 1 > nul'
@response = 'Up'
else
@response = 'Down'
end
self.update_attribute(:serverStatus, @response.to_s)
@response
end

这是我的 schedule.rb 文件中的代码,其中包含 cron 作业

#every 2.minutes do
# runner "Server.last.name = 'Works'"
# runner "for x in Server.all.each {x.serverUp?}", environment =>"development"
#end

every 2.minutes do
runner "Server.ping", environment =>"development"
end

我尝试了这两种方法,(我最近使用的是后者,我在服务器模型中为其创建了 self.ping 方法。

因此,当我在我的 rails 目录中运行 whenever 时,这是我得到的输出:

PS C:\SIS> whenever
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /bin/bash -l -c 'cd C:/SIS
&& script/rails runner -e production '\''Server.ping'\'''

## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.

1st) 有趣的是,即使我在代码中声明了,环境似乎并没有改变为开发。2nd) crontab 文件没有更新(我什至不知道是否存在一个开始?)

所以我尝试通过运行 whenever -w ping 创建一个 crontab whenever file,但结果是

PS C:\SIS> whenever -w ping
[fail] Couldn't write crontab; try running `whenever' with no options to ensure your schedule file is valid.

所以这是我的问题,我们将不胜感激。

编辑: 好的,我决定使用 rufus-scheduler 来经常更新我的服务器列表。我测试了每隔几分钟在表中创建一个新服务器是否可行,这似乎可行。但是,因为我无法阻止它创建服务器,所以我将它从 bundle 中删除,删除了“task_scheduler”文件,然后重新安装它并像以前一样完成所有操作。但是,当我现在尝试运行服务器时,出现以下错误:

C:/SIS/config/initializers/task_scheduler.rb:1:in '<top (required)>': undefined method 'start_new' for Rufus::Scheduler:
Module (NoMethodError)
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in 'load'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in 'block in load'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'block in load_dependency'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in 'new_constants_in'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'load_dependency'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:235:in 'load'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/engine.rb:201:in 'block (2 levels) in <class:Engine>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/engine.rb:200:in 'each'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/engine.rb:200:in 'block in <class:Engine>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:25:in 'instance_exec'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:25:in 'run'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:50:in 'block in run_initializers'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:49:in 'each'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/initializable.rb:49:in 'run_initializers'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:134:in 'initialize!'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:77:in 'method_missing'
from C:/SIS/config/environment.rb:5:in '<top (required)>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in 'require'

from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in 'block in require'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'block in load_dependency'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in 'new_constants_in'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in 'load_dependency'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in 'require'

from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/application.rb:103:in 'require_environment!'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.9/lib/rails/commands.rb:22:in '<top (required)>'
from script/rails:6:in 'require'
from script/rails:6:in '<main>'

task_scheduler中的代码如下:

scheduler = Rufus::Scheduler.start_new

scheduler.every '1m' do
# Server.all.each do
# |server| server.name.serverUp?
# end

Server.ping

end

不确定使用这两种方法中的哪一种,因此我将第一种方法注释掉了。帮助将不胜感激。提前致谢

编辑好吧,终于解决了,不得不把 require 'rubygems'
在我的代码顶部需要“rufus/scheduler”
。现在它工作正常:-)

最佳答案

Cron 是一个 Unix 程序。它在 Windows 上不存在。

关于ruby-on-rails - 在 Windows 中执行 cron 时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7063975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com