Background process in ruby script with heroku - ruby

I want to get info from twitter for my project, I'm using tweetstream gem, and it works in my code, but I need it to be done as a background process in heroku as a script. So I'm using Event Machine gem to do so, and it works in my computer, but I haven't been able to make it run on heroku on it's own.
I've read that I need to use the Procfile and say it is a worker process, but I can't make it work, but on my local it works fine.
I'm new to background process and working with servers.

Heroku dynos do not support background processes in the "free" or "hobbyist" tiers.
Here is the Heroku Documentation on Background Jobs.
It costs about $30 bucks a month to add this on. There is other implementation hurdles to overcome with a local server vs. Heroku with background jobs. The best starting point I can give you is that Heroku does not support background jobs by default. You have to "Scale the worker dynos"

Related

Using bye bug on Heroku

I have a simple Sinatra application. When I launch it (rackup) locally, and I place a byebug breakpoint, then I can see and interact with bye bug when that spot is reached in the code.
When I deploy that same app on heroku, I have problems:
Using heroku logs -t I can see the output of the server as it runs, and when it hits the break point, I can see it but not interact with it.
Using heroku run irb I can run an interactive rib session but it is not of the running server.
I think this must be possible but I cannot find it documented anywhere.
I highly doubt this is possible.
When you run "heroku run irb", you are spinning up and interacting with a Heroku one-off dyno.
The one-off dyno is a completely separate VM, that has no connection (out of the box) with the dyno(s) running your Sinatra app.
You can check on Heroku Elements to see if there might be add-ons to enable you to debug your running Sinatra app, but out of the box, I don't think you can run an interactive irb session against it.

Running ‘heroku run:detached’ programmatically. How exactly?

I've tried googling and stackoverflowing this and all I get is links back to the API reference or examples of scaling dynos, which is not what I want.
heroku run:detached is great as it just spins up a dyno with your app, runs whatever you want, then spins down the dyno.
How do I achieve the exact same things but using the Heroku Platform API?
I've seen people mention you have to use the Dyno endpoint on the API, but how? Can someone offer an exact example of how I would run the following from the API?
$ heroku run:detached --size 2x rake my_task.rb
You can use the platform API to to this, and create a dyno.
See https://devcenter.heroku.com/articles/platform-api-reference#dyno-create
By sending a POST request to /apps/your_app_name/dynos with the following parameters:
command, the command you wich to run.
attach, set it to false.
This will create a one-off dyno and detach it.
This is what the toolbelt does when you run the run:detached command. You can see how it works here: https://github.com/heroku/heroku/blob/01cd753570cb62b917843112fb29d1cdd43ba335/lib/heroku/command/run.rb#L65

how do i debug a node.js app using heroku foreman?

i am new to heroku and node.js. i have a small node.js app which i can start and run successfully on my local machine using FOREMAN from the heroku toolbelt. i do not understand how to best debug the app, though.
i see that there is an eclipse debugger for node.js as well as the node-inspector project. but i cant seem to make these work with FOREMAN.
foreman start
if it is not possible to debug an app started by foreman, what is the purpose of foreman?
Just an addition, your Procfile could look like this:
web: node web.js
webDebug: node --debug-brk=5858 web.js`
So to start debugging you just call foreman start webDebug, you could call the configuration however you want.
The debugger will listen on port 5858 and you'll have to call your app from http://localhost:5100 instead of http://localhost:5000.
The purpose of Foreman is to allow you to run complex applications, which may consist of several processes, easily. Check out the author's blog post on Foreman:
Lately I've noticed my web apps are getting complicated to run. My
apps no longer consist of a single web process; I usually have have
one or more types of background workers to run different types of jobs
and a clock process to handle scheduling. Splitting an app up in this
way is great for performance and scalability. The downside, however,
is that it becomes much more complicated to get the app and all of its
parts running.
Foreman is an attempt to make this easier. Using foreman you can
declare the various processes that are needed to run your application
using a Procfile.
By leveraging Foreman, Heroku has made it so that you can essentially run any kind of process you want to--a Rails app, a Sinatra app, a Node.js app, or anything else--simply by specifying how to start it in your Procfile, which Foreman reads and executes.
Foreman also allows you to take this simple Procfile and export it to production environments using tools like Upstart and Init. It does not provide any debugging functionality (nor is it meant to).

Heroku scheduling for one-off jobs

I want to schedule jobs to happen at a specific time and date but I'm getting confused by the wide range of options for doing so.
My requirements:
These are not recurring jobs, they only need to happen once at a specified date and time
I'm the only user of the app so don't need to deal with heavy traffic
I would like to minimise the cost of running this on Heroku, i.e. not paying idle dynos
Any tips on which combinations of gems etc. to use?
Have you looked into using https://github.com/bvandenbos/resque-scheduler? You'll need the Redis To Go addon on Heroku. This will cost you $36 a month because you'll need a scheduler process running alongside your web process. However, I've done this for free. See the README here: https://github.com/austinthecoder/pinger.
Good luck!
Due to Heroku Scheduler (default Heroku add-on) doesn't allow you to schedule your job as specific time. It is best to rely on a clock process to do the job. Gem such has Clockwork could be set up please see Heroku's Clockwork guide. You need to combine Clock with a background queuer such as resque or sidekiq. I highly recommend you go for sidekiq. Please bear in mind that both resque and sidekiq requires redis which is offered by redistogo add-on and it will cost you money to run it.

How to set up a periodic nodeJS worker on Heroku Cedar

I've got an Express Web app running as my main app on Heroku Cedar. I need to run a worker job periodically. I know I can specify a worker: in my Procfile, but that seems to be for a forever running kind of job. Perhaps there is a way to have the event mechanism of nodeJS caus e the worker to Idle, and use Cron to poke it alive periodically??
to keep your process alive you can try using an external service which will "ping" your application, you can use the newrelice free addon on heroku for that.
I am currently experimenting and it seems that even with this the application is still put in idle mode but it restarts on the next "ping" so it is still up most of the time.
I don't known node.js but I do my worker with Ruby+EventMachine inside a ruby on rails application and it works fine, you just need something to work in the background aside of your web requests.

Resources