Skip to content

Commit

Permalink
Exit properly on premature IB Gateway exit events (#87)
Browse files Browse the repository at this point in the history
* Detect disconnected IBAutomater on weekly restart.

This allows to recover from cases where the automater is not running so the restart would never be triggered.

* Shut down on premature automater exit

* Bumped IBAutomater version to 2.0.77

* Bumped IBAutomater version to 2.0.77
  • Loading branch information
jhonabreul authored Oct 27, 2023
1 parent d0f500d commit a405fb0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4447,8 +4447,16 @@ private void StartGatewayWeeklyRestartTask()
{
Log.Trace($"InteractiveBrokersBrokerage.StartGatewayWeeklyRestartTask(): triggering weekly restart manually");

// stopping the gateway will make the IBAutomater emit the exit event, which will trigger the restart
_ibAutomater?.Stop();
if (_ibAutomater.IsRunning())
{
// stopping the gateway will make the IBAutomater emit the exit event, which will trigger the restart
_ibAutomater?.Stop();
}
else
{
// if the gateway is not running, we start it
CheckIbAutomaterError(_ibAutomater.Start(false));
}
}
else
{
Expand Down Expand Up @@ -4480,11 +4488,16 @@ private void OnIbAutomaterExited(object sender, ExitedEventArgs e)
_stateManager.Reset();
StopGatewayRestartTask();

// check if IBGateway was closed because of an IBAutomater error
if (_isDisposeCalled)
{
return;
}

// check if IBGateway was closed because of an IBAutomater error, die if so
var result = _ibAutomater.GetLastStartResult();
CheckIbAutomaterError(result, false);

if (!result.HasError && !_isDisposeCalled)
if (!result.HasError)
{
// IBGateway was closed by IBAutomater because the auto-restart token expired or it was closed manually (less likely)
Log.Trace("InteractiveBrokersBrokerage.OnIbAutomaterExited(): IBGateway close detected, restarting IBAutomater...");
Expand Down Expand Up @@ -4519,6 +4532,10 @@ private void OnIbAutomaterExited(object sender, ExitedEventArgs e)
}
}, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.Default);
}
else
{
OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Error, "IBAutomaterError", result.ErrorMessage));
}
}

private TimeSpan GetRestartDelay()
Expand Down Expand Up @@ -4611,7 +4628,7 @@ public static DateTime ComputeNextWeeklyRestartTimeUtc(TimeSpan weeklyRestartUtc
/// </summary>
private DateTime GetNextWeeklyRestartTimeUtc(DateTime currentDate)
{
return ComputeNextWeeklyRestartTimeUtc(_weeklyRestartUtcTime, currentDate);
return ComputeNextWeeklyRestartTimeUtc(_weeklyRestartUtcTime, currentDate);
}

private void CheckIbAutomaterError(StartResult result, bool throwException = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@

<ItemGroup>
<PackageReference Include="QuantConnect.Brokerages" Version="2.5.*" />
<PackageReference Include="QuantConnect.IBAutomater" Version="2.0.76">
<!--
Content files of dependencies are excluded by default.
This allows content files to be available in project where nuget will be consumed.
-->
<PrivateAssets>analyzers;build</PrivateAssets>
</PackageReference>
<PackageReference Include="QuantConnect.IBAutomater" Version="2.0.77">
<!--
Content files of dependencies are excluded by default.
This allows content files to be available in project where nuget will be consumed.
-->
<PrivateAssets>analyzers;build</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit a405fb0

Please sign in to comment.