~cbehrens/openstack-guest-agents/lp764221

« back to all changes in this revision

Viewing changes to src/xenserver/windows/src/Rackspace.Cloud.Server.Agent/CommandQueue.cs

  • Committer: Antony Messerli
  • Date: 2011-03-02 21:56:51 UTC
  • Revision ID: amesserl@rackspace.com-20110302215651-0clqh49spumg13c6
Initial commit Rackspace Windows Guest Agent

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
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;
 
6
 
 
7
namespace Rackspace.Cloud.Server.Agent {
 
8
    public interface ICommandQueue {
 
9
        void Work();
 
10
    }
 
11
 
 
12
    public class CommandQueue : ICommandQueue {
 
13
        private readonly IXenStore _store;
 
14
        private readonly ICommandFactory _factory;
 
15
        private readonly ILogger _logger;
 
16
 
 
17
        public CommandQueue(IXenStore store, ICommandFactory factory, ILogger logger) {
 
18
            _store = store;
 
19
            _factory = factory;
 
20
            _logger = logger;
 
21
        }
 
22
 
 
23
        public void Work() {
 
24
            var commands = _store.GetCommands();
 
25
            if (commands.Count == 0) {
 
26
                LogManager.ShouldBeLogging = false;
 
27
                return;
 
28
            }
 
29
 
 
30
            LogManager.ShouldBeLogging = true;
 
31
            foreach (var command in commands) {
 
32
                ProcessCommand(command);
 
33
            }
 
34
        }
 
35
 
 
36
        private void ProcessCommand(Command command) {
 
37
            var removeMessageFromXenStore = true;
 
38
 
 
39
            try {
 
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"];
 
46
                var output = "";
 
47
                var error = "";
 
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
 
53
                                                                           {
 
54
                                                                               returncode = result.ExitCode, 
 
55
                                                                               message = exception.Message + 
 
56
                                                                               output + error
 
57
                                                                           }));
 
58
            } catch(Exception ex) {
 
59
                removeMessageFromXenStore = false;
 
60
                _logger.Log(String.Format("Exception was : {0}\nStackTrace Was: {1}", ex.Message, ex.StackTrace));
 
61
            } finally {
 
62
                if (removeMessageFromXenStore) _store.Remove(command.key);
 
63
            }
 
64
        }
 
65
    }
 
66
}
 
 
b'\\ No newline at end of file'