Button / link to run a method

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).

  1. 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?

  2. Is this even a good idea / implementation ?

  3. What is a good way to implement this?

Robert W. wrote:

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).

GitHub - collectiveidea/delayed_job: Database based asynchronous priority queue system -- Extracted from Shopify

Alright - so basically, stick to the basic Rails idea… Convention over
Configuration, right? :slight_smile:
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).

Aldric G. wrote:

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).

  1. 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.

  1. 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.

  1. 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).