package com.peterphi.rules.daemon;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.peterphi.rules.RulesEngine;
import com.peterphi.rules.types.RuleSet;
import com.peterphi.rules.types.Rules;
import com.peterphi.std.annotation.Doc;
import com.peterphi.std.guice.common.daemon.GuiceDaemon;
import com.peterphi.std.guice.common.daemon.GuiceRecurringDaemon;
import com.peterphi.std.guice.common.lifecycle.GuiceLifecycleListener;
import com.peterphi.std.guice.common.metrics.GuiceMetricNames;
import com.peterphi.std.guice.common.serviceprops.composite.GuiceConfig;
import com.peterphi.std.guice.common.serviceprops.composite.GuiceConfigChangeObserver;
import com.peterphi.std.guice.common.serviceprops.jaxbref.JAXBResourceFactory;
import com.peterphi.std.threading.Timeout;
import ognl.OgnlException;

/**
 * Created by bmcleod on 08/09/2016.
 */
@Doc("Periodically assess and runs a set of configured rules")
public class RulesDaemon extends GuiceRecurringDaemon implements GuiceConfigChangeObserver, GuiceLifecycleListener
{
	@Inject
	Provider<Rules> rulesProvider;

	@Inject
	RulesEngine rulesEngine;

	public static final String IGNORE_METHOD_ERRORS = "rules.daemon.ignore.method.errors";

	@Inject(optional = true)
	@Named(IGNORE_METHOD_ERRORS)
	Boolean ignoreMethodErrors = false;

	@Inject
	GuiceConfig guiceConfig;

	boolean enabled = true;

	@Inject
	protected RulesDaemon(@Named("rules.daemon.sleep.time") final Timeout sleepTime)
	{
		super(sleepTime);
	}

	@Override
	public void postConstruct()
	{
		super.postConstruct();
		guiceConfig.registerChangeObserver(this);
	}

	@Override
	protected void execute() throws Exception
	{
		Rules rules = rulesProvider.get();
		rulesEngine.run(rules, true);
	}


	@Override
	public void propertyChanged(final String name)
	{
		if (IGNORE_METHOD_ERRORS.equals(name))
		{
			ignoreMethodErrors = guiceConfig.getBoolean(name, false);
		}
	}


	public boolean isEnabled()
	{
		return enabled;
	}


	public void setEnabled(final boolean enabled)
	{
		this.enabled = enabled;
	}
}

@@ -213,147 +213,49 @@
 Rule
-Set;%0Aimport com.peterphi.rules.types.Rules;%0Aimport com.peterphi.std.annotation.Doc;%0Aimport com.peterphi.std.guice.common.daemon.GuiceDaemon
+s;%0Aimport com.peterphi.std.annotation.Doc
 ;%0Aim
@@ -393,71 +393,8 @@
 er;%0A
-import com.peterphi.std.guice.common.metrics.GuiceMetricNames;%0A
 impo
@@ -553,87 +553,8 @@
 er;%0A
-import com.peterphi.std.guice.common.serviceprops.jaxbref.JAXBResourceFactory;%0A
 impo
@@ -595,35 +595,8 @@
 out;
-%0Aimport ognl.OgnlException;
 %0A%0A/*
@@ -1115,16 +1115,80 @@
 onfig;%0A%0A
+%09@Inject(optional = true)%0A%09@Named(%22daemon.RulesDaemon.enabled%22)%0A
 %09boolean
@@ -1201,24 +1201,25 @@
 ed = true;%0A%0A
+%0A
 %09@Inject%0A%09pr
@@ -1320,24 +1320,25 @@
 pTime);%0A%09%7D%0A%0A
+%0A
 %09@Override%0A%09
@@ -1441,16 +1441,17 @@
 s);%0A%09%7D%0A%0A
+%0A
 %09@Overri
@@ -1499,16 +1499,69 @@
 tion%0A%09%7B%0A
+%09%09if (!enabled)%0A%09%09%09return; // We have been disabled%0A%0A
 %09%09Rules 

Add ability to disable rules daemon