在rails上使用ruby运行后台任务

时间:2022-01-29 23:31:30
class WeatherController < ApplicationController
    def data
        @weather = Weather.all   #show data in data.html.erb
        e = Extractor.new
        e.saveData
    end
end

Here is a piece of code in my controller, I use Extractor to get data from webpages and show them on localhost:3000/weather/data. Now I want to run e.saveData every 10 minutes background, but not refresh my page, could you help me please.

这是我的控制器中的一段代码,我使用Extractor从网页获取数据并在localhost:3000 / weather / data上显示它们。现在我想每隔10分钟运行一次e.saveData背景,但不刷新我的页面,请你帮我。

3 个解决方案

#1


0  

You need to introduce background workers

你需要介绍后台工作人员

https://github.com/mperham/sidekiq

https://github.com/ondrejbartas/sidekiq-cron

https://github.com/Moove-it/sidekiq-scheduler

#2


0  

http://edgeguides.rubyonrails.org/active_job_basics.html

The Purpose of Active Job

积极工作的目的

The main point is to ensure that all Rails apps will have a job infrastructure in place, even if it's in the form of an "immediate runner". We can then have framework features and other gems build on top of that, without having to worry about API differences between various job runners such as Delayed Job and Resque. Picking your queuing backend becomes more of an operational concern, then.

重点是确保所有Rails应用程序都有一个就业基础设施,即使它是“直接跑步者”的形式。然后我们可以在此基础上构建框架功能和其他宝石,而不必担心各种作业运行程序(如Delayed Job和Resque)之间的API差异。那么,挑选你的排队后端就变得更加困难了。

#3


0  

I am the one who asked the question.

我是问这个问题的人。

After comparison, I found that 'rufus-scheduler' is most friendly to use.

经过比较,我发现'rufus-scheduler'是最友好的。

See it on https://github.com/jmettraux/rufus-scheduler#readme

请访问https://github.com/jmettraux/rufus-scheduler#readme

Add it in gemfile, run 'bundle install', then create a file named 'scheduler.rb' in /config/initializer, then put code in it(see the examples), done!

将它添加到gemfile中,运行'bundle install',然后在/ config / initializer中创建一个名为'scheduler.rb'的文件,然后将代码放入其中(参见示例),完成!

#1


0  

You need to introduce background workers

你需要介绍后台工作人员

https://github.com/mperham/sidekiq

https://github.com/ondrejbartas/sidekiq-cron

https://github.com/Moove-it/sidekiq-scheduler

#2


0  

http://edgeguides.rubyonrails.org/active_job_basics.html

The Purpose of Active Job

积极工作的目的

The main point is to ensure that all Rails apps will have a job infrastructure in place, even if it's in the form of an "immediate runner". We can then have framework features and other gems build on top of that, without having to worry about API differences between various job runners such as Delayed Job and Resque. Picking your queuing backend becomes more of an operational concern, then.

重点是确保所有Rails应用程序都有一个就业基础设施,即使它是“直接跑步者”的形式。然后我们可以在此基础上构建框架功能和其他宝石,而不必担心各种作业运行程序(如Delayed Job和Resque)之间的API差异。那么,挑选你的排队后端就变得更加困难了。

#3


0  

I am the one who asked the question.

我是问这个问题的人。

After comparison, I found that 'rufus-scheduler' is most friendly to use.

经过比较,我发现'rufus-scheduler'是最友好的。

See it on https://github.com/jmettraux/rufus-scheduler#readme

请访问https://github.com/jmettraux/rufus-scheduler#readme

Add it in gemfile, run 'bundle install', then create a file named 'scheduler.rb' in /config/initializer, then put code in it(see the examples), done!

将它添加到gemfile中,运行'bundle install',然后在/ config / initializer中创建一个名为'scheduler.rb'的文件,然后将代码放入其中(参见示例),完成!