2015-03-29 00:57:13 +01:00
/ *
* Copyright Elasticsearch B . V . and / or licensed to Elasticsearch B . V . under one
* or more contributor license agreements . Licensed under the Elastic License ;
* you may not use this file except in compliance with the Elastic License .
* /
package org.elasticsearch.watcher.trigger ;
import org.elasticsearch.common.collect.ImmutableMap ;
import org.elasticsearch.common.component.AbstractComponent ;
import org.elasticsearch.common.inject.Inject ;
import org.elasticsearch.common.settings.Settings ;
import org.elasticsearch.common.xcontent.XContentParser ;
import java.io.IOException ;
import java.util.Collection ;
import java.util.List ;
import java.util.Set ;
import java.util.concurrent.CopyOnWriteArrayList ;
/ * *
*
* /
public class TriggerService extends AbstractComponent {
private final Listeners listeners ;
private final ImmutableMap < String , TriggerEngine > engines ;
@Inject
public TriggerService ( Settings settings , Set < TriggerEngine > engines ) {
super ( settings ) ;
listeners = new Listeners ( ) ;
ImmutableMap . Builder < String , TriggerEngine > builder = ImmutableMap . builder ( ) ;
for ( TriggerEngine engine : engines ) {
builder . put ( engine . type ( ) , engine ) ;
engine . register ( listeners ) ;
}
this . engines = builder . build ( ) ;
}
public synchronized void start ( Collection < ? extends TriggerEngine . Job > jobs ) {
for ( TriggerEngine engine : engines . values ( ) ) {
engine . start ( jobs ) ;
}
}
public synchronized void stop ( ) {
for ( TriggerEngine engine : engines . values ( ) ) {
engine . stop ( ) ;
}
}
public void add ( TriggerEngine . Job job ) {
engines . get ( job . trigger ( ) . type ( ) ) . add ( job ) ;
}
2015-04-08 15:31:00 +02:00
/ * *
* Removes the job associated with the given name from this trigger service .
*
* @param jobName The name of the job to remove
* @return { @code true } if the job existed and removed , { @code false } otherwise .
* /
2015-03-29 00:57:13 +01:00
public boolean remove ( String jobName ) {
for ( TriggerEngine engine : engines . values ( ) ) {
if ( engine . remove ( jobName ) ) {
return true ;
}
}
return false ;
}
public void register ( TriggerEngine . Listener listener ) {
listeners . add ( listener ) ;
}
public Trigger parseTrigger ( String jobName , XContentParser parser ) throws IOException {
XContentParser . Token token = parser . currentToken ( ) ;
assert token = = XContentParser . Token . START_OBJECT ;
token = parser . nextToken ( ) ;
if ( token ! = XContentParser . Token . FIELD_NAME ) {
2015-05-02 01:15:30 +02:00
throw new TriggerException ( " could not parse trigger for [{}]. expected trigger type string field, but found [{}] " , jobName , token ) ;
2015-03-29 00:57:13 +01:00
}
String type = parser . text ( ) ;
token = parser . nextToken ( ) ;
if ( token ! = XContentParser . Token . START_OBJECT ) {
2015-05-02 01:15:30 +02:00
throw new TriggerException ( " could not parse trigger [{}] for [{}]. expected trigger an object as the trigger body, but found [{}] " , type , jobName , token ) ;
2015-03-29 00:57:13 +01:00
}
Trigger trigger = parseTrigger ( jobName , type , parser ) ;
token = parser . nextToken ( ) ;
2015-05-02 01:15:30 +02:00
if ( token ! = XContentParser . Token . END_OBJECT ) {
throw new TriggerException ( " could not parse trigger [{}] for [{}]. expected [END_OBJECT] token, but found [{}] " , type , jobName , token ) ;
}
2015-03-29 00:57:13 +01:00
return trigger ;
}
public Trigger parseTrigger ( String jobName , String type , XContentParser parser ) throws IOException {
TriggerEngine engine = engines . get ( type ) ;
assert engine ! = null ;
return engine . parseTrigger ( jobName , parser ) ;
}
public TriggerEvent parseTriggerEvent ( String historyRecordId , XContentParser parser ) throws IOException {
XContentParser . Token token = parser . currentToken ( ) ;
assert token = = XContentParser . Token . START_OBJECT ;
token = parser . nextToken ( ) ;
if ( token ! = XContentParser . Token . FIELD_NAME ) {
throw new TriggerException ( " could not parse trigger event for [ " + historyRecordId + " ]. expected trigger type string field, but found [ " + token + " ] " ) ;
}
String type = parser . text ( ) ;
token = parser . nextToken ( ) ;
if ( token ! = XContentParser . Token . START_OBJECT ) {
2015-05-02 01:15:30 +02:00
throw new TriggerException ( " could not parse trigger event [ " + type + " ] for [ " + historyRecordId + " ]. expected trigger an object as the trigger body, but found [ " + token + " ] " ) ;
2015-03-29 00:57:13 +01:00
}
TriggerEvent trigger = parseTriggerEvent ( historyRecordId , type , parser ) ;
token = parser . nextToken ( ) ;
2015-05-02 01:15:30 +02:00
if ( token ! = XContentParser . Token . END_OBJECT ) {
throw new TriggerException ( " could not parse trigger [{}] for [{}]. expected [END_OBJECT] token, but found [{}] " , type , historyRecordId , token ) ;
}
2015-03-29 00:57:13 +01:00
return trigger ;
}
public TriggerEvent parseTriggerEvent ( String context , String type , XContentParser parser ) throws IOException {
TriggerEngine engine = engines . get ( type ) ;
2015-05-02 01:15:30 +02:00
if ( engine = = null ) {
throw new TriggerException ( " Unknown trigger type [{}] " , type ) ;
}
2015-03-29 00:57:13 +01:00
return engine . parseTriggerEvent ( context , parser ) ;
}
static class Listeners implements TriggerEngine . Listener {
private List < TriggerEngine . Listener > listeners = new CopyOnWriteArrayList < > ( ) ;
public void add ( TriggerEngine . Listener listener ) {
listeners . add ( listener ) ;
}
@Override
2015-04-16 12:25:31 +02:00
public void triggered ( Iterable < TriggerEvent > events ) {
2015-03-29 00:57:13 +01:00
for ( TriggerEngine . Listener listener : listeners ) {
2015-04-16 12:25:31 +02:00
listener . triggered ( events ) ;
2015-03-29 00:57:13 +01:00
}
}
}
}