There are two questions - one, is this possible, two, is this a good
idea (and if not, then three : how is it a good idea to implement this).
I have a ruby method in a file under lib/ . It is included in
environment.rb. It is called, and runs, every hour, using
rufus-scheduler. I would like to add to one of my views a button or a
link which would allow one to run this method manually. I haven’t found
a way to do this (link_to, link_to_remote, link_to_function, etc. etc.)
Can this be done?
You could add a controller with the appropriate route (in
config/routes.rb) containing an action method that in turn calls your
method on your library class.
It would even be better, depending on how your background processor
works, to use the controller action to schedule the task for execution
rather than calling the method directly.
The delayed_job gem has a convenient way to do this with with #send_later(method, params).
Alright - so basically, stick to the basic Rails idea… Convention over
Configuration, right?
Thanks for the delayed_job idea - but as I said, it runs every hour
already. I don’t see much of a point in adding that as a delayed job (I
expect it takes a whopping 10 seconds to run, maybe 15 if the wind is
contrary).
There are two questions - one, is this possible, two, is this a good
idea (and if not, then three : how is it a good idea to implement this).
I have a ruby method in a file under lib/ . It is included in
environment.rb. It is called, and runs, every hour, using
rufus-scheduler. I would like to add to one of my views a button or a
link which would allow one to run this method manually. I haven’t found
a way to do this (link_to, link_to_remote, link_to_function, etc. etc.)
Can this be done?
You could add a controller with the appropriate route (in
config/routes.rb) containing an action method that in turn calls your
method on your library class.
Is this even a good idea / implementation ?
It’s not a terrible idea as long as the method does not block other
requests while executing for too long.
What is a good way to implement this?
It would even be better, depending on how your background processor
works, to use the controller action to schedule the task for execution
rather than calling the method directly.
The delayed_job gem has a convenient way to do this with with #send_later(method, params).
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.