How to schedule a rake tasks in Google Cloud? - ruby-on-rails

I had a Rails application on Heroku, and to schedule some rake tasks I used an add-on called Scheduler. I had to change my application to Google Cloud and I do not know how to schedule the same rakes. Could someone help me?

Reference:
https://cloud.google.com/appengine/docs/flexible/ruby/scheduling-jobs-with-cron-yaml
This will allow you to setup cron scripts that call out to a web endpoint. My suggestion would be to add an API endpoint that can trigger the code you need ran. If security is an issue, you can always add http basic auth behind the endpoint and pass it along in the URL payload from the cron.
If you wanted to get dirty with it, you could trigger the rake code from the controller itself, though I wouldn't recommend this approach as it's bad design, but instead just move all the code your executing in rake to the controller.
If the above approach doesn't fit your needs, then the next best option would be to setup a sidekiq instance and use that to schedule and run code from your codebase.

Related

Running code repeatedly in Ruby on Rails with Heroku for an indefinite period

I am attempting to build a web application with Ruby on Rails that users site up for and get an email alert when a certain event happens.
As such, I need to be able to make an API call and then based on the JSON response, send the alert, but I need a way to have this API call happen repeatedly for an indefinite amount of time automatically. I am also using Heroku at this time if that needs to be taken into account.
Thanks for your help.
This sound like a cron job in plain old linux. Heroku calls this addon Scheduler. You have to define the task withing lib/tasks/scheduler.rake
For further information read the heroku docs for scheduler here

Are there existing gems or services for web hook implementations?

Most major services like github provide Webhooks functionality.
So, with github - you can set hooks to notify you on every commit.
In the same time web hooks are not that easy.
Each web hook has to be ran asynchronously to not block web server at the time of communicating with destination. And it can take a good time (10-15 seconds). There should be implemented repeating functionality (in case if destination is not responding).
So, I think that there for sure should be some service or library which will do this for you.
Do you know any of these?
I need to send data to lots of endpoints and to receive a response from them..
You need a gem providing background job functionality. Sidekiq and Delayed Job are ones of most frequently used.
Idea is that after request (in ruby on rails you can use after_action hook or just do it in controller action) you create a job which will be executed asynchronously. Put logic you need in the job class
Both sidekiq and delayed job have repeating functionality, just pick gem that looks simpler to use
There is a gem called ActiveHook but it does not appear to be maintained anymore.
Benedikt Deicke wrote a good article on sending webhooks with Rails, you should check it.

Rails - Implementing a real time console or logger for background tasks

I have a couple of rake tasks. I would want to be able to trigger these manually from my Rails admin. So far, this is not a problem. But those tasks contain a lot of puts and prints, and it would be cool to be able to see these in the browser as they happen.
(I have no problem rewriting the tasks to be run with delayed_job/sidekiq/redis if necessary)
Any idea how this could be achieved?
Update:
Idea #1: What about doing puts and pushing a message to Faye, and just subscribe to a specific channel in the browser? :) I'm going to use Faye soon anyways. Yes, or no? :)
In this case, it may be the best to publish your messages to Faye in a protected channel, and subscribe to that in Faye after starting the job. You may need to start the job using Delayed job or resque to launch the job asynchronously.

Suggestions for how to write a service in Rails 3

I am building an application which will send status requests to users (via email & sms) on a regular basis. I want to execute the service each hour which will:
Query the database for all requests that need to be sent (based on some logic)
Send the requests through Amazon's Simple Email Service (this is already working)
Write a record of the status request notification back to the data store
I am considering wrapping up this series of operations into a single controller with an end point that can be called remotely to kick off the process within the rails app.
Longer term, I will break this process out into an app that can be run independently of my rails app, but for now I'm just trying to keep it simple.
My first inclination is to build the following:
Controller with the following elements:
A method which will orchestrate the steps outlined above (and can be called externally)
A call to the status_request model which will bring back a collection of request needing to be sent
A loop to iterate through the pending requests, which will:
Make a call to my AWS Simple Email Service module to actually send the email, and
Make a call to the status_request model to log the request back to the database
Model:
A method on my status_request model which will bring back a collection of requests that need to be sent
A method in my status_request model which will log that a notification was sent
Since this will behave as a service that gets called periodically from an outside scheduler I don't think I'll need a view for this operation. (Will, of course, need views to show users and admins what requests have been sent, but that's later...).
As someone new to Rails, I'm asking for review of this approach and any suggestions you may have.
Thanks!
Instead of a controller which Jeff pointed out exposes a security risk, you may just want to expose a rake task and use cron to invoke it on an hourly basis.
If you are still interested in building a controller, look at devise gem and its single access token, token_authenticatable, for securing the methods you are exposing.
You may also want to look at delayed_job or resque to offload the call to status_request and the loop to AWS simple service to a background worker process.
You may want a seperate controller and view for the log file so you can review progress on demand.
And if you want to get real fancy use Amazon SNS to send you alerts when the service reaches some unacceptable level of failures, backlog, etc.
Since you are trying to invoke this from an outside process, your approach should work. You could also have a worker process that processes task when they are there.
You will need routes to expose your service, and you may want to also make security decisions. How will the service that invokes your application authenticate so all others can't hit it at will?
Another consideration should be how many emails are you sending. If there are enough, we may want to look into the fact that writing this sort of loop is going to be extremely top heavy; and may affect users on the current system if it's a web application.
In the end, there are many ways to do this. I would focus on the performance/usage you expect as well as security. There's never one perfect way to solve a problem like this, and your way should just be aware of the variables it will need to be operating within.
Resque and Redis might be helpful to you in scheduling and performing operatio n .They are simple and superfast, [here](http://railscasts.com/episodes/271-resque] is a simple tut on same.

Processing incoming emails on Heroku

For my side project kwiqi, I use ActionMailer's 'receive' method to process incoming email messages for tracking my expenses. Heroku doesn't have a local mail server running that same code will not work. One solution I've thought of is to periodically hit a controller action that will pull messages from Gmail. Are there other solutions that are reasonable? Is anyone processing incoming emails in Heroku?
You can use sendgrid addon, and their parse api (http://wiki.sendgrid.com/doku.php?id=parse_api). I've written a short tutorial on how to do so here: http://nanceskitchen.com/2010/02/21/accept-incoming-emails-into-a-heroku-app-using-sendgrid/
I know that this is a little late but for anyone else that might find this useful in future we created the http:///CloudMailin.com addon for Heroku that should help you to receive email on Heroku Rails apps really easily.
Heroku support running workers using DelayedJob. Workers are resourced just like Dynos (you pay by the hour) and for this you get a dedicated resource to process your emails.
In the past I have used Cron calling a controller in my app. It's pretty effective.
If the hourly limitation is an issue, you can call your app from another location ... I have a cheap Dreamhost account for some of my non-priority sites that I have used as Cron systems.
There are also a number of ping and uptime services that you can use for this purpose as well ... simply pass these services your email controller.
A real limitation of Heroku currently is that the most rapid frequency they support for cron jobs is hourly.
I'd recommend using Gmail and using delayed job as an alternative to cron to set a more reasonable frequency. There is a good tutorial on setting this up at WiseJive

Resources