Node Singleton Job

One job per node (instance)

  • If you want to run a service with one instance on every node in the cluster, Node Singleton job is the answer
  • Context Variable: job
  • Running Job Handle Variable: THIS

Example

1) A Binary rule (CityFeeder)

Binary Rule

  • This rule takes a line separated file contains city name per line and pushes into a queue
def queue = grid.stringQueue("CITIES"); //create or get the queue

def lines = msg.lines();
int count = 0;

for(line in lines){
    
    def city = line.trim();
    
    if("".equals(city)) continue; //ignore blank lines
    
    queue.offer(city);
    
    ++count;
}

log.info("%d cities feeded", count);

2) NodeSingleton Job - (PollCityData)

  • This job takes the city names from the queue and fetch the weather data from the 3rd party API service
//def API_KEY = "XXXXXXXXXXXXXX";
def API_KEY = domain.get("api_key"); //get the stored API key
def API_URL = "https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api}&units={units}";
def queue = grid.stringQueue("CITIES"); //create or get the queue

try{

    log.info("PollCityData running...");

    while(THIS.isRunning()){
        
        def city = queue.take(); //wait till a city name arrives in the queue
        
        try{
            
            def res = rest.get(API_URL)
                            .routeParam("city", city)
                            .routeParam("api", API_KEY)
                            .routeParam("units", "metric")
                            .asJson()
                            .getBody()
                            ;
            def main = res.getObject().getJSONObject("main").toString();
            
            log.info("Node:%s - %s", THIS.getNodeId(), main);

        }catch(Exception ex){
            log.error(ex);
        }
        
    }

}finally{
    log.info("PollCityData stopped.");
}

See Also

Related REST APIs

[Create / Update Job(ref:upsertjob)
Get Job
Delete Job
Count Jobs
List Jobs
Set Job State
Count Running Jobs
List Running Jobs
Start Job
Restart Job
Count All Running Jobs