Skip to content

ExecAsyncCommand method added to interface #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<artifactId>core</artifactId>
<version>1.0.15</version>


<!-- LICENSE -->
<licenses>
<license>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/hobbit/core/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ private Commands() {

public static final byte REQUEST_SYSTEM_RESOURCES_USAGE = 18;

public static final byte EXECUTE_ASYNC_COMMAND = 19;

private static final ImmutableMap<Byte, String> ID_TO_COMMAND_NAME_MAP = generateMap();

private static ImmutableMap<Byte, String> generateMap() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.commons.io.IOUtils;
import org.hobbit.core.Commands;
import org.hobbit.core.Constants;
import org.hobbit.core.data.ExecuteCommandData;
import org.hobbit.core.data.StartCommandData;
import org.hobbit.core.data.StopCommandData;
import org.hobbit.core.rabbit.RabbitMQUtils;
Expand Down Expand Up @@ -60,11 +61,13 @@ public abstract class AbstractCommandReceivingComponent extends AbstractComponen
* sent via the command queue and for which an answer is expected.
*/
private String responseQueueName = null;
//private String responseQueueName2 = null;
/**
* Consumer of the queue that is used to receive responses for messages that
* are sent via the command queue and for which an answer is expected.
*/
private QueueingConsumer responseConsumer = null;
//private QueueingConsumer responseConsumer2 = null;
/**
* Factory for generating queues with which the commands are sent and
* received. It is separated from the data connections since otherwise the
Expand Down Expand Up @@ -306,6 +309,16 @@ private void initResponseQueue() throws IOException {
}
}

// private void initResponseQueue2() throws IOException {
// if (responseQueueName2 == null) {
// responseQueueName2 = cmdChannel.queueDeclare().getQueue();
// }
// if (responseConsumer2 == null) {
// responseConsumer2 = new QueueingConsumer(cmdChannel);
// cmdChannel.basicConsume(responseQueueName2, responseConsumer2);
// }
// }

/**
* @return the cmdResponseTimeout
*/
Expand All @@ -320,6 +333,34 @@ public void setCmdResponseTimeout(long cmdResponseTimeout) {
this.cmdResponseTimeout = cmdResponseTimeout;
}

/**
* This executes command against running container.
* This functionality must be enabled by platform administrator
*
* @param containerId
* the name of the container instance that should be stopped
*/
protected boolean execAsyncCommand(String containerId, String[] command) {
try {
initResponseQueue();
byte data[] = RabbitMQUtils.writeString(
gson.toJson(new ExecuteCommandData(containerId, command)));
BasicProperties props = new BasicProperties.Builder().deliveryMode(2).replyTo(responseQueueName).build();
sendToCmdQueue(Commands.EXECUTE_ASYNC_COMMAND, data, props);
QueueingConsumer.Delivery delivery = responseConsumer.nextDelivery(cmdResponseTimeout*2);
Objects.requireNonNull(delivery, "Didn't got a response for a create container message.");
if (delivery.getBody().length > 0){
if(RabbitMQUtils.readString(delivery.getBody()).equals("Succeeded"))
return true;
}


} catch (Exception e) {
LOGGER.error("Got exception while trying to execute command for the container of the \"" + containerId+ "\" image.", e);
}
return false;
}

@Override
public void close() throws IOException {
if (cmdChannel != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ protected void addContainerObserver(String containerName, ContainerStateObserver
}
}

@Override
public boolean execAsyncCommand(String containerId, String[] command) {
return super.execAsyncCommand(containerId, command);
}


@Override
public RabbitQueueFactory getFactoryForIncomingCmdQueues() {
return cmdQueueFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ public interface PlatformConnector {
public RabbitQueueFactory getFactoryForIncomingDataQueues();
public RabbitQueueFactory getFactoryForOutgoingCmdQueues();
public RabbitQueueFactory getFactoryForIncomingCmdQueues();

public boolean execAsyncCommand(String containerId, String[] command);
}
56 changes: 56 additions & 0 deletions src/main/java/org/hobbit/core/data/ExecuteCommandData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.hobbit.core.data;

/**
* @author Pavel Smirnov. ([email protected] / [email protected])
*/
/**
* This file is part of core.
*
* core is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* core is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with core. If not, see <http://www.gnu.org/licenses/>.
*/

public class ExecuteCommandData {

public String containerId;
public String[] command;

public ExecuteCommandData(String containerId, String[] command) {
this.containerId = containerId;
this.command = command;
}

public String getContainerId() {
return containerId;
}

public void setContainerId(String value) { this.containerId = value; }

public String[] getCommand() {
return command;
}

public void setCommand(String[] value) { this.command = value; }


@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("ExecuteCommandData [containerId=");
builder.append(containerId);
builder.append(", command=");
builder.append(command);
builder.append("]");
return builder.toString();
}
}