forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeanEngineSystemHandlers.cs
141 lines (129 loc) · 5.49 KB
/
LeanEngineSystemHandlers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* 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.
*
*/
using System;
using System.ComponentModel.Composition;
using QuantConnect.Configuration;
using QuantConnect.Interfaces;
using QuantConnect.Lean.Engine.Server;
using QuantConnect.Logging;
using QuantConnect.Util;
namespace QuantConnect.Lean.Engine
{
/// <summary>
/// Provides a container for the system level handlers
/// </summary>
public class LeanEngineSystemHandlers : IDisposable
{
private readonly IApi _api;
private readonly IMessagingHandler _notify;
private readonly IJobQueueHandler _jobQueue;
private readonly ILeanManager _leanManager;
/// <summary>
/// Gets the api instance used for communicating algorithm limits, status, and storing of log data
/// </summary>
public IApi Api
{
get { return _api; }
}
/// <summary>
/// Gets the messaging handler instance used for communicating various packets to listeners, including
/// debug/log messages, email/sms/web messages, as well as results and run time errors
/// </summary>
public IMessagingHandler Notify
{
get { return _notify; }
}
/// <summary>
/// Gets the job queue responsible for acquiring and acknowledging an algorithm job
/// </summary>
public IJobQueueHandler JobQueue
{
get { return _jobQueue; }
}
/// <summary>
/// Gets the ILeanManager implementation using to enhance the hosting environment
/// </summary>
public ILeanManager LeanManager
{
get { return _leanManager; }
}
/// <summary>
/// Initializes a new instance of the <see cref="LeanEngineSystemHandlers"/> class with the specified handles
/// </summary>
/// <param name="jobQueue">The job queue used to acquire algorithm jobs</param>
/// <param name="api">The api instance used for communicating limits and status</param>
/// <param name="notify">The messaging handler user for passing messages from the algorithm to listeners</param>
/// <param name="leanManager"></param>
public LeanEngineSystemHandlers(IJobQueueHandler jobQueue, IApi api, IMessagingHandler notify, ILeanManager leanManager)
{
if (jobQueue == null)
{
throw new ArgumentNullException("jobQueue");
}
if (api == null)
{
throw new ArgumentNullException("api");
}
if (notify == null)
{
throw new ArgumentNullException("notify");
}
if (leanManager == null)
{
throw new ArgumentNullException("leanManager");
}
_api = api;
_jobQueue = jobQueue;
_notify = notify;
_leanManager = leanManager;
}
/// <summary>
/// Creates a new instance of the <see cref="LeanEngineSystemHandlers"/> class from the specified composer using type names from configuration
/// </summary>
/// <param name="composer">The composer instance to obtain implementations from</param>
/// <returns>A fully hydrates <see cref="LeanEngineSystemHandlers"/> instance.</returns>
/// <exception cref="CompositionException">Throws a CompositionException during failure to load</exception>
public static LeanEngineSystemHandlers FromConfiguration(Composer composer)
{
return new LeanEngineSystemHandlers(
composer.GetExportedValueByTypeName<IJobQueueHandler>(Config.Get("job-queue-handler")),
composer.GetExportedValueByTypeName<IApi>(Config.Get("api-handler")),
composer.GetExportedValueByTypeName<IMessagingHandler>(Config.Get("messaging-handler")),
composer.GetExportedValueByTypeName<ILeanManager>(Config.Get("lean-manager-type", "LocalLeanManager")));
}
/// <summary>
/// Initializes the Api, Messaging, and JobQueue components
/// </summary>
public void Initialize()
{
Api.Initialize(Config.GetInt("job-user-id", 0), Config.Get("api-access-token", ""), Config.Get("data-folder"));
Notify.Initialize();
JobQueue.Initialize(Api);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
Log.Trace("LeanEngineSystemHandlers.Dispose(): start...");
Api.DisposeSafely();
LeanManager.DisposeSafely();
Notify.DisposeSafely();
Log.Trace("LeanEngineSystemHandlers.Dispose(): Disposed of system handlers.");
}
}
}