Reputation: 5862
I'm simply trying to get a function to run at a specific time, but I'm not having much luck. Let me just run through the basics of what I've done
#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) =======================
So I basically have a button on my view that calls post_now
, which then calls a function within a custom model that I have. Here's an example of what I have:
#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
and then here's the SchedulePost class, which is simply writing a text file (for now)
#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
So what I'm expecting to happen is that when post_now is called, it's supposed to run the schedule_post function 1 minute from now, which is simply supposed to write a text file. If I remove the .delay(run_at: 1.minute.from_now)
block and just leave schedule_post
there, then it works fine. So I'm definitely doing something wrong here with the delay function.
Upvotes: 1
Views: 456
Reputation: 6121
You need to start the delayed_job worker to perform delayed tasks...in your terminal from app root directory do
rake jobs:work
Upvotes: 2