Skip to content

Commit

Permalink
Merge pull request #2367 from abarz722/develop
Browse files Browse the repository at this point in the history
Rework schedulers
  • Loading branch information
tpurschke committed Mar 26, 2024
2 parents 80da5e3 + 745d4da commit 2e98871
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 81 deletions.
2 changes: 1 addition & 1 deletion roles/database/files/sql/idempotent/fworch-texts.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4449,7 +4449,7 @@ INSERT INTO txt VALUES ('H5620', 'German', 'Gemeinsame Netzwerkareas: Vom Admin
');
INSERT INTO txt VALUES ('H5620', 'English', 'Common Network Areas: Network areas defined by the administrator, which are permitted to be used by all connections.
They are visible in the object library and are not offered in the list of available areas for Common Services.
The flags "in Source" and "in Destination" determine, where the Common Network Area are allowed to be used.
The flags "in Source" and "in Destination" determine, where the Common Network Areas are allowed to be used.
');
INSERT INTO txt VALUES ('H5621', 'German', 'Ein Modellierer kann einige persönliche Voreinstellungen für die Darstellung der Modellierung überschreiben.
Ausgangswert ist der vom Admin in den <a href="/help/settings/modelling">Modellierungseinstellungen</a> gesetzte Wert.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
subscription subscribeImportAppDataConfigChanges {
config (where: { _or: [{config_key: {_eq: "importAppDataSleepTime"}}, {config_key: {_eq: "importAppDataStartAt"}} , {config_key: {_eq: "importAppDataPath"}}]}, limit: 2){
config (where: { _or: [{config_key: {_eq: "importAppDataSleepTime"}}, {config_key: {_eq: "importAppDataStartAt"}} , {config_key: {_eq: "importAppDataPath"}}]}, limit: 3){
config_key
config_value
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
subscription subscribeImportNotifyConfigChanges {
config (where: { _or: [{config_key: {_eq: "impChangeNotifySleepTime"}}, {config_key: {_eq: "impChangeNotifyStartAt"}}, {config_key: {_eq: "impChangeNotifyActive"}} ]}, limit: 3){
config_key
config_value
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
subscription subscribeImportSubnetDataConfigChanges {
config (where: { _or: [{config_key: {_eq: "importSubnetDataSleepTime"}}, {config_key: {_eq: "importSubnetDataStartAt"}}, {config_key: {_eq: "importSubnetDataPath"}} ]}, limit: 2){
config (where: { _or: [{config_key: {_eq: "importSubnetDataSleepTime"}}, {config_key: {_eq: "importSubnetDataStartAt"}}, {config_key: {_eq: "importSubnetDataPath"}} ]}, limit: 3){
config_key
config_value
}
Expand Down
9 changes: 8 additions & 1 deletion roles/lib/files/FWO.Api.Client/Queries/ConfigQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class ConfigQueries : Queries
public static readonly string getConfigItemByKey;
public static readonly string subscribeAutodiscoveryConfigChanges;
public static readonly string subscribeDailyCheckConfigChanges;
public static readonly string subscribeImportAppDataConfigChanges;
public static readonly string subscribeImportSubnetDataConfigChanges;
public static readonly string subscribeImportNotifyConfigChanges;


static ConfigQueries()
{
Expand All @@ -29,14 +33,17 @@ static ConfigQueries()
getCustomTextsPerLanguage = File.ReadAllText(QueryPath + "config/getCustomTextsPerLanguage.graphql");
upsertCustomText = File.ReadAllText(QueryPath + "config/upsertCustomText.graphql");
deleteCustomText = File.ReadAllText(QueryPath + "config/deleteCustomText.graphql");
getConfigSubscription = File.ReadAllText(QueryPath + "config/getConfigSubscription.graphql");
addConfigItem = File.ReadAllText(QueryPath + "config/addConfigItem.graphql");
updateConfigItem = File.ReadAllText(QueryPath + "config/updateConfigItem.graphql");
getConfigItemsByUser = File.ReadAllText(QueryPath + "config/getConfigItemsByUser.graphql");
getConfigItemByKey = File.ReadAllText(QueryPath + "config/getConfigItemByKey.graphql");
upsertConfigItem = File.ReadAllText(QueryPath + "config/upsertConfigItem.graphql");
subscribeAutodiscoveryConfigChanges = File.ReadAllText(QueryPath + "config/subscribeAutodiscoveryConfigChanges.graphql");
getConfigSubscription = File.ReadAllText(QueryPath + "config/getConfigSubscription.graphql");
subscribeDailyCheckConfigChanges = File.ReadAllText(QueryPath + "config/subscribeDailyCheckConfigChanges.graphql");
subscribeImportAppDataConfigChanges = File.ReadAllText(QueryPath + "config/subscribeImportAppDataConfigChanges.graphql");
subscribeImportSubnetDataConfigChanges = File.ReadAllText(QueryPath + "config/subscribeImportSubnetDataConfigChanges.graphql");
subscribeImportNotifyConfigChanges = File.ReadAllText(QueryPath + "config/subscribeImportNotifyConfigChanges.graphql");
}
catch (Exception exception)
{
Expand Down
16 changes: 13 additions & 3 deletions roles/lib/files/FWO.Config.Api/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,30 @@ public async Task SetUserId(ApiConnection apiConnection, int userId, bool waitFo
}

protected void SubscriptionUpdateHandler(ConfigItem[] configItems)
{
HandleUpdate(configItems, false);
}

public void SubscriptionPartialUpdateHandler(ConfigItem[] configItems)
{
HandleUpdate(configItems, true);
}

protected void HandleUpdate(ConfigItem[] configItems, bool partialUpdate)
{
semaphoreSlim.Wait();
try
{
Log.WriteDebug("Config subscription update", "New config values received from config subscription");
RawConfigItems = configItems;
Update(configItems);
Update(configItems, partialUpdate);
OnChange?.Invoke(this, configItems);
Initialized = true;
}
finally { semaphoreSlim.Release(); }
}

protected void Update(ConfigItem[] configItems)
protected void Update(ConfigItem[] configItems, bool partialUpdate = false)
{
foreach (PropertyInfo property in GetType().GetProperties())
{
Expand All @@ -86,7 +96,7 @@ protected void Update(ConfigItem[] configItems)
Log.WriteError("Load Config Items", $"Config item with key \"{key}\" could not be loaded. Using default value.", exception);
}
}
else
else if (!partialUpdate)
{
// If this is a global config
if (UserId == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ public static async Task<AutoDiscoverScheduler> CreateAsync(ApiConnection apiCon
return new AutoDiscoverScheduler(apiConnection, globalConfig);
}

private AutoDiscoverScheduler(ApiConnection apiConnection, GlobalConfig globalConfig) : base(apiConnection, globalConfig)
private AutoDiscoverScheduler(ApiConnection apiConnection, GlobalConfig globalConfig)
: base(apiConnection, globalConfig, ConfigQueries.subscribeAutodiscoveryConfigChanges)
{}

/// <summary>
/// set scheduling timer from config values
/// </summary>
protected override void GlobalConfig_OnChange(Config.Api.Config globalConfig, ConfigItem[] _)
protected override void OnGlobalConfigChange(List<ConfigItem> config)
{
AutoDiscoverTimer.Interval = globalConfig.AutoDiscoverSleepTime * GlobalConst.kHoursToMilliseconds;
ScheduleTimer.Stop();
globalConfig.SubscriptionPartialUpdateHandler(config.ToArray());
AutoDiscoverTimer.Interval = globalConfig.AutoDiscoverSleepTime * GlobalConst.kHoursToMilliseconds;
StartScheduleTimer();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public static async Task<DailyCheckScheduler> CreateAsync(ApiConnection apiConne
return new DailyCheckScheduler(apiConnection, config);
}

private DailyCheckScheduler(ApiConnection apiConnection, GlobalConfig globalConfig) : base(apiConnection, globalConfig)
private DailyCheckScheduler(ApiConnection apiConnection, GlobalConfig globalConfig)
: base(apiConnection, globalConfig, ConfigQueries.subscribeDailyCheckConfigChanges)
{
if(globalConfig.RecRefreshStartup)
{
Expand All @@ -41,10 +42,11 @@ private DailyCheckScheduler(ApiConnection apiConnection, GlobalConfig globalConf
/// <summary>
/// set scheduling timer from fixed value
/// </summary>
protected override void GlobalConfig_OnChange(Config.Api.Config globalConfig, ConfigItem[] _)
protected override void OnGlobalConfigChange(List<ConfigItem> config)
{
DailyCheckTimer.Interval = DailyCheckSleepTime;
DailyCheckScheduleTimer.Stop();
globalConfig.SubscriptionPartialUpdateHandler(config.ToArray());
DailyCheckTimer.Interval = DailyCheckSleepTime;
StartScheduleTimer();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FWO.Api.Client;
using FWO.Api.Client.Queries;
using FWO.Api.Data;
using FWO.Config.Api;
using FWO.Config.Api.Data;
Expand All @@ -24,15 +25,17 @@ public static async Task<ImportAppDataScheduler> CreateAsync(ApiConnection apiCo
return new ImportAppDataScheduler(apiConnection, globalConfig);
}

private ImportAppDataScheduler(ApiConnection apiConnection, GlobalConfig globalConfig) : base(apiConnection, globalConfig)
private ImportAppDataScheduler(ApiConnection apiConnection, GlobalConfig globalConfig)
: base(apiConnection, globalConfig, ConfigQueries.subscribeImportAppDataConfigChanges)
{}

/// <summary>
/// set scheduling timer from config values
/// </summary>
protected override void GlobalConfig_OnChange(Config.Api.Config globalConfig, ConfigItem[] _)
protected override void OnGlobalConfigChange(List<ConfigItem> config)
{
ScheduleTimer.Stop();
globalConfig.SubscriptionPartialUpdateHandler(config.ToArray());
if(globalConfig.ImportAppDataSleepTime > 0)
{
ImportAppDataTimer.Interval = globalConfig.ImportAppDataSleepTime * GlobalConst.kHoursToMilliseconds;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FWO.Api.Client;
using FWO.Api.Client.Queries;
using FWO.Api.Data;
using FWO.Config.Api;
using FWO.Config.Api.Data;
Expand All @@ -24,16 +25,18 @@ public static async Task<ImportChangeNotifyScheduler> CreateAsync(ApiConnection
return new ImportChangeNotifyScheduler(apiConnection, globalConfig);
}

private ImportChangeNotifyScheduler(ApiConnection apiConnection, GlobalConfig globalConfig) : base(apiConnection, globalConfig)
private ImportChangeNotifyScheduler(ApiConnection apiConnection, GlobalConfig globalConfig)
: base(apiConnection, globalConfig, ConfigQueries.subscribeImportNotifyConfigChanges)
{}

/// <summary>
/// set scheduling timer from config values
/// </summary>
protected override void GlobalConfig_OnChange(Config.Api.Config globalConfig, ConfigItem[] _)
protected override void OnGlobalConfigChange(List<ConfigItem> config)
{
ScheduleTimer.Stop();
if(globalConfig.ImpChangeNotifySleepTime > 0)
globalConfig.SubscriptionPartialUpdateHandler(config.ToArray());
if(globalConfig.ImpChangeNotifyActive && globalConfig.ImpChangeNotifySleepTime > 0)
{
ImportChangeNotifyTimer.Interval = globalConfig.ImpChangeNotifySleepTime * 1000; // convert seconds to milliseconds
StartScheduleTimer();
Expand All @@ -45,7 +48,7 @@ protected override void GlobalConfig_OnChange(Config.Api.Config globalConfig, Co
/// </summary>
protected override void StartScheduleTimer()
{
if (globalConfig.ImpChangeNotifySleepTime > 0)
if (globalConfig.ImpChangeNotifyActive && globalConfig.ImpChangeNotifySleepTime > 0)
{
DateTime startTime = DateTime.Now;
try
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FWO.Api.Client;
using FWO.Api.Client.Queries;
using FWO.Api.Data;
using FWO.Config.Api;
using FWO.Config.Api.Data;
Expand All @@ -24,15 +25,17 @@ public static async Task<ImportSubnetDataScheduler> CreateAsync(ApiConnection ap
return new ImportSubnetDataScheduler(apiConnection, globalConfig);
}

private ImportSubnetDataScheduler(ApiConnection apiConnection, GlobalConfig globalConfig) : base(apiConnection, globalConfig)
private ImportSubnetDataScheduler(ApiConnection apiConnection, GlobalConfig globalConfig)
: base(apiConnection, globalConfig, ConfigQueries.subscribeImportSubnetDataConfigChanges)
{}

/// <summary>
/// set scheduling timer from config values
/// </summary>
protected override void GlobalConfig_OnChange(Config.Api.Config globalConfig, ConfigItem[] _)
protected override void OnGlobalConfigChange(List<ConfigItem> config)
{
ScheduleTimer.Stop();
globalConfig.SubscriptionPartialUpdateHandler(config.ToArray());
if (globalConfig.ImportSubnetDataSleepTime > 0)
{
ImportSubnetDataTimer.Interval = globalConfig.ImportSubnetDataSleepTime * GlobalConst.kHoursToMilliseconds;
Expand Down
21 changes: 17 additions & 4 deletions roles/middleware/files/FWO.Middleware.Server/SchedulerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,43 @@ public abstract class SchedulerBase
/// </summary>
protected GlobalConfig globalConfig;

/// <summary>
/// Global config change subscription
/// </summary>
protected GraphQlApiSubscription<List<ConfigItem>>? ConfigDataSubscription;

private List<Alert> openAlerts = new();


/// <summary>
/// Constructor starting the Schedule timer
/// </summary>
protected SchedulerBase(ApiConnection apiConnection, GlobalConfig globalConfig)
protected SchedulerBase(ApiConnection apiConnection, GlobalConfig globalConfig, string configDataSubscription)
{
this.apiConnection = apiConnection;
this.globalConfig = globalConfig;
globalConfig.OnChange += GlobalConfig_OnChange;
StartScheduleTimer();
ConfigDataSubscription = apiConnection.GetSubscription<List<ConfigItem>>(ApiExceptionHandler, OnGlobalConfigChange, configDataSubscription);
}

/// <summary>
/// set scheduling timer from config values, to be overwritten for specific scheduler
/// </summary>
protected abstract void GlobalConfig_OnChange(Config.Api.Config globalConfig, ConfigItem[] _);
protected abstract void OnGlobalConfigChange(List<ConfigItem> _);

/// <summary>
/// start the scheduling timer, to be overwritten for specific scheduler
/// </summary>
protected abstract void StartScheduleTimer();

/// <summary>
/// subscription exception handling
/// </summary>
protected static void ApiExceptionHandler(Exception exception)
{
Log.WriteError("Import App Data Config", "Api subscription lead to exception. Retry subscription.", exception);
// Subscription will be restored if no exception is thrown here
}

/// <summary>
/// set an alert in error case with
/// </summary>
Expand Down
Loading

0 comments on commit 2e98871

Please sign in to comment.