Skip to content

feat: add agent status to tray app #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 12, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -51,10 +51,10 @@ jobs:
cache-dependency-path: '**/packages.lock.json'
- name: dotnet restore
run: dotnet restore --locked-mode
- name: dotnet publish
run: dotnet publish --no-restore --configuration Release --output .\publish
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: publish
path: .\publish\
#- name: dotnet publish
# run: dotnet publish --no-restore --configuration Release --output .\publish
#- name: Upload artifact
# uses: actions/upload-artifact@v4
# with:
# name: publish
# path: .\publish\
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -403,3 +403,5 @@ FodyWeavers.xsd
.idea/**/shelf

publish
WindowsAppRuntimeInstall-x64.exe
wintun.dll
40 changes: 2 additions & 38 deletions App/App.csproj
Original file line number Diff line number Diff line change
@@ -10,18 +10,13 @@
<PublishProfile>Properties\PublishProfiles\win-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<Nullable>enable</Nullable>
<EnableMsixTooling>true</EnableMsixTooling>
<EnableMsixTooling>false</EnableMsixTooling>
<WindowsPackageType>None</WindowsPackageType>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<!-- To use CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute: -->
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
<AppxManifest Include="Package.appxmanifest">
<SubType>Designer</SubType>
</AppxManifest>
</ItemGroup>

<ItemGroup>
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>
@@ -40,43 +35,12 @@
</PackageReference>
<PackageReference Include="H.NotifyIcon.WinUI" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26100.1742" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.250108002" />
</ItemGroup>

<!--
Defining the "Msix" ProjectCapability here allows the Single-project MSIX Packaging
Tools extension to be activated for this project even if the Windows App SDK Nuget
package has not yet been restored.
-->
<ItemGroup Condition="'$(DisableMsixProjectCapabilityAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<ProjectCapability Include="Msix" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CoderSdk\CoderSdk.csproj" />
<ProjectReference Include="..\Vpn.Proto\Vpn.Proto.csproj" />
<ProjectReference Include="..\Vpn\Vpn.csproj" />
</ItemGroup>

<!--
Defining the "HasPackageAndPublishMenuAddedByProject" property here allows the Solution
Explorer "Package and Publish" context menu entry to be enabled for this project even if
the Windows App SDK Nuget package has not yet been restored.
-->
<PropertyGroup Condition="'$(DisableHasPackageAndPublishMenuAddedByProject)'!='true' and '$(EnableMsixTooling)'=='true'">
<HasPackageAndPublishMenu>true</HasPackageAndPublishMenu>
</PropertyGroup>

<!-- Publish Properties -->
<PropertyGroup>
<!--
This does not work in CI at the moment, so we need to set it to false
Error: C:\Program Files\dotnet\sdk\9.0.102\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(400,5): error NETSDK1094: Unable to optimize assemblies for performance: a valid runtime package was not found. Either set the PublishReadyToRun property to false, or use a supported runtime identifier when publishing. When targeting .NET 6 or higher, make sure to restore packages with the PublishReadyToRun property set to true. [D:\a\coder-desktop-windows\coder-desktop-windows\App\App.csproj]
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
-->
<PublishReadyToRun>False</PublishReadyToRun>
<PublishTrimmed Condition="'$(Configuration)' == 'Debug'">False</PublishTrimmed>
<PublishTrimmed Condition="'$(Configuration)' != 'Debug'">True</PublishTrimmed>
</PropertyGroup>
</Project>
9 changes: 2 additions & 7 deletions App/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -12,7 +12,6 @@ namespace Coder.Desktop.App;
public partial class App : Application
{
private readonly IServiceProvider _services;
private readonly bool _handleClosedEvents = true;

public App()
{
@@ -49,12 +48,8 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
var trayWindow = _services.GetRequiredService<TrayWindow>();
trayWindow.Closed += (sender, args) =>
{
// TODO: wire up HandleClosedEvents properly
if (_handleClosedEvents)
{
args.Handled = true;
trayWindow.AppWindow.Hide();
}
args.Handled = true;
trayWindow.AppWindow.Hide();
};
}
}
2 changes: 2 additions & 0 deletions App/Converters/VpnLifecycleToBoolConverter.cs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@

namespace Coder.Desktop.App.Converters;

