Skip to content

Commit

Permalink
Fixes scheduler to be more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed May 12, 2020
1 parent 9f66429 commit f0b9c46
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
3 changes: 2 additions & 1 deletion exampleapps/apps/test1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ public override Task InitializeAsync()

var rand = new Random();
var randNumber = rand.Next();
RunEvery(TimeSpan.FromSeconds(5)).Subscribe(s => Log("Hello world {rand}", randNumber));
RunEvery(TimeSpan.FromSeconds(5), () => Log("Hello world {rand}", randNumber));


RunEvery(TimeSpan.FromSeconds(15), () => throw new Exception("Ohh noo man!"));

// EventChanges
// .Subscribe(f =>
Expand Down
70 changes: 64 additions & 6 deletions src/App/NetDaemon.App/Common/Reactive/AppDaemonRxApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,85 @@ public RxEntity Entity(string entityId)
}

/// <inheritdoc/>
public IObservable<long> RunDaily(string time)
public IDisposable RunDaily(string time, Action action)
{
DateTime timeOfDayToTrigger;

if (!DateTime.TryParseExact(time, "HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out timeOfDayToTrigger))
{
throw new FormatException($"{time} is not a valid time for the current locale");
}
return Observable.Timer(timeOfDayToTrigger, TimeSpan.FromDays(1), TaskPoolScheduler.Default).TakeWhile(x => this.IsEnabled);

return Observable.Timer(
timeOfDayToTrigger,
TimeSpan.FromDays(1),
TaskPoolScheduler.Default)
.TakeWhile(x => this.IsEnabled)
.Subscribe(
s =>
{
try
{
action();
}
catch (Exception e)
{
LogError(e, "Error, RunDaily APP: {app}", Id ?? "unknown");
}
},
ex =>
{
LogError(ex, "Error, RunDaily_ex APP: {app}", Id ?? "unknown");
});
}

/// <inheritdoc/>
public IObservable<long> RunEvery(TimeSpan timespan)
public IDisposable RunEvery(TimeSpan timespan, Action action)
{
return Observable.Interval(timespan, TaskPoolScheduler.Default).TakeWhile(x => this.IsEnabled);
return Observable.Interval(timespan, TaskPoolScheduler.Default)
.TakeWhile(x => this.IsEnabled)
.Subscribe(
s =>
{
try
{
action();
}
catch (Exception e)
{
LogError(e, "Error, RunEvery APP: {app}", Id ?? "unknown");
}
},
ex =>
{
LogError(ex, "Error, RunEvery_ex APP: {app}", Id ?? "unknown");
});
}

/// <inheritdoc/>
public IObservable<long> RunIn(TimeSpan timespan)
public IDisposable RunIn(TimeSpan timespan, Action action)
{
return Observable.Timer(timespan, TaskPoolScheduler.Default).TakeWhile(x => this.IsEnabled);
return Observable.Timer(timespan, TaskPoolScheduler.Default)
.TakeWhile(x => this.IsEnabled)
.Subscribe(
s =>
{
try
{
action();
}
catch (Exception e)
{
LogError(e, "Error, RunIn APP: {app}", Id ?? "unknown");
}
},
ex =>
{
LogError(ex, "Error, RunIn_ex APP: {app}", Id ?? "unknown");
});
}

/// <inheritdoc/>
Expand Down

0 comments on commit f0b9c46

Please sign in to comment.