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?
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.
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.
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.
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With