Just to give some background, I have a Play 2.4 application which uses Scala 2.11.8.
I deploy it to an AWS stack using OpsWorks which has 2 available instances. In the application have a module which uses an ActorSystem scheduler to periodically execute a method to do some cleanup operation. When running one instance of the app locally the cleanup works fine, but once the app is deployed the cleanup is unnecessarily run on every instance.
My question is not specific to the stack I am using, but rather a high-level approach to programmatically limit an operation to a single instance once the app is deployed. For the purpose of this question, assume I do not have access to the stack configuration.
I've searched for similar questions on S.O. but can't seem to find this particular issue. I also don't want to hardcode hostnames because that isn't a reliable solution.
Here is a suggestion that you could possible do:
Containerise your application using Docker
Use an orchestrator like Kubernetes or Docker Swarm
Set up service discovery for your application server (Your play application in this case)
Set up a Load Balancer that fronts your application server
By sending a HTTP request to your server (your Load Balancer), it is guaranteed that the request lands in just one instance of your application server which will then perform the needed clean up job.