user3239600
user3239600

Reputation: 321

How to run a scheduled task on a single openshift pod only?

Story: in my java code i have a few ScheduledFuture's that i need to run everyday on specific time (15:00 for example), the only available thing that i have is database, my current application and openshift with multiple pods. I can't move this code out of my application and must run it from there.

Problem: ScheduledFuture works on every pod, but i need to run it only once a day. I have a few ideas, but i don't know how to implement them.

Idea #1: Set environment variable to specific pod, then i will be able to check if this variable exists (and its value), read it and run schedule task if required. I know that i have a risk of hovered pods, but that's better not to run scheduled task at all than to run it multiple times.

Idea #2: Determine a leader pod somehow, this seems to be a bad idea in my case since it always have "split-brain" problem.

Idea #3 (a bit offtopic): Create my own synchronization algorithm thru database. To be fair, it's the simplest way to me since i'm a programmer and not SRE. I understand that this is not the best one tho.

Idea #4 (a bit offtopic): Just use quartz schedule library. I personally don't really like that and would prefer one of the first two ideas (if i will able to implement them), but at the moment it seems like my only valid choice.

UPD. May be you have some other suggestions or a warning that i shouldn't ever do that?

Upvotes: 1

Views: 2652

Answers (2)

Turing85
Turing85

Reputation: 20185

I would suggest to use a ready-to-use solution. Getting those things right, especially covering all possible corner-cases wrt. reliability, is hard. If you do not want to use quartz, I would at least suggest to use a database-backed solution. Postgres, for example, has SELECT ... FOR UPDATE SKIP LOCKED; (scroll down to the section "The Locking Clause") which may be used to implement one-time only scheduling.

Upvotes: 2

Maciej Niedźwiedź
Maciej Niedźwiedź

Reputation: 310

You can create cron job using openshift https://docs.openshift.com/container-platform/4.7/nodes/jobs/nodes-nodes-jobs.html and have this job trigger some endpoint in you application that will invoke your logic.

Upvotes: 1

Related Questions