/*
* 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.Collections.Generic;
using System.Threading;
using QuantConnect.Util;
namespace QuantConnect.Scheduling
{
///
/// Helper class that will monitor timer consumers and request more time if required.
/// Used by
///
public class TimeMonitor : IDisposable
{
private readonly List _timeConsumers;
private readonly Timer _timer;
///
/// Returns the number of time consumers currently being monitored
///
public int Count
{
get
{
lock (_timeConsumers)
{
return _timeConsumers.Count;
}
}
}
///
/// Creates a new instance
///
public TimeMonitor(int monitorIntervalMs = 100)
{
_timeConsumers = new List();
_timer = new Timer(state =>
{
lock (_timeConsumers)
{
_timeConsumers.RemoveAll(time => time.Finished);
foreach (var consumer in _timeConsumers)
{
if (consumer.NextTimeRequest == null)
{
// first time, for performance we register this here and not the time consumer
consumer.NextTimeRequest = consumer.TimeProvider.GetUtcNow().AddMinutes(1);
}
else if (consumer.TimeProvider.GetUtcNow() >= consumer.NextTimeRequest)
{
// each minute request additional time from the isolator
consumer.NextTimeRequest = consumer.NextTimeRequest.Value.AddMinutes(1);
try
{
// this will notify the isolator that we've exceed the limits
consumer.IsolatorLimitProvider.RequestAdditionalTime(minutes: 1);
}
catch
{
// pass
}
}
}
}
}, null, monitorIntervalMs, monitorIntervalMs);
}
///
/// Adds a new time consumer element to be monitored
///
/// Time consumer instance
public void Add(TimeConsumer consumer)
{
lock (_timeConsumers)
{
_timeConsumers.Add(consumer);
}
}
///
/// Disposes of the inner timer
///
public void Dispose()
{
_timer.DisposeSafely();
}
}
}