refactor: Run RemoteQueryManager as an IHostedService

This paradigm more accurately represents the RemoteQueryManager as a
Service that can be started/stopped.
This commit is contained in:
John Colvin committed 2025-08-24 13:46:50 -04:00
1 parent 450ef9985a
commit 3d94eabab7
4 files changed
+61 -29

No files matched your search

@@ -1,16 +1,24 @@
using Bts.DataStream;
using Bts.DataStreamCore;
using Bts.DataStreamExpressions;
using Bts.PubSub;
using bts;
using Microsoft.Extensions.Hosting;
using Spark.Core.Disposables;
using System;
using System.Collections.Generic;
using Bts.DataStreamExpressions;
using BCG.Utilities.General;
using bts;
using Bts.PubSub;
using Bts.DataStreamCore;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace BCG {
// listens to the requests posted on "cloud_streams" request table, and
// if unfulfilled starts a ViewPubsliher
public class RemoteQueryManager : IWorker {
public class RemoteQueryManager : IHostedService {
public class StreamPubUnit {
public string name;
public string stream_expression;
@@ -41,25 +49,46 @@ namespace BCG {
}
private IDictionary<string, StreamPubUnit>
_stream_pub_map = new Dictionary<string, StreamPubUnit>();
private ViewStream _stream = new TableStream("cloud_streams");
readonly IPubTableBroker _broker;
readonly IManagedSubscriber _sub;
readonly object syncRoot = new();
IDisposable? cloudStreamsSub;
IDisposable? remoteRequestsSub;
bool isDisposed;
public RemoteQueryManager(IManagedSubscriber managedSubscriber, IPubTableBroker pubTableBroker) {
_sub = managedSubscriber;
_broker = pubTableBroker;
_stream.onChange += _on_str;
_sub.Subscribe("RemoteRequest", (string tag, byte[] data) => {
RemoteRequest req = RemoteRequest.construct_from_bytes(data);
_spawn_remote_stream(req.published_table_name, req.stream_expression, false);
});
}
private void _on_str(object obj)
{
if (obj is not DataPacket dp) {
return;
public Task StartAsync(CancellationToken cancellationToken) {
lock (syncRoot) {
Log.assert(!isDisposed);
Log.isnull(cloudStreamsSub);
Log.isnull(remoteRequestsSub);
cloudStreamsSub = new TableStream("cloud_streams").AsObservable().Subscribe(_on_str);
remoteRequestsSub = _sub.Observe("RemoteRequest").Subscribe(data => {
RemoteRequest req = RemoteRequest.construct_from_bytes(data);
_spawn_remote_stream(req.published_table_name, req.stream_expression, false);
});
}
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken) {
lock (syncRoot) {
if (!isDisposed) {
isDisposed = true;
Disposable.dispose(ref cloudStreamsSub);
Disposable.dispose(ref remoteRequestsSub);
}
}
return Task.CompletedTask;
}
private void _on_str(DataPacket dp)
{
foreach (IBlueRow r in dp.newRows)
{
string name = r.get<string>("name");
@@ -86,9 +115,5 @@ namespace BCG {
private void _expose_to_ws(string name, ViewStream stream) {
// TODO: john
}
public void doWork() {
}
}
}
@@ -1,11 +1,11 @@
namespace Bts.DataStream.Services;
using BCG;
using BCG.Utilities.General;
using Microsoft.Extensions.DependencyInjection;
public static class DataStreamServices {
public static IServiceCollection AddRemoteQueryManager(this IServiceCollection services) =>
services.AddSingleton<IWorker, RemoteQueryManager>();
services.AddSingleton<RemoteQueryManager>()
.AddHostedService(sp => sp.GetRequiredService<RemoteQueryManager>());
}
@@ -3,5 +3,5 @@ namespace Bts.PubSub;
using System;
public interface IManagedSubscriber {
void Subscribe(string tag, Action<string, byte[]> onData);
IObservable<byte[]> Observe(string tag);
}
@@ -2,12 +2,16 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using BCG.Utilities.General;
using bts;
using Bts.PubSub;
using OnData = System.Action<string, byte[]>;
using OnData = System.Action<byte[]>;
namespace BCG {
// public delegate void Handler(IPubItem pi);
@@ -73,7 +77,7 @@ namespace BCG {
// // BlueEventLog.logEvent("Trying to unsubscribe a PubItem tag that was never subsribed", BlueEventLevel.Error);
// }
public void unsubcribe(string tag, OnData on_data) {
public void unsubscribe(string tag, OnData on_data) {
if (_dispatch_table.TryGetValue(tag, out var tag_cbs)) {
tag_cbs.Remove(on_data);
if (tag_cbs.Count == 0) {
@@ -83,8 +87,11 @@ namespace BCG {
}
}
void IManagedSubscriber.Subscribe(string tag, OnData onData) =>
subscribe(tag, onData);
IObservable<byte[]> IManagedSubscriber.Observe(string tag) =>
Observable.Create<byte[]>(o => {
subscribe(tag, o.OnNext);
return Disposable.Create(() => unsubscribe(tag, o.OnNext));
});
private void _fire_events(IEnumerable<bts.PubSubServerMessage> updates) {
// TODO
@@ -92,7 +99,7 @@ namespace BCG {
ArgumentNullException.ThrowIfNull(u.tag);
if (_dispatch_table.TryGetValue(u.tag, out var cbs)) {
foreach (var cb in cbs) {
cb.Invoke(u.tag, u.data ?? Array.Empty<byte>());
cb.Invoke(u.data ?? Array.Empty<byte>());
}
}
}