Skip to content

Commit

Permalink
feat: pause watcher execution
Browse files Browse the repository at this point in the history
  • Loading branch information
jdaar committed May 8, 2023
1 parent 739d1f9 commit f8417e5
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 38 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ jobs:
dotnet-version: ${{ matrix.dotnet-version }}
- name: Install dependencies
run: dotnet restore
- name: Install tool dependencies
run: dotnet tool restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
Expand Down
16 changes: 10 additions & 6 deletions ConnectionInterface/ConnectionInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@

namespace ConnectionInterface
{
public enum t_PipeCommand
public enum t_PipeCommand : int
{
GET_WATCHER = 0,
GET_ALL_WATCHER = 1,
CREATE_WATCHER = 2,
UPDATE_WATCHER = 3,
DELETE_WATCHER = 4,
GET_SERVICESETTING = 5,
GET_ALL_SERVICESETTING = 6,
CREATE_SERVICESETTING = 7,
UPDATE_SERVICESETTING = 8,
DELETE_SERVICESETTING = 9,
PAUSE_WATCHER = 5,
START_WATCHER = 6,
GET_SERVICESETTING = 7,
GET_ALL_SERVICESETTING = 8,
CREATE_SERVICESETTING = 9,
UPDATE_SERVICESETTING = 10,
DELETE_SERVICESETTING = 11,
GET_WATCHER_STATUS = 12,
}
public enum t_ResponseStatus
{
Expand All @@ -39,6 +42,7 @@ public class PipeResponsePayload
{
public string? ErrorMessage { get; set; }
public List<Watcher>? Watchers { get; set; }
public bool? WatcherStatus { get; set; }
}

public class PipeResponse
Expand Down
8 changes: 8 additions & 0 deletions ConnectionInterface/Watcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ public class Watcher
[Column(name: "created_at")]
public DateTime CreatedAt { get; set; }

[Required]
[Column(name: "executed_at")]
public DateTime ExecutedAt { get; set; }

[Required]
[Column(name: "modified_files")]
public int ModifiedFiles { get; set; }


[Required]
[StringLength(256)]
Expand Down
Binary file added fsBuddyClient/Assets/Icons/power.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions fsBuddyClient/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<None Remove="Assets\Icons\edit.png" />
<None Remove="Assets\Icons\logs.png" />
<None Remove="Assets\Icons\pause.png" />
<None Remove="Assets\Icons\power.png" />
<None Remove="Assets\Icons\shrink.png" />
<None Remove="Assets\logo_black.jpg" />
<None Remove="Assets\logo_black.svg" />
Expand All @@ -37,6 +38,7 @@
<Resource Include="Assets\Icons\edit.png" />
<Resource Include="Assets\Icons\logs.png" />
<Resource Include="Assets\Icons\pause.png" />
<Resource Include="Assets\Icons\power.png" />
<Resource Include="Assets\Icons\shrink.png" />
<Resource Include="Assets\logo_black.jpg" />
<Resource Include="Assets\logo_black.svg" />
Expand Down
4 changes: 2 additions & 2 deletions fsBuddyClient/View/Components/Navbar.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
</Button.Template>
</Button>

<Button Grid.Column="3" Command="{Binding RefreshCommand}">
<Button Grid.Column="3" Command="{Binding SwitchConnectionStatusCommand}">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border Style="{StaticResource ButtonWrapper}" Grid.Column="3" >
Expand All @@ -200,7 +200,7 @@
<Image HorizontalAlignment="Center" Grid.Column="0">
<Image.Source>
<BitmapImage
UriSource="pack://application:,,,/Assets/Icons/logs.png"
UriSource="pack://application:,,,/Assets/Icons/power.png"
DecodePixelHeight="100" DecodePixelWidth="90">
</BitmapImage>
</Image.Source>
Expand Down
4 changes: 2 additions & 2 deletions fsBuddyClient/View/Components/WatcherList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@
<TextBlock Style="{StaticResource CardTitle}" Grid.Row="2" Grid.Column="0" Text="Modified files"/>
<TextBlock Style="{StaticResource CardTitle}" Grid.Row="4" Grid.Column="0" Text="Search pattern"/>

<TextBlock Style="{StaticResource CardParagraph}" Grid.Row="0" Grid.Column="2" Text="Placeholder"/>
<TextBlock Style="{StaticResource CardParagraph}" Grid.Row="2" Grid.Column="2" Text="Placeholder" />
<TextBlock Style="{StaticResource CardParagraph}" Grid.Row="0" Grid.Column="2" Text="{Binding ExecutedAt}"/>
<TextBlock Style="{StaticResource CardParagraph}" Grid.Row="2" Grid.Column="2" Text="{Binding ModifiedFiles}" />
<TextBlock Style="{StaticResource CardParagraph}" Grid.Row="4" Grid.Column="2" Text="{Binding SearchPattern}" />

<TextBlock Style="{StaticResource CardTitle}" Grid.Row="0" Grid.Column="4" Text="Creation date"/>
Expand Down
5 changes: 5 additions & 0 deletions fsBuddyClient/ViewModel/Presenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public int WatcherCount {

public void OnPropertyChanged(string propertyName)
{
if (propertyName == nameof(Watchers))
{
// Todo
}

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

Expand Down
15 changes: 15 additions & 0 deletions fsBuddyConfiguration/ManagerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.ComponentModel;
using System.Diagnostics;
using ConnectionInterface;
using System.Runtime.CompilerServices;

namespace Configuration
{
Expand Down Expand Up @@ -81,6 +82,20 @@ public async Task UpdateWatcher(int watcherId, Watcher watcherData)
await _context.SaveChangesAsync(CancellationToken.None);
}

public async Task RegisterExecution(int watcherId)
{
var now = DateTime.UtcNow;
var watcher = await _context.Watchers.SingleOrDefaultAsync(watcher => watcher.Id == watcherId);
if (watcher == null)
{
return;
}
watcher.ExecutedAt = now;
watcher.ModifiedFiles = watcher.ModifiedFiles + 1;

await _context.SaveChangesAsync(CancellationToken.None);
}

public async Task<ServiceSetting?> GetServiceSetting(t_Setting type)
{
return await _context.SystemSettings.Where(setting => setting.Type == type).FirstOrDefaultAsync();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Configuration.Migrations
{
/// <inheritdoc />
public partial class execmodifiedfields : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "executed_at",
table: "watchers",
type: "TEXT",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

migrationBuilder.AddColumn<int>(
name: "modified_files",
table: "watchers",
type: "INTEGER",
nullable: false,
defaultValue: 0);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "executed_at",
table: "watchers");

migrationBuilder.DropColumn(
name: "modified_files",
table: "watchers");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,20 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("TEXT")
.HasColumnName("created_at");
b.Property<DateTime>("ExecutedAt")
.HasColumnType("TEXT")
.HasColumnName("executed_at");
b.Property<string>("InputPath")
.IsRequired()
.HasMaxLength(256)
.HasColumnType("TEXT")
.HasColumnName("input_path");
b.Property<int>("ModifiedFiles")
.HasColumnType("INTEGER")
.HasColumnName("modified_files");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
Expand Down
8 changes: 7 additions & 1 deletion fsBuddyService/FileSystemWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ class FileSystemWatcherDisposable : IDisposable
private Watcher _watcher;

private static Dictionary<t_WatcherAction, Action<Watcher, FileSystemEventArgs>> WatcherActions = new();

private static Func<int, Task>? RegisterExecutionCallback;
private static bool IsWatcherActionsInitialized { get; set; } = false;

public FileSystemWatcher? fsWatcher;


private void PopulateWatcherActions() {
WatcherActions.Add(
t_WatcherAction.MOVE,
delegate (Watcher watcher, FileSystemEventArgs fsEvent)
{
RegisterExecutionCallback?.Invoke(watcher.Id);
Log.Information("From watcher action {@watcher} and event {@fsEvent}", watcher, fsEvent);

if (watcher.OutputPath == null)
Expand Down Expand Up @@ -88,13 +92,15 @@ private void PopulateWatcherActions() {
IsWatcherActionsInitialized = true;
}

public FileSystemWatcherDisposable(Watcher watcher)
public FileSystemWatcherDisposable(Watcher watcher, Func<int, Task> registerExecutionCallback)
{
if (!IsWatcherActionsInitialized)
{
PopulateWatcherActions();
}

RegisterExecutionCallback = registerExecutionCallback;

_watcher = watcher;
using (LogContext.PushProperty("WatcherId", _watcher.Id))
{
Expand Down
43 changes: 43 additions & 0 deletions fsBuddyService/PipeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,49 @@ await _configurationManager.GetWatcher(pipeRequest.Payload.WatcherId ?? -1)
Watchers = watchers
}
};
case t_PipeCommand.PAUSE_WATCHER:
if (pipeRequest?.Payload?.WatcherId == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher id not defined"
}
};
}

_watcherManager.StopFsDisposable(pipeRequest.Payload.WatcherId ?? -1);

Log.Information("Stopping watcher with id {WatcherId}", pipeRequest.Payload.WatcherId);
return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
}
};
case t_PipeCommand.GET_WATCHER_STATUS:
if (pipeRequest?.Payload?.WatcherId == null)
{
return new PipeResponse
{
Status = t_ResponseStatus.FAILURE,
Payload = new PipeResponsePayload {
ErrorMessage = "Watcher id not defined"
}
};
}

var isWatcherRunning = _watcherManager.IsFsDisposableRunning(pipeRequest.Payload.WatcherId ?? -1);

Log.Information("Stopping watcher with id {WatcherId}", pipeRequest.Payload.WatcherId);
return new PipeResponse
{
Status = t_ResponseStatus.SUCCESS,
Payload = new PipeResponsePayload {
WatcherStatus = isWatcherRunning
}
};
default:
return new PipeResponse
{
Expand Down
Loading

0 comments on commit f8417e5

Please sign in to comment.