1

cronjobs 的新手和类似的东西,以前从来没有做过,所以现在我试着在 RailsCasts 上看到它之后,把它用作插件来弄脏我的手。因此,我正在尝试为我的 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在服务器模型中创建了该方法。

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

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 文件没有更新(我什至不知道是否存在一个开始?)

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

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 来经常更新我的服务器列表。我测试了每分钟在表中创建一个新服务器是否可行,这似乎可行。但是,因为我无法阻止它创建服务器,所以我将它从捆绑包中删除,删除了“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' require 'rufus/scheduler'我的代码放在上面。现在它工作正常:-)

4

4 回答 4

3

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

于 2011-08-15T11:43:07.633 回答
1

虽然它远不如 cron 灵活,但 windows 有AT可能能够解决您的问题的命令,但是您只能安排它每天最多运行一次。查看如何在命令行上使用ATrun 。AT /?

要对正在运行的任务进行更细粒度的控制,您可以使用基于 GUI 的 Windows 任务调度程序。您可以在控制面板 -> 系统和安全 -> 管理工具 -> 任务计划程序中找到它

于 2011-08-15T12:30:59.853 回答
0

我有同样的问题,我是这样解决的:

我在 AutoIT 中创建了一个小型 EXE,该 exe 将读取文件/url 的名称以从 ini 文件(config.ini)中执行

我命名了 EXE CRON.exe 并让它通过计划任务启动。

config.ini 中的文件/url 可以是其他可执行文件或 url,脚本将计算出如何运行每种类型。

AutoIT 是一种非常简单的脚本语言,你可以在下面看到我的 CRON.exe 代码:

$iniFile='config.ini'

$i=0
While 1 
    $i+=1
    $file=IniRead($iniFile, 'crons', $i, 'NULL')
    If $file='NULL' Then ExitLoop

    If StringInStr($file, 'http://') Then
        InetGet($file, @TempDir, 1)
    Else
        $wd=StringSplit($file, '\', 3)
        $workingDir=''
        For $i=0 To UBound($wd)-2
            $workingDir&=$wd[$i]&'\'
        Next
        RunWait($file, $workingDir)
    EndIf
WEnd

ini 文件示例:

[CRONS]
;paths of the files to execute
1=C:\anotherEXE.exe
2=http://mySite.com/myCron.php

所有文件都按顺序执行,没有重叠,您可以选择更改要使用的 AutoIT 代码,Run而不是RunWait同时执行所有文件。

于 2011-10-17T15:16:16.387 回答
0

是的,我同意 Jorg M,cron 作业是 unix 程序,而且 rufus 调度程序在内部调用 cron unix 进程,所以远离两者,你应该使用延迟作业 ActiveRecord,它可以帮助你做同样的事情。

这是根据我的经验,还有其他方法,但这是我用过的最好的方法。

好运

于 2014-05-23T11:28:59.670 回答