Lean  $LEAN_TAG$
LeanEngineSystemHandlers.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
18 using System.ComponentModel.Composition;
22 using QuantConnect.Logging;
23 using QuantConnect.Util;
24 
26 {
27  /// <summary>
28  /// Provides a container for the system level handlers
29  /// </summary>
30  public class LeanEngineSystemHandlers : IDisposable
31  {
32  private readonly IApi _api;
33  private readonly IMessagingHandler _notify;
34  private readonly IJobQueueHandler _jobQueue;
35  private readonly ILeanManager _leanManager;
36 
37  /// <summary>
38  /// Gets the api instance used for communicating algorithm limits, status, and storing of log data
39  /// </summary>
40  public IApi Api
41  {
42  get { return _api; }
43  }
44 
45  /// <summary>
46  /// Gets the messaging handler instance used for communicating various packets to listeners, including
47  /// debug/log messages, email/sms/web messages, as well as results and run time errors
48  /// </summary>
50  {
51  get { return _notify; }
52  }
53 
54  /// <summary>
55  /// Gets the job queue responsible for acquiring and acknowledging an algorithm job
56  /// </summary>
58  {
59  get { return _jobQueue; }
60  }
61 
62  /// <summary>
63  /// Gets the ILeanManager implementation using to enhance the hosting environment
64  /// </summary>
66  {
67  get { return _leanManager; }
68  }
69 
70  /// <summary>
71  /// Initializes a new instance of the <see cref="LeanEngineSystemHandlers"/> class with the specified handles
72  /// </summary>
73  /// <param name="jobQueue">The job queue used to acquire algorithm jobs</param>
74  /// <param name="api">The api instance used for communicating limits and status</param>
75  /// <param name="notify">The messaging handler user for passing messages from the algorithm to listeners</param>
76  /// <param name="leanManager"></param>
78  {
79  if (jobQueue == null)
80  {
81  throw new ArgumentNullException(nameof(jobQueue));
82  }
83  if (api == null)
84  {
85  throw new ArgumentNullException(nameof(api));
86  }
87  if (notify == null)
88  {
89  throw new ArgumentNullException(nameof(notify));
90  }
91  if (leanManager == null)
92  {
93  throw new ArgumentNullException(nameof(leanManager));
94  }
95  _api = api;
96  _jobQueue = jobQueue;
97  _notify = notify;
98  _leanManager = leanManager;
99  }
100 
101  /// <summary>
102  /// Creates a new instance of the <see cref="LeanEngineSystemHandlers"/> class from the specified composer using type names from configuration
103  /// </summary>
104  /// <param name="composer">The composer instance to obtain implementations from</param>
105  /// <returns>A fully hydrates <see cref="LeanEngineSystemHandlers"/> instance.</returns>
106  /// <exception cref="CompositionException">Throws a CompositionException during failure to load</exception>
108  {
109  return new LeanEngineSystemHandlers(
110  composer.GetExportedValueByTypeName<IJobQueueHandler>(Config.Get("job-queue-handler")),
111  composer.GetExportedValueByTypeName<IApi>(Config.Get("api-handler")),
112  composer.GetExportedValueByTypeName<IMessagingHandler>(Config.Get("messaging-handler")),
113  composer.GetExportedValueByTypeName<ILeanManager>(Config.Get("lean-manager-type", "LocalLeanManager")));
114  }
115 
116  /// <summary>
117  /// Initializes the Api, Messaging, and JobQueue components
118  /// </summary>
119  public void Initialize()
120  {
123  }
124 
125  /// <summary>
126  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
127  /// </summary>
128  /// <filterpriority>2</filterpriority>
129  public void Dispose()
130  {
131  Log.Trace("LeanEngineSystemHandlers.Dispose(): start...");
132 
133  LeanManager.DisposeSafely();
134  Notify.DisposeSafely();
135  Api.DisposeSafely();
136 
137  Log.Trace("LeanEngineSystemHandlers.Dispose(): Disposed of system handlers.");
138  }
139  }
140 }