Skip to content

Commit

Permalink
[CastIt.Server] Crash fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Sep 19, 2021
1 parent 691e58f commit 4c4c2ba
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 52 deletions.
26 changes: 18 additions & 8 deletions CastIt.Server.Windows.TrayIcon/TrayIcon.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CastIt.Application.Common.Utils;
using CastIt.Application.Server;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.IO;
Expand All @@ -22,16 +23,25 @@ public class TrayIcon

public TrayIcon()
{
_serviceController = Array.Find(ServiceController.GetServices(), s => s.ServiceName == WebServerUtils.ServerProcessName);
if (_serviceController == null)
try
{
MessageBox.Show("CastIt.Server is not installed");
Exit(null, EventArgs.Empty);
return;
_serviceController = Array.Find(ServiceController.GetServices(), s => s.ServiceName == WebServerUtils.ServerProcessName);
if (_serviceController == null)
{
MessageBox.Show("CastIt.Server is not installed");
Exit(null, EventArgs.Empty);
return;
}
CreateTrayIcon();
CheckShowServiceNotElevatedWarning();
}
catch (Exception e)
{
string ex = JsonConvert.SerializeObject(e);
string logPath = AppFileUtils.GetServerLogsPath();
string filePath = Path.Combine(logPath, $"tray_icon_crash_{DateTime.Now}.txt");
File.WriteAllText(filePath, ex);
}

CreateTrayIcon();
CheckShowServiceNotElevatedWarning();
}

private bool CheckShowServiceNotElevatedWarning()
Expand Down
30 changes: 15 additions & 15 deletions CastIt.Server/Services/AppDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,28 +157,28 @@ public async Task SavePlayListChanges(List<ServerPlayList> playLists)
if (playLists.Count == 0)
return;

var tasks = playLists.Select(playList => _db.Update<PlayList>(playList.Id)
.Set(p => p.Position, playList.Position)
.Set(p => p.Shuffle, playList.Shuffle)
.Set(p => p.Loop, playList.Loop)
.ExecuteAffrowsAsync()
);

await Task.WhenAll(tasks);
foreach (var playList in playLists)
{
await _db.Update<PlayList>(playList.Id)
.Set(p => p.Position, playList.Position)
.Set(p => p.Shuffle, playList.Shuffle)
.Set(p => p.Loop, playList.Loop)
.ExecuteAffrowsAsync();
}
}

public async Task SaveFileChanges(List<ServerFileItem> files)
{
if (files.Count == 0)
return;

var tasks = files.Select(file => _db.Update<FileItem>(file.Id)
.Set(f => f.PlayedPercentage, file.PlayedPercentage)
.Set(f => f.Position, file.Position)
.ExecuteAffrowsAsync()
);

await Task.WhenAll(tasks);
foreach (var file in files)
{
await _db.Update<FileItem>(file.Id)
.Set(f => f.PlayedPercentage, file.PlayedPercentage)
.Set(f => f.Position, file.Position)
.ExecuteAffrowsAsync();
}
}

private void ApplyMigrations()
Expand Down
75 changes: 48 additions & 27 deletions CastIt.Server/Services/CastItHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,36 @@ private void SetCastItEventHandlers()

public override async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"{nameof(StartAsync)}: Deleting server logs and previews...");
_fileService.DeleteServerLogsAndPreviews();
_serverService.Init();
try
{
_logger.LogInformation($"{nameof(StartAsync)}: Deleting server logs and previews...");
_fileService.DeleteServerLogsAndPreviews();

_logger.LogInformation($"{nameof(StartAsync)}: Initializing castit service...");
await _castService.Init();
_logger.LogInformation($"{nameof(StartAsync)}: Initializing server...");
_serverService.Init();

_logger.LogInformation($"{nameof(StartAsync)}: Initializing app settings...");
await _appSettings.Init();
_logger.LogInformation($"{nameof(StartAsync)}: Initializing castit service...");
await _castService.Init();

_logger.LogInformation($"{nameof(StartAsync)}: Initializing ffmpeg...");
await _fFmpegService.Init(_appSettings.FFmpegExePath, _appSettings.FFprobeExePath);
_logger.LogInformation($"{nameof(StartAsync)}: Initializing app settings...");
await _appSettings.Init();

_logger.LogInformation($"{nameof(StartAsync)}: Initializing file watchers...");
InitializeOrUpdateFileWatcher(false);
_logger.LogInformation($"{nameof(StartAsync)}: Initializing ffmpeg...");
await _fFmpegService.Init(_appSettings.FFmpegExePath, _appSettings.FFprobeExePath);

_logger.LogInformation($"{nameof(StartAsync)}: Initialization completed");
await base.StartAsync(cancellationToken);
_logger.LogInformation($"{nameof(StartAsync)}: Initializing file watchers...");
InitializeOrUpdateFileWatcher(false);
}
catch (Exception e)
{
_logger.LogError(e, $"{nameof(StartAsync)}: Unknown error while starting the service");
throw;
}
finally
{
_logger.LogInformation($"{nameof(StartAsync)}: Initialization completed");
await base.StartAsync(cancellationToken);
}
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
Expand All @@ -128,24 +140,33 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

public override async Task StopAsync(CancellationToken cancellationToken)
{
//TODO: THIS MSG IS NOT GETTING DELIVERED
_logger.LogInformation($"{nameof(StopAsync)}: Hosted service is going down!");
await _castItHub.Clients.All.ServerMessage(AppMessageType.ServerIsClosing);

_logger.LogInformation($"{nameof(StopAsync)}: Cancelling any pending duration job...");
_setDurationTokenSource.Cancel();
try
{
//TODO: THIS MSG IS NOT GETTING DELIVERED
_logger.LogInformation($"{nameof(StopAsync)}: Hosted service is going down!");
await _castItHub.Clients.All.ServerMessage(AppMessageType.ServerIsClosing);

_logger.LogInformation($"{nameof(StopAsync)}: Stop listening to folders...");
_fileWatcherService.StopListening();
_logger.LogInformation($"{nameof(StopAsync)}: Cancelling any pending duration job...");
_setDurationTokenSource.Cancel();

_logger.LogInformation($"{nameof(StopAsync)}: Cleaning the cast service...");
await _castService.StopAsync();
_logger.LogInformation($"{nameof(StopAsync)}: Stop listening to folders...");
_fileWatcherService.StopListening();

_logger.LogInformation($"{nameof(StopAsync)}: Saving current settings...");
await _appSettings.SaveCurrentSettings();
_logger.LogInformation($"{nameof(StopAsync)}: Cleaning the cast service...");
await _castService.StopAsync();

_logger.LogInformation($"{nameof(StopAsync)}: Stop completed");
await base.StopAsync(cancellationToken);
_logger.LogInformation($"{nameof(StopAsync)}: Saving current settings...");
await _appSettings.SaveCurrentSettings();
}
catch (Exception e)
{
_logger.LogError(e, $"{nameof(StopAsync)}: Unknown error while stopping the service");
}
finally
{
_logger.LogInformation($"{nameof(StopAsync)}: Stop completed");
await base.StopAsync(cancellationToken);
}
}

private void InitializeOrUpdateFileWatcher(bool update)
Expand Down
4 changes: 3 additions & 1 deletion CastIt.Server/Services/ServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public void Init()
//WebServerUtils.GetWebServerIpAddress()
//Keep in mind that to test this thing on IIS you need to add to the configuration file (.vs\CastIt\config)
//<binding protocol="http" bindingInformation="*:62003:your.local.ipaddress" />
var ipAddress = serverAddressesFeature.Addresses.FirstOrDefault(s =>

_logger.LogInformation($"The following addresses = {string.Join(",", serverAddressesFeature.Addresses)} will be checked");
string ipAddress = serverAddressesFeature.Addresses.FirstOrDefault(s =>
!s.Contains("*", StringComparison.OrdinalIgnoreCase) &&
!s.Contains("localhost", StringComparison.OrdinalIgnoreCase) &&
!s.Contains("[::]")) ?? WebServerUtils.GetWebServerIpAddress();
Expand Down
2 changes: 1 addition & 1 deletion CastIt.Server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void ConfigureServices(IServiceCollection services)

//Cors is required for the subtitles to work
services.AddCors();

//Should be more than enough for the hosted service to complete
services.Configure<HostOptions>(opts => opts.ShutdownTimeout = TimeSpan.FromSeconds(15));

Expand Down

0 comments on commit 4c4c2ba

Please sign in to comment.