Get name of running process (Procfile) inside heroku - heroku

My Rails app on Heroku has a number of processes defined in the Procfile
Is there a way for an app to know what process name started it?
e.g. for Procfile:
web: rails s
job: run jobs
Is there an ENV variable (or similar) available within the running process to know if its web or job?

Apparently Heroku sets up DYNO variable, as per https://devcenter.heroku.com/articles/dynos#local-environment-variables

Related

How can I have a distinct Procfile for local development vs Heroku deployment?

In my local environment, I want to start MongoDB, for instance, but on Heroku, it is not needed.
How can I have a different set of processes on my local environment vs Heroku?
Create two Procfile with distinct extensions.
So you'll have
./Procfile with the processes as you intend them for Heroku
./Procfile.local with the processes as you need them locally
when starting foreman locally you'll need to type:
foreman start -f Procfile.local

How to reach a Heroku deployment within Heroku bash or Heroku Scheduler

I'm trying to invoke a HTTP endpoint in my deployed Heroku app with Heroku Scheduler, which basically runs commands on Heroku bash at a fixed rate.
When I run $ heroku run bash, I can read my port with echo $PORT (let's say 5555). However when I try to access my webpage using curl http://localhost:5555, it does not work.
What is the way to reach to the app within Heroku bash without using the app name?
When you run "heroku run bash" you are actually firing up a one-off dyno which - according to Heroku - is a different process (from the dyno running your "web" process type) running on a different runtime instance. Routing does not exist between processes, so the HTTP requests you're issuing will always fail.
It's seems that there is no pre-configured environment variable.
However you can set it:
heroku config:add HEROKU_URL=http://<your app>.herokuapp.com

Heroku - how to start job worker (delayed job)?

I have some miniapp that use delayed_job. On my localhost everything works fine, but when I deploy my app to Heroku and click on the link that should be executed by delayed_job, so nothing happen, the "task" is just saved into the table delayed_job.
In this article on heroku blog is written, that the task from delayed_job table is executed, when is run this command rake jobs:work.
But how can I run this command? Where should be the command placed? In the code, or from terminal console?
If you are running the Cedar stack, run the following from the terminal console:
heroku run rake jobs:work
If you are running the older stacks (Bamboo, Aspen, etc.):
heroku rake jobs:work
see: https://devcenter.heroku.com/articles/rake
According to the delayed_job documentation, you can also start a worker programmatically:
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/environment'
Delayed::Worker.new.start
You should use a Procfile to scpecify the commands for your dynos.
For example you would have something like this in your Procfile:
appDir/Procfile
web: bundle exec rails server -p $PORT
worker: bundle exec rake jobs:work
To use this on your development machine, you should use Foreman, it's all explained at the docs.
https://devcenter.heroku.com/articles/procfile
In our case we're only running a delayed job once a month, so didn't want to have a worker dyno running constantly.
To solve this we queue up the job (with .delayed) and then use the Heroku platform API to spawn rake jobs:workoff in a one-off worker. The API call returns relatively quickly.
PlatformAPI.connect_oauth(ENV["YOUR_HEROKU_KEY"]).dyno.create(ENV["YOUR_HEROKU_APP_NAME"],{command: 'rake jobs:workoff'})

How to run Play! background jobs on Heroku

I'm hardly using heroku for my Play! applications nowadays and I am new. I can deploy my web application but background jobs don't run. I tried to add worker but I got this error :
$ heroku scale worker=1
Scaling worker processes... ! No such type as worker
I created a Procfile includes just
web: play run --http.port=$PORT $PLAY_OPTS
It's just for web, I couldn't find expressions to run workers.
How can I run my background jobs on Heroku?
You have to define the worker instead of web:
worker: play run --http.port=$PORT $PLAY_OPTS

Resque Scheduler on Heroku... Do it remotely?

I'm adding Resque-Scheduler in my app on Heroku
So... I need ONE alone and distinct worker acting as the scheduler and
many doing the jobs.
This is how I've done it :
I've a distinct Heroku App which does nothing but has 1 resque-scheduler worker, running 24/7, adding Resque tasks to the Redis DB of the "distant" main App.
(I do that mapping jobs:work task to resque:scheduler or resque:work)
Is this the best way to do it on the Heroku's platform ; or am I doing it totally wrong ?
Thanks !
EDIT:
minimal app for scheduling on Heroku :
http://github.com/clmntlxndr/heroku-scheduler
UPDATE: With the new CEDAR stack on Heroku and Procfiles, it's possible to start a distinct task for each worker.
web: bundle exec rails server -p $PORT
scheduler: bundle exec rake resque:scheduler
worker: bundle exec rake jobs:work
http://devcenter.heroku.com/articles/procfile
I think I misread your question the first time. I missed the part where scheduler requires a separate rake task.
Yes, I think the only way to do it is to have two separate heroku apps, because heroku workers will only run rake jobs:work and presumably you can only map this onto one of the resque rake tasks.
You could try this:
desc "Alias for resque:work (To run workers on Heroku)"
task "jobs:work" => ["resque:work", "resque:scheduler"]
But I have sincere doubts about that actually working properly with how Heroku monitors worker processes and stuff. Also, double check the rake syntax there; it's just from memory. I know it's possible to specify multiple dependencies though.

Resources