Delayed_job 在 Rails 应用程序的 Ruby 中不工作
Delayed_job not working in Ruby on Rails app
我只是想在特定时间获取 运行 的函数,但运气不佳。让我 运行 简单介绍一下我所做的事情
#Gemfile
gem 'delayed_job_active_record'
#config/application.rb
module SampleApp
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.active_job.queue_adapter = :delayed_job
end
end
#Ran from command line
[06.07.2017/15:38:02] user@ubuntu $ rails g delayed_job:active_record
Running via Spring preloader in process 72879
identical bin/delayed_job
chmod bin/delayed_job
create db/migrate/20170607203821_create_delayed_jobs.rb
[06.07.2017/15:38:21] user@ubuntu $ rake db:migrate
== 20170607203821 CreateDelayedJobs: migrating ================================
-- create_table(:delayed_jobs, {:force=>true})
-> 0.0088s
-- add_index(:delayed_jobs, [:priority, :run_at], {:name=>"delayed_jobs_priority"})
-> 0.0051s
== 20170607203821 CreateDelayedJobs: migrated (0.0142s) =======================
所以我的视图上基本上有一个调用 post_now
的按钮,然后它会调用我拥有的自定义模型中的一个函数。这是我所拥有的示例:
#posts_controller.rb
def post_now
set_post
submit_post = SchedulePost.delay(run_at: 1.minute.from_now).schedule_post(@post)
redirect_to posts_path, flash: {success: submit_post[1] }
end
然后是 SchedulePost class,它只是在编写一个文本文件(暂时)
#models/schedule_post.rb
class SchedulePost < ActiveRecord::Base
def self.schedule_post(post)
command = "touch #{Rails.root}/public/test.txt"
system(command)
return
end
end
所以我期望发生的是,当 post_now 被调用时,它应该 运行 从现在起 1 分钟后的 schedule_post 函数,它应该简单地写成一个文本文件。如果我删除 .delay(run_at: 1.minute.from_now)
块并只将 schedule_post
留在那里,那么它就可以正常工作。所以我肯定在延迟功能上做错了。
您需要启动 delayed_job worker 来执行延迟任务...在您的终端中从应用程序根目录执行
rake jobs:work
我只是想在特定时间获取 运行 的函数,但运气不佳。让我 运行 简单介绍一下我所做的事情
#Gemfile
gem 'delayed_job_active_record'
#config/application.rb
module SampleApp
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.active_job.queue_adapter = :delayed_job
end
end
#Ran from command line
[06.07.2017/15:38:02] user@ubuntu $ rails g delayed_job:active_record
Running via Spring preloader in process 72879
identical bin/delayed_job
chmod bin/delayed_job
create db/migrate/20170607203821_create_delayed_jobs.rb
[06.07.2017/15:38:21] user@ubuntu $ rake db:migrate
== 20170607203821 CreateDelayedJobs: migrating ================================
-- create_table(:delayed_jobs, {:force=>true})
-> 0.0088s
-- add_index(:delayed_jobs, [:priority, :run_at], {:name=>"delayed_jobs_priority"})
-> 0.0051s
== 20170607203821 CreateDelayedJobs: migrated (0.0142s) =======================
所以我的视图上基本上有一个调用 post_now
的按钮,然后它会调用我拥有的自定义模型中的一个函数。这是我所拥有的示例:
#posts_controller.rb
def post_now
set_post
submit_post = SchedulePost.delay(run_at: 1.minute.from_now).schedule_post(@post)
redirect_to posts_path, flash: {success: submit_post[1] }
end
然后是 SchedulePost class,它只是在编写一个文本文件(暂时)
#models/schedule_post.rb
class SchedulePost < ActiveRecord::Base
def self.schedule_post(post)
command = "touch #{Rails.root}/public/test.txt"
system(command)
return
end
end
所以我期望发生的是,当 post_now 被调用时,它应该 运行 从现在起 1 分钟后的 schedule_post 函数,它应该简单地写成一个文本文件。如果我删除 .delay(run_at: 1.minute.from_now)
块并只将 schedule_post
留在那里,那么它就可以正常工作。所以我肯定在延迟功能上做错了。
您需要启动 delayed_job worker 来执行延迟任务...在您的终端中从应用程序根目录执行
rake jobs:work