[DependencyProperty<bool>("Unknown", DefaultValue = false)]
[DependencyProperty<bool>("Starting", DefaultValue = false)]
[DependencyProperty<bool>("Started", DefaultValue = false)]
[DependencyProperty<bool>("Stopping", DefaultValue = false)]
@@ -18,6 +19,7 @@ public object Convert(object value, Type targetType, object parameter, string la

return lifecycle switch
{
VpnLifecycle.Unknown => Unknown,
VpnLifecycle.Starting => Starting,
VpnLifecycle.Started => Started,
VpnLifecycle.Stopping => Stopping,
12 changes: 9 additions & 3 deletions App/Models/RpcModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Coder.Desktop.Vpn.Proto;

namespace Coder.Desktop.App.Models;

@@ -11,6 +13,7 @@ public enum RpcLifecycle

public enum VpnLifecycle
{
Unknown,
Stopped,
Starting,
Started,
@@ -21,17 +24,20 @@ public class RpcModel
{
public RpcLifecycle RpcLifecycle { get; set; } = RpcLifecycle.Disconnected;

public VpnLifecycle VpnLifecycle { get; set; } = VpnLifecycle.Stopped;
public VpnLifecycle VpnLifecycle { get; set; } = VpnLifecycle.Unknown;

public List<object> Agents { get; set; } = [];
public List<Workspace> Workspaces { get; set; } = [];

public List<Agent> Agents { get; set; } = [];

public RpcModel Clone()
{
return new RpcModel
{
RpcLifecycle = RpcLifecycle,
VpnLifecycle = VpnLifecycle,
Agents = Agents,
Workspaces = Workspaces.ToList(),
Agents = Agents.ToList(),
};
}
}
3 changes: 0 additions & 3 deletions App/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"profiles": {
"App (Package)": {
"commandName": "MsixPackage"
},
"App (Unpackaged)": {
"commandName": "Project"
}
6 changes: 4 additions & 2 deletions App/Services/CredentialManager.cs
Original file line number Diff line number Diff line change
@@ -66,12 +66,14 @@ public async Task SetCredentials(string coderUrl, string apiToken, CancellationT

try
{
var cts = CancellationTokenSource.CreateLinkedTokenSource(ct);
cts.CancelAfter(TimeSpan.FromSeconds(15));
var sdkClient = new CoderApiClient(uri);
sdkClient.SetSessionToken(apiToken);
// TODO: we should probably perform a version check here too,
// rather than letting the service do it on Start
_ = await sdkClient.GetBuildInfo(ct);
_ = await sdkClient.GetUser(User.Me, ct);
_ = await sdkClient.GetBuildInfo(cts.Token);
_ = await sdkClient.GetUser(User.Me, cts.Token);
}
catch (Exception e)
{
58 changes: 53 additions & 5 deletions App/Services/RpcController.cs
Original file line number Diff line number Diff line change
@@ -96,6 +96,7 @@ public async Task Reconnect(CancellationToken ct = default)
{
state.RpcLifecycle = RpcLifecycle.Connecting;
state.VpnLifecycle = VpnLifecycle.Stopped;
state.Workspaces.Clear();
state.Agents.Clear();
});

@@ -125,7 +126,8 @@ public async Task Reconnect(CancellationToken ct = default)
MutateState(state =>
{
state.RpcLifecycle = RpcLifecycle.Disconnected;
state.VpnLifecycle = VpnLifecycle.Stopped;
state.VpnLifecycle = VpnLifecycle.Unknown;
state.Workspaces.Clear();
state.Agents.Clear();
});
throw new RpcOperationException("Failed to reconnect to the RPC server", e);
@@ -134,10 +136,18 @@ public async Task Reconnect(CancellationToken ct = default)
MutateState(state =>
{
state.RpcLifecycle = RpcLifecycle.Connected;
// TODO: fetch current state
state.VpnLifecycle = VpnLifecycle.Stopped;
state.VpnLifecycle = VpnLifecycle.Unknown;
state.Workspaces.Clear();
state.Agents.Clear();
});

var statusReply = await _speaker.SendRequestAwaitReply(new ClientMessage
{
Status = new StatusRequest(),
}, ct);
if (statusReply.MsgCase != ServiceMessage.MsgOneofCase.Status)
throw new InvalidOperationException($"Unexpected reply message type: {statusReply.MsgCase}");
ApplyStatusUpdate(statusReply.Status);
}

public async Task StartVpn(CancellationToken ct = default)
@@ -234,9 +244,40 @@ private async Task<IDisposable> AcquireOperationLockNowAsync()
return locker;
}

private void ApplyStatusUpdate(Status status)
{
MutateState(state =>
{
state.VpnLifecycle = status.Lifecycle switch
{
Status.Types.Lifecycle.Unknown => VpnLifecycle.Unknown,
Status.Types.Lifecycle.Starting => VpnLifecycle.Starting,
Status.Types.Lifecycle.Started => VpnLifecycle.Started,
Status.Types.Lifecycle.Stopping => VpnLifecycle.Stopping,
Status.Types.Lifecycle.Stopped => VpnLifecycle.Stopped,
_ => VpnLifecycle.Stopped,
};
state.Workspaces.Clear();
state.Workspaces.AddRange(status.PeerUpdate.UpsertedWorkspaces);
state.Agents.Clear();
state.Agents.AddRange(status.PeerUpdate.UpsertedAgents);
});
}

private void SpeakerOnReceive(ReplyableRpcMessage<ClientMessage, ServiceMessage> message)
{
// TODO: this
switch (message.Message.MsgCase)
{
case ServiceMessage.MsgOneofCase.Status:
ApplyStatusUpdate(message.Message.Status);
break;
case ServiceMessage.MsgOneofCase.Start:
case ServiceMessage.MsgOneofCase.Stop:
case ServiceMessage.MsgOneofCase.None:
default:
// TODO: log unexpected message
break;
}
}

private async Task DisposeSpeaker()
@@ -251,7 +292,14 @@ private async Task DisposeSpeaker()
private void SpeakerOnError(Exception e)
{
Debug.WriteLine($"Error: {e}");
Reconnect(CancellationToken.None).Wait();
try
{
Reconnect(CancellationToken.None).Wait();
}
catch
{
// best effort to immediately reconnect
}
}

private void AssertRpcConnected()
Loading