Skip to content
Advertisement

Rails task not running unless rails runner has been executed in the command prompt since the linux server machine’s bootup

I’m trying to execute this task to update a large amount of data from a controller so that when an administrator user accesses this method it will run the said task. I’ve confirmed that there aren’t any issues with the task itself, but I’m not so sure about the way it’s being called.

The problem I’m running into is that when I’ve freshly restarted my virtual machine running the server (using vagrant) the program won’t execute. But after some testing, I’ve found that after I run rails runner (the command executed doesn’t matter) it will start working.

class Admin::AccessLogManageController < Admin::AdminController
  def update_project_ids
    command = "rbenv exec bundle exec rails runner --environment=#{Rails.env} Tasks::UpdateAccessLogProjectIds.execute"
    pid =spawn(command, :chdir=>".")
    Process.detach(pid)
  end
end

Advertisement

Answer

I found the problem. It has to do with spring, essentially the environment is set up to try to run with spring, but it doesn’t automatically start the processes. Adding DISABLE_SPRING=1 seems to fix the issue.

class Admin::AccessLogManageController < Admin::AdminController
  def update_project_ids
    command = "DISABLE_SPRING=1 rbenv exec bundle exec rails runner --environment=#{Rails.env} Tasks::UpdateAccessLogProjectIds.execute"
    pid =spawn(command, :chdir=>".")
    Process.detach(pid)
  end
end

This had the information I needed to solve the problem. Rails runner without spring

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement