Distributed CRON in Kubernetes - java

distributed CRON in Kubernetes is still a work in progress (https://github.com/kubernetes/kubernetes/issues/2156).
What do you use for CRON jobs in Kubernetes today?
Do you recommend any solution that works well with Spring/JVM-based services? Spring/JVM startup time is quite high and if CRON scheduler started a new JVM for each job, startup time might be much higher than time of actual work - is there any solution that could run the job in existing JVM?
Thank you,
Jakub

I think Mesos Chronos is still ideal solution.

I wrote a small Go app that functions like cron but writes log info to stdout (no email!) and can be built into a static binary for easy containerization.
I built kubectl from source as a static binary and included it in the image (it may be a static binary in the most recent releases). Kubectl will automatically look for the service account token/certs in /var/run/secrets/kubernetes.io/serviceaccount/ so you should be good to go unless you're not using the default service account.
I then set up a crontab to run kubectl to create a job at the period that I wanted. The crontab and yaml files for the jobs can be mounted as a secret. You can either use conf2kube or some other way of generating the secrets. I wrote a simple python script.
It's totally a workaround until there is proper support but I hope that helps.

I'm using cron jobs in kubernetes with java, each job launches a new JVM, so no. No reuse here.
To reuse you must jave something like a webapp always running and schedule jobs to run inside this already running app.

Related

Reliable way to create scheduler for executing Java program in Windows

I have a task in which, I have monitor a folder for new files and upload its content to database. For this, I have written a Java Program, created an executable JAR and scheduled it for execution after fix interval i.e after 5 min. using Windows Task Scheduler on Windows 7.
I have to ask about reliability of this approach. How reliable is Task Scheduler? Is there any better approach like creating a windows service ,etc by which I can schedule my Program/ JAR.
Any suggestions would be appreciated.
Thanks!
Best Regards,
Kunal
Take a look on Quartz
Quartz Scheduler is a Java-based open-source job scheduling service which is used in most of the applications to schedule their timely jobs.

Creating a background process using java

I want run a background process at a specific time. I want that process to be run on the server even without running the application from End-User side. The application is made using Spring. Please suggest how to approach for it.
Thanks and regards
Souvik
I depends highly on what platform you are working on, and what you want to achieve.
If it is a simple application, that you simply want to invoke that on specific time, then you can use scheduling tools available on your platform, for example, crontab for Unix, or scheduled task (at) for Windows.
If you want the application to be run as a daemon process, and the application itself will handle the scheduling, then you need to solve two problem: 1. create a daemon process (aka system service), and 2. doing scheduling in Java.
For problem 1, there are already answer for it. Just have a search on Google on "Java System Service" will give you some other useful tools too, like Java Service Wrapper
For problem 2, there are a lot of way to perform scheduling in Java. You can do it by built-in Timer etc, or using scheduling library like Quartz

Synchronize cronjobs on multiple servers

I am having multiple servers which run the same back-end process in JAVA through cron-tab scheduling.
Earlier I had just one server on which I used to schedule all jobs. This was easier to manage.
Now with increased servers and same process running on all servers with same code base, I want to schedule same cron-jobs on all machines.
I am facing problem, because I have to face lot of trouble and do manual work to change / update all servers if there is a single change.
Can anybody tell me some way to manage scheduling on multiple servers from a common point ?
Is there any tool / library / API etc to handle this kind of situation ?
As of now, I am updating / copying (using SCP) JAR on all servers and then restart all processes by individually logging into to each of the servers.
As of now, I will prefer If I do not have to change my JAVA code to manage this(In log run, I can think of that).
However, If I can create a small JAVA application (using Quartz) OR even a shell script to handle scheduling, I will go for it.
I want to be clear that updating JAR on all servers is not my problem, I am working on setup build script(ANT) for that.
My concern is common scheduling code for all servers.
Is there a way to schedule jobs on one server which starts jobs on remote server ? If this happens, I only have to manage scheduling on one server. I wont need to worry about clock sync issues as well ..
You can create a text file, containing the crontab entries in it, and when you deploy the jar file in each server, you can update the crontab at the same time as well, using ant, by running the command crontab mycrontab.txt.

Most efficient method to schedule java application to run daily

I have a java application which downloads a file from an FTP server and I need it to run daily. I was just wondering what the best method to schedule it to run would be. The options I am currently considering are the Windows scheduler and Java timer task.
At the moment I have the windows scheduler pointing at a batch file which runs my program.
If anybody has any knowledge in the area I would appreciate to hear from you!!
Look at AT TASKS on windows and CRON JOBS on unix. These can run your Java program.
You should also take a look at Quartz which is a Java-based job scheduling system.
You can also get help from this link.
Why don't you use #Schedule abilities of EJB 3?
If your server is not EJB 3 - capable, use Quartz, Timer and TimerTask classes.

run periodic tasks on server in the background

What's the best/easiest way to run periodic tasks (like a daemon thread) on a tomcat/jetty server? How do I start the thread? Is there a simple mechanism or is this a bad idea at all?
If want to keep everything on java side, give a look to Quartz.
It handles failover and fine grained repartition of jobs, with the same flexibility of cron jobs.
It's okay and effective to stash a java.util.Timer (or better yet ScheduledExecutor) instance in your ServeletContext. Create it in a Servlet's init() call and all your servlets can add TimerTasks to it.
One general purpose way which works for many systems is simply to have a cron job which performs a periodic wget against your app.
I can't answer the tomcat/jetty stuff, but I've done similar things with Python based web apps.
I normally just run a separate app that does the periodic tasks needed. If interop is needed between the website and the app, that communication can happen through some sort of API (using something like XML-RPC/unix sockets/etc) or even just through the database layer, if that's adequate.
Hope that helps.
If you want to use a cron job but don't have administrative access to the development system, you can do a user crontab by executing the command:
crontab -e
It uses vi by default on most systems, but you can change it to the editor of your choice via:
export EDITOR=/usr/local/bin/my_editor
Then, executing the crontab -e command will launch your crontab file in your editor. Upon saving, the changes will be committed back into the system's cron.

Categories