'How should I go about writing code that runs on a schedule that will run on multiple containers without race conditions?

I'm wanting to write a service in nestjs that polls another service on a schedule. The problem is that if I have multiple containers how can I make sure one and only one container picks up the job? for example, say I need to obtain a session token from an upstream api on a schedule, but if I request multiple session ID's simultaneously previously requested session tokens are invalidated. Another example is say I want to poll some data source and post it into a sql table. I wouldn't want multiple containers to poll the same resource simultaneously. Is this an instance where I would want to use a cron job?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source