I created a module in my_rails_project/lib/ssc.rb. I can execute the
method in rails console (i.e. SSC::Monitor.method1(a,b)). How can I
execute the method regularly? I am using Linux, can I do it using
`crontab`? Thank you.
module SSC
class Monitor
def self.method1(a,b)
bla bla bla
some code will access the model class
end
I tried script/runner for this, but every execution need to initialize
the rails environment. Any other good idea for it? What is your
good/best practice?
There are a number of different ways to have a task persist, and run on
a regular schedule. I've also previously used background-db and cron
seems to be a popular option. I tend to go with whatever has the easiest
tutorials that i can find!
Anyone else feel free to weigh in if you have any more suggestions.
That’s a great idea, until someone in the outside finds out about that path/resource. You’re opening up too much and scriptkiddies can get happy attempting a DoS. Wondering if you handle that somehow (IP address check or something)?
That's a great idea, until someone in the outside finds out about that
path/resource. You're opening up too much and scriptkiddies can get
happy attempting a DoS. Wondering if you handle that somehow (IP address
check or something)?
How about using HTTP basic authentication so only authorized clients can
access the controller action? Which would be only your rake task.
I haven't thought this through, It's just the first thing that came to
mind.
I'm not sure, but an alternative is to put the wget command in a small
shell script (Linux talk, iiuc) or a .bat file (MS talk, iiuc). Then
invoke that shell script or .bat file from cron.
What looks attractive to me about this approach (of using wget via cron instead of a rake task) is that you are not bringing up an entire rails environment to handle a task. There’s less overhead. Just be sure to lock it down with a combination of things that have been suggested here: IP Address check, local_request?, http basic authentication. There may be more you can do…
You will need to pass the para1, and para2 to the someaction method so
at a minimum you have to change the signature of the someaction method
as below:
def someaction para1, para2
...
end
now within this method you can do anything you want only thing is that
this method needs to be public in scope and you need to make sure that
it requires no authentication otherwise your wget request will fail.
I usually use script/runner in Rails for such needs. wget may or may
not work depending on how restful your routes are and controller
action is.