// Path: oncue-common/src/main/java/oncue/common/messages/ThrottledWorkRequest.java
// public class ThrottledWorkRequest extends AbstractWorkRequest {
// 
// 	private static final long serialVersionUID = -6509063237201496945L;
// 
// 	private int maxJobs;
// 
// 	/**
// 	 * @param jobs
// 	 *            is the number of jobs the agent can cope with
// 	 */
// 	public ThrottledWorkRequest(ActorRef agent, Set<String> workerTypes, int jobs) {
// 		super(agent, workerTypes);
// 		this.maxJobs = jobs;
// 	}
// 
// 	public int getMaxJobs() {
// 		return maxJobs;
// 	}
// 
// 	@Override
// 	public String toString() {
// 		StringBuilder builder = new StringBuilder();
// 		for (String workerType : getWorkerTypes()) {
// 			builder.append("[" + workerType + "]");
// 		}
// 		return "Throttled work request for " + maxJobs + " jobs for worker types: " + builder.toString();
// 	}
// 
// }


import java.util.Set;
import oncue.common.messages.ThrottledWorkRequest;
import com.typesafe.config.Config;
/*******************************************************************************
 * Copyright 2013 Michael Marconi
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package oncue.agent;




/**
 * This agent will work on the configured maximum number of jobs at any one
 * time, before asking for more work.
 */
public class ThrottledAgent extends AbstractAgent {

	// The maximum number of concurrent workers
	private final Integer MAX_WORKERS;

	public ThrottledAgent(Set<String> workerTypes) {
		super(workerTypes);
		Config config = getContext().system().settings().config();
		if (!config.hasPath("oncue.agent.throttled-agent.max-jobs")) {
			throw new RuntimeException(
					"Configuration is missing the maximum concurrent jobs configuration for the throttled agent.");
		}
		MAX_WORKERS = config.getInt("oncue.agent.throttled-agent.max-jobs");
		log.info("The throttled agent will process a maximum of {} jobs in parallel", MAX_WORKERS);
	}

	@Override
	protected void requestWork() {
		/*
		 * Don't request work if this agent is already dealing with all the jobs
		 * it can manage
		 */
		if (jobsInProgress.size() < MAX_WORKERS) {
			int jobsToRequest = MAX_WORKERS - jobsInProgress.size();
			log.debug("Requesting {} new jobs", jobsToRequest);
// Path: oncue-common/src/main/java/oncue/common/messages/ThrottledWorkRequest.java
// public class ThrottledWorkRequest extends AbstractWorkRequest {
// 
// 	private static final long serialVersionUID = -6509063237201496945L;
// 
// 	private int maxJobs;
// 
// 	/**
// 	 * @param jobs
// 	 *            is the number of jobs the agent can cope with
// 	 */
// 	public ThrottledWorkRequest(ActorRef agent, Set<String> workerTypes, int jobs) {
// 		super(agent, workerTypes);
// 		this.maxJobs = jobs;
// 	}
// 
// 	public int getMaxJobs() {
// 		return maxJobs;
// 	}
// 
// 	@Override
// 	public String toString() {
// 		StringBuilder builder = new StringBuilder();
// 		for (String workerType : getWorkerTypes()) {
// 			builder.append("[" + workerType + "]");
// 		}
// 		return "Throttled work request for " + maxJobs + " jobs for worker types: " + builder.toString();
// 	}
// 
// }


// Path: oncue-agent/src/main/java/oncue/agent/ThrottledAgent.java
import java.util.Set;
import oncue.common.messages.ThrottledWorkRequest;
import com.typesafe.config.Config;

/*******************************************************************************
 * Copyright 2013 Michael Marconi
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package oncue.agent;




/**
 * This agent will work on the configured maximum number of jobs at any one
 * time, before asking for more work.
 */
public class ThrottledAgent extends AbstractAgent {

	// The maximum number of concurrent workers
	private final Integer MAX_WORKERS;

	public ThrottledAgent(Set<String> workerTypes) {
		super(workerTypes);
		Config config = getContext().system().settings().config();
		if (!config.hasPath("oncue.agent.throttled-agent.max-jobs")) {
			throw new RuntimeException(
					"Configuration is missing the maximum concurrent jobs configuration for the throttled agent.");
		}
		MAX_WORKERS = config.getInt("oncue.agent.throttled-agent.max-jobs");
		log.info("The throttled agent will process a maximum of {} jobs in parallel", MAX_WORKERS);
	}

	@Override
	protected void requestWork() {
		/*
		 * Don't request work if this agent is already dealing with all the jobs
		 * it can manage
		 */
		if (jobsInProgress.size() < MAX_WORKERS) {
			int jobsToRequest = MAX_WORKERS - jobsInProgress.size();
			log.debug("Requesting {} new jobs", jobsToRequest);
			getScheduler().tell(new ThrottledWorkRequest(getSelf(), getWorkerTypes(), jobsToRequest), getSelf());