0

I have a file named Cron.rb located in app/lib.

To execute the file I do something like this.

Cron.run

This executes a method which creates a thread and processes cron jobs that I have programmed into the file.

I want a graceful way of automatically starting this Cron.run function when rails is started in either development or production. I can't use a cron Job to start this file. I want it to start alongside the current instance of ActiveRecord with all the other Models, etc.

4
  • 4
    Why don't you add something to config/initializers ? startup_cron.rb which contains Cron.run ? Commented Jan 30, 2018 at 22:22
  • Such an easy solution! This worked, thanks! @SteveTurczyn Commented Jan 30, 2018 at 22:34
  • @SteveTurczyn - would you mind posting your comment as an answer, so the question would be flagged as "answered" ... and would stop popping up on my screen ;-) ...? Commented Jan 31, 2018 at 2:33
  • @Myst no problem, now done. :) Commented Jan 31, 2018 at 9:54

1 Answer 1

2

If you want to execute code when rails is started, add an initializer.

It just needs the one line you want to execute.

config/initializers/startup_cron.rb

Cron.run
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.