/*
* 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
{
///
/// Provides a container for the system level handlers
///
public class LeanEngineSystemHandlers : IDisposable
{
private readonly IApi _api;
private readonly IMessagingHandler _notify;
private readonly IJobQueueHandler _jobQueue;
private readonly ILeanManager _leanManager;
///
/// Gets the api instance used for communicating algorithm limits, status, and storing of log data
///
public IApi Api
{
get { return _api; }
}
///
/// 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
///
public IMessagingHandler Notify
{
get { return _notify; }
}
///
/// Gets the job queue responsible for acquiring and acknowledging an algorithm job
///
public IJobQueueHandler JobQueue
{
get { return _jobQueue; }
}
///
/// Gets the ILeanManager implementation using to enhance the hosting environment
///
public ILeanManager LeanManager
{
get { return _leanManager; }
}
///
/// Initializes a new instance of the class with the specified handles
///
/// The job queue used to acquire algorithm jobs
/// The api instance used for communicating limits and status
/// The messaging handler user for passing messages from the algorithm to listeners
///
public LeanEngineSystemHandlers(IJobQueueHandler jobQueue, IApi api, IMessagingHandler notify, ILeanManager leanManager)
{
if (jobQueue == null)
{
throw new ArgumentNullException(nameof(jobQueue));
}
if (api == null)
{
throw new ArgumentNullException(nameof(api));
}
if (notify == null)
{
throw new ArgumentNullException(nameof(notify));
}
if (leanManager == null)
{
throw new ArgumentNullException(nameof(leanManager));
}
_api = api;
_jobQueue = jobQueue;
_notify = notify;
_leanManager = leanManager;
}
///
/// Creates a new instance of the class from the specified composer using type names from configuration
///
/// The composer instance to obtain implementations from
/// A fully hydrates instance.
/// Throws a CompositionException during failure to load
public static LeanEngineSystemHandlers FromConfiguration(Composer composer)
{
return new LeanEngineSystemHandlers(
composer.GetExportedValueByTypeName(Config.Get("job-queue-handler", "QuantConnect.Queues.JobQueue")),
composer.GetExportedValueByTypeName(Config.Get("api-handler", "QuantConnect.Api.Api")),
composer.GetExportedValueByTypeName(Config.Get("messaging-handler", "QuantConnect.Messaging.Messaging")),
composer.GetExportedValueByTypeName(Config.Get("lean-manager-type", "LocalLeanManager")));
}
///
/// Initializes the Api, Messaging, and JobQueue components
///
public void Initialize()
{
Notify.Initialize(new MessagingHandlerInitializeParameters(Api));
JobQueue.Initialize(Api, Notify);
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
/// 2
public void Dispose()
{
Log.Trace("LeanEngineSystemHandlers.Dispose(): start...");
LeanManager.DisposeSafely();
Notify.DisposeSafely();
Api.DisposeSafely();
Log.Trace("LeanEngineSystemHandlers.Dispose(): Disposed of system handlers.");
}
}
}