2
using Rackspace.Cloud.Server.Agent.Configuration;
3
using Rackspace.Cloud.Server.Agent.Interfaces;
4
using Rackspace.Cloud.Server.Agent.Utilities;
5
using Rackspace.Cloud.Server.Common.Logging;
7
namespace Rackspace.Cloud.Server.Agent {
8
public interface ICommandQueue {
12
public class CommandQueue : ICommandQueue {
13
private readonly IXenStore _store;
14
private readonly ICommandFactory _factory;
15
private readonly ILogger _logger;
17
public CommandQueue(IXenStore store, ICommandFactory factory, ILogger logger) {
24
var commands = _store.GetCommands();
25
if (commands.Count == 0) {
26
LogManager.ShouldBeLogging = false;
30
LogManager.ShouldBeLogging = true;
31
foreach (var command in commands) {
32
ProcessCommand(command);
36
private void ProcessCommand(Command command) {
37
var removeMessageFromXenStore = true;
40
var executableResult = _factory.CreateCommand(command.name).Execute(command.value);
41
_store.Write(command.key, new Json<object>().Serialize(new { returncode = executableResult.ExitCode, message = executableResult.Output.Value() }));
42
} catch (InvalidCommandException exception) {
43
_store.Write(command.key, new Json<object>().Serialize(new { returncode = "1", message = exception.Message }));
44
} catch (UnsuccessfulCommandExecutionException exception) {
45
var result = (ExecutableResult) exception.Data["result"];
48
if (result.Output != null && !string.IsNullOrEmpty(result.Output.Value()))
49
output = ", Output:" + result.Output.Value();
50
if (result.Error != null && !string.IsNullOrEmpty(result.Error.Value()))
51
error = ", Error:" + result.Error.Value();
52
_store.Write(command.key, new Json<object>().Serialize(new
54
returncode = result.ExitCode,
55
message = exception.Message +
58
} catch(Exception ex) {
59
removeMessageFromXenStore = false;
60
_logger.Log(String.Format("Exception was : {0}\nStackTrace Was: {1}", ex.Message, ex.StackTrace));
62
if (removeMessageFromXenStore) _store.Remove(command.key);
b'\\ No newline at end of file'