How to configure the CRON task to run only once for each set of instances?

We host our site on AWS. We currently have 3 instances of EC2 in the same cluster using AWS load balancer.

Servers have linux, apache, java, mysql and tomcat 6.0.

We decide how to set up a task to run every hour. The obvious place for this is in the Java code, but there is one problem.

The problem is that since we have 3 instances in the cluster (they are all identical), the task will be performed 3 times per hour, and not once per hour, once per instance.

I have several ideas to overcome this, but I hoped there was a better, perhaps industry standard, on how to manage this.

One idea is to save to the database that it is already running. The task will see that it is already working today or not. I see errors there.

Another idea was to use cron installed on one of the instances in your own OS, outside the Tomcat code. This will use wget to invoke the webpage that invokes the java method. Since this will only call one of the instances, it should only start once.

Both methods look like hacks and error prone. Is there any real way to do this?

+5
source share
2 answers

cron/wget, . .

- JVM, , , . : -DschedulerEnabled=true. , .

, Quartz Clustering. , HA. , , , , .

+3

, , , , . wget , , , ( URL- , ), @sourcedelica , cron.

, , , cron .

, , (, cron, ) .

, :

CREATE TABLE `Server` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `uname` varchar(32) NOT NULL,
    `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `alive` timestamp NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
);

CREATE TABLE `Lock` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `code` varchar(128) NOT NULL,
    `pid` int(10) unsigned DEFAULT NULL,
    `server` int(10) unsigned DEFAULT NULL,
    `locked` timestamp NULL DEFAULT NULL,
    `used` timestamp NULL DEFAULT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `code` (`code`)
);

uname , ; alive .

:

SELECT * FROM Lock WHERE code='cron-cluster';

,

INSERT INTO `Lock` ...

Lock id 32. server pid NULL, id , .

UPDATE Lock SET server=1,pid=4233 WHERE id=32 AND server IS NULL and pid IS NULL;

, , (, n ):

SELECT COUNT(id) FROM Lock WHERE code='cron-cluster' AND server=1 AND pid=4233;

1, , 0 , .

, , - ; , Lock, server alive , , , server .

server, , ..

, , .

+3

All Articles