Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you programmatically access current Heroku dyno id/name?

Tags:

java

heroku

On Heroku, can you programmatically, from within an app, get some kind of identifier for the dyno currently executing your code? For example the dyno name (e.g. "web.1" or "worker.1"), or some other id.

If yes, how to do this in Java?

like image 394
Jonik Avatar asked May 04 '13 08:05

Jonik


People also ask

How many dynos do I need Heroku?

Standard-2x dynos offer the best combination of price and performance for most apps. If you encounter memory quota warnings on 2x dynos, you should make the jump to Performance-L. For either dyno type, autoscaling is the only way to know how many dynos you should be running.

What is dynos in Heroku?

As explained in Heroku's docs, Dynos are simply lightweight Linux containers dedicated to running your application processes. At the most basic level, a newly deployed app to Heroku will be supported by one Dyno for running web processes.

What is worker in Heroku?

Worker: Worker dynos can be of any process type declared in your Procfile, other than “web”. Worker dynos are typically used for background jobs, queueing systems, and timed jobs. You can have multiple kinds of worker dynos in your application. For example, one for urgent jobs and another for long-running jobs.


2 Answers

There is always the hostname of the machine (which looks something like d.LONG_GUID. I suppose (though haven't tried) that this should work:

String localhostname = java.net.InetAddress.getLocalHost().getHostName();

Also, a little known secret, but you can get the "web.1", "web.2" id's by looking at the value of the environment variable PS1

String hostId = System.getenv("PS1")

EDIT 2013-July-26

As per Heroku changelog, the local environment variable DYNO now replaces PS, which replaced PS1.

like image 86
Nitzan Shaked Avatar answered Sep 28 '22 04:09

Nitzan Shaked


Heroku team have addressed that issue and now the Dyno Manager adds DYNO environment variables that holds identifier of your Dyno e.g. web.1, web.2, foo.1 etc. However, the variable is still experimental and subject to change or removal.

You can get value of that variable using System.getenv(String) in Java.

Example:

final String dynoId = System.getenv("DYNO");
final Matcher matcher = Pattern.compile("(\\w+)\\.(\\d+)").matcher(dynoId);

String id = null;

if(matcher.find()) {
    id = matcher.group(2); // returns index: 1
    // matcher.group(1) - returns name: web
}

I hope that will help

like image 33
Tom Avatar answered Sep 28 '22 04:09

Tom