wip
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
use crate::{Server, TlsConfigError};
|
||||
use crate::{Server, TlsConfigError, ServerConfig};
|
||||
|
||||
/// ServerBuilder for constructing Server instances with a fluent API
|
||||
pub struct ServerBuilder {
|
||||
@@ -12,7 +12,7 @@ pub struct ServerBuilder {
|
||||
tls_port: Option<u16>,
|
||||
enable_auth: bool,
|
||||
enable_webhooks: bool,
|
||||
circle_worker_id: String,
|
||||
|
||||
circles: HashMap<String, Vec<String>>,
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ impl ServerBuilder {
|
||||
tls_port: None,
|
||||
enable_auth: false,
|
||||
enable_webhooks: false,
|
||||
circle_worker_id: "default".to_string(),
|
||||
|
||||
circles: HashMap::new(),
|
||||
}
|
||||
}
|
||||
@@ -48,10 +48,7 @@ impl ServerBuilder {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn worker_id(mut self, worker_id: impl Into<String>) -> Self {
|
||||
self.circle_worker_id = worker_id.into();
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
pub fn with_tls(mut self, cert_path: String, key_path: String) -> Self {
|
||||
self.enable_tls = true;
|
||||
@@ -79,6 +76,21 @@ impl ServerBuilder {
|
||||
self.circles = circles;
|
||||
self
|
||||
}
|
||||
|
||||
/// Load configuration from a ServerConfig instance
|
||||
pub fn from_config(mut self, config: ServerConfig) -> Self {
|
||||
self.host = config.host;
|
||||
self.port = config.port;
|
||||
self.redis_url = config.redis_url;
|
||||
self.enable_auth = config.auth;
|
||||
self.enable_tls = config.tls;
|
||||
self.cert_path = config.cert;
|
||||
self.key_path = config.key;
|
||||
self.tls_port = config.tls_port;
|
||||
self.enable_webhooks = config.webhooks;
|
||||
self.circles = config.circles;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> Result<Server, TlsConfigError> {
|
||||
Ok(Server {
|
||||
@@ -91,13 +103,13 @@ impl ServerBuilder {
|
||||
tls_port: self.tls_port,
|
||||
enable_auth: self.enable_auth,
|
||||
enable_webhooks: self.enable_webhooks,
|
||||
circle_worker_id: self.circle_worker_id,
|
||||
|
||||
circle_name: "default".to_string(),
|
||||
circle_public_key: "default".to_string(),
|
||||
circles: self.circles,
|
||||
nonce_store: HashMap::new(),
|
||||
authenticated_pubkey: None,
|
||||
dispatcher: None,
|
||||
supervisor: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
use crate::Server;
|
||||
use actix::prelude::*;
|
||||
use actix_web_actors::ws;
|
||||
use hero_dispatcher::{Dispatcher, ScriptType};
|
||||
use hero_supervisor::{Supervisor, ScriptType};
|
||||
use serde_json::{json, Value};
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -82,7 +82,7 @@ impl Server {
|
||||
}
|
||||
};
|
||||
|
||||
let dispatcher = match self.dispatcher.clone() {
|
||||
let supervisor = match self.supervisor.clone() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
let err_resp = JsonRpcResponse {
|
||||
@@ -90,7 +90,7 @@ impl Server {
|
||||
result: None,
|
||||
error: Some(JsonRpcError {
|
||||
code: -32603,
|
||||
message: "Internal error: dispatcher not available".to_string(),
|
||||
message: "Internal error: supervisor not available".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
id: client_rpc_id,
|
||||
@@ -102,7 +102,7 @@ impl Server {
|
||||
|
||||
let client_rpc_id_clone = client_rpc_id.clone();
|
||||
let fut = async move {
|
||||
dispatcher.start_job(&job_id).await
|
||||
supervisor.start_job(&job_id).await
|
||||
};
|
||||
|
||||
ctx.spawn(
|
||||
@@ -190,7 +190,7 @@ impl Server {
|
||||
}
|
||||
};
|
||||
|
||||
let dispatcher = match self.dispatcher.clone() {
|
||||
let supervisor = match self.supervisor.clone() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
let err_resp = JsonRpcResponse {
|
||||
@@ -198,7 +198,7 @@ impl Server {
|
||||
result: None,
|
||||
error: Some(JsonRpcError {
|
||||
code: -32603,
|
||||
message: "Internal error: dispatcher not available".to_string(),
|
||||
message: "Internal error: supervisor not available".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
id: client_rpc_id,
|
||||
@@ -210,7 +210,7 @@ impl Server {
|
||||
|
||||
let client_rpc_id_clone = client_rpc_id.clone();
|
||||
let fut = async move {
|
||||
dispatcher.get_job_status(&job_id).await
|
||||
supervisor.get_job_status(&job_id).await
|
||||
};
|
||||
|
||||
ctx.spawn(
|
||||
@@ -279,7 +279,7 @@ impl Server {
|
||||
return;
|
||||
}
|
||||
|
||||
let dispatcher = match self.dispatcher.clone() {
|
||||
let supervisor = match self.supervisor.clone() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
let err_resp = JsonRpcResponse {
|
||||
@@ -287,7 +287,7 @@ impl Server {
|
||||
result: None,
|
||||
error: Some(JsonRpcError {
|
||||
code: -32603,
|
||||
message: "Internal error: dispatcher not available".to_string(),
|
||||
message: "Internal error: supervisor not available".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
id: client_rpc_id,
|
||||
@@ -299,7 +299,7 @@ impl Server {
|
||||
|
||||
let client_rpc_id_clone = client_rpc_id.clone();
|
||||
let fut = async move {
|
||||
dispatcher.list_jobs().await
|
||||
supervisor.list_jobs().await
|
||||
};
|
||||
|
||||
ctx.spawn(
|
||||
@@ -403,7 +403,7 @@ impl Server {
|
||||
}
|
||||
};
|
||||
|
||||
let dispatcher = match self.dispatcher.clone() {
|
||||
let supervisor = match self.supervisor.clone() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
let err_resp = JsonRpcResponse {
|
||||
@@ -411,7 +411,7 @@ impl Server {
|
||||
result: None,
|
||||
error: Some(JsonRpcError {
|
||||
code: -32603,
|
||||
message: "Internal error: dispatcher not available".to_string(),
|
||||
message: "Internal error: supervisor not available".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
id: client_rpc_id,
|
||||
@@ -423,7 +423,7 @@ impl Server {
|
||||
|
||||
let client_rpc_id_clone = client_rpc_id.clone();
|
||||
let fut = async move {
|
||||
dispatcher
|
||||
supervisor
|
||||
.new_job()
|
||||
.context_id(&circle_pk)
|
||||
.script_type(ScriptType::RhaiSAL)
|
||||
@@ -518,7 +518,7 @@ impl Server {
|
||||
}
|
||||
};
|
||||
|
||||
let dispatcher = match self.dispatcher.clone() {
|
||||
let supervisor = match self.supervisor.clone() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
let err_resp = JsonRpcResponse {
|
||||
@@ -526,7 +526,7 @@ impl Server {
|
||||
result: None,
|
||||
error: Some(JsonRpcError {
|
||||
code: -32603,
|
||||
message: "Internal error: dispatcher not available".to_string(),
|
||||
message: "Internal error: supervisor not available".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
id: client_rpc_id,
|
||||
@@ -538,7 +538,7 @@ impl Server {
|
||||
|
||||
let client_rpc_id_clone = client_rpc_id.clone();
|
||||
let fut = async move {
|
||||
dispatcher.get_job_output(&job_id).await
|
||||
supervisor.get_job_output(&job_id).await
|
||||
};
|
||||
|
||||
ctx.spawn(
|
||||
@@ -625,7 +625,7 @@ impl Server {
|
||||
}
|
||||
};
|
||||
|
||||
let dispatcher = match self.dispatcher.clone() {
|
||||
let supervisor = match self.supervisor.clone() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
let err_resp = JsonRpcResponse {
|
||||
@@ -633,7 +633,7 @@ impl Server {
|
||||
result: None,
|
||||
error: Some(JsonRpcError {
|
||||
code: -32603,
|
||||
message: "Internal error: dispatcher not available".to_string(),
|
||||
message: "Internal error: supervisor not available".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
id: client_rpc_id,
|
||||
@@ -645,7 +645,7 @@ impl Server {
|
||||
|
||||
let client_rpc_id_clone = client_rpc_id.clone();
|
||||
let fut = async move {
|
||||
dispatcher.get_job_logs(&job_id).await
|
||||
supervisor.get_job_logs(&job_id).await
|
||||
};
|
||||
|
||||
ctx.spawn(
|
||||
@@ -733,7 +733,7 @@ impl Server {
|
||||
}
|
||||
};
|
||||
|
||||
let dispatcher = match self.dispatcher.clone() {
|
||||
let supervisor = match self.supervisor.clone() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
let err_resp = JsonRpcResponse {
|
||||
@@ -741,7 +741,7 @@ impl Server {
|
||||
result: None,
|
||||
error: Some(JsonRpcError {
|
||||
code: -32603,
|
||||
message: "Internal error: dispatcher not available".to_string(),
|
||||
message: "Internal error: supervisor not available".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
id: client_rpc_id,
|
||||
@@ -753,7 +753,7 @@ impl Server {
|
||||
|
||||
let client_rpc_id_clone = client_rpc_id.clone();
|
||||
let fut = async move {
|
||||
dispatcher.stop_job(&job_id).await
|
||||
supervisor.stop_job(&job_id).await
|
||||
};
|
||||
|
||||
ctx.spawn(
|
||||
@@ -840,7 +840,7 @@ impl Server {
|
||||
}
|
||||
};
|
||||
|
||||
let dispatcher = match self.dispatcher.clone() {
|
||||
let supervisor = match self.supervisor.clone() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
let err_resp = JsonRpcResponse {
|
||||
@@ -848,7 +848,7 @@ impl Server {
|
||||
result: None,
|
||||
error: Some(JsonRpcError {
|
||||
code: -32603,
|
||||
message: "Internal error: dispatcher not available".to_string(),
|
||||
message: "Internal error: supervisor not available".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
id: client_rpc_id,
|
||||
@@ -860,7 +860,7 @@ impl Server {
|
||||
|
||||
let client_rpc_id_clone = client_rpc_id.clone();
|
||||
let fut = async move {
|
||||
dispatcher.delete_job(&job_id).await
|
||||
supervisor.delete_job(&job_id).await
|
||||
};
|
||||
|
||||
ctx.spawn(
|
||||
@@ -929,7 +929,7 @@ impl Server {
|
||||
return;
|
||||
}
|
||||
|
||||
let dispatcher = match self.dispatcher.clone() {
|
||||
let supervisor = match self.supervisor.clone() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
let err_resp = JsonRpcResponse {
|
||||
@@ -937,7 +937,7 @@ impl Server {
|
||||
result: None,
|
||||
error: Some(JsonRpcError {
|
||||
code: -32603,
|
||||
message: "Internal error: dispatcher not available".to_string(),
|
||||
message: "Internal error: supervisor not available".to_string(),
|
||||
data: None,
|
||||
}),
|
||||
id: client_rpc_id,
|
||||
@@ -949,7 +949,7 @@ impl Server {
|
||||
|
||||
let client_rpc_id_clone = client_rpc_id.clone();
|
||||
let fut = async move {
|
||||
dispatcher.clear_all_jobs().await
|
||||
supervisor.clear_all_jobs().await
|
||||
};
|
||||
|
||||
ctx.spawn(
|
||||
|
@@ -3,7 +3,7 @@ use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
||||
use actix_web_actors::ws;
|
||||
use log::{info, error}; // Added error for better logging
|
||||
use once_cell::sync::Lazy;
|
||||
use hero_dispatcher::{Dispatcher, DispatcherBuilder, DispatcherError};
|
||||
use hero_supervisor::{Supervisor, SupervisorBuilder, SupervisorError};
|
||||
use hero_job::{Job, JobStatus};
|
||||
use rustls::pki_types::PrivateKeyDer;
|
||||
use rustls::ServerConfig as RustlsServerConfig;
|
||||
@@ -211,7 +211,7 @@ pub struct Server {
|
||||
pub circles: HashMap<String, Vec<String>>,
|
||||
nonce_store: HashMap<String, NonceResponse>,
|
||||
authenticated_pubkey: Option<String>,
|
||||
pub dispatcher: Option<Dispatcher>,
|
||||
pub supervisor: Option<Supervisor>,
|
||||
}
|
||||
|
||||
impl Server {
|
||||
@@ -552,15 +552,15 @@ impl Server {
|
||||
|
||||
let fut = async move {
|
||||
let caller_id = public_key.unwrap_or_else(|| "anonymous".to_string());
|
||||
match DispatcherBuilder::new()
|
||||
match SupervisorBuilder::new()
|
||||
.redis_url(&redis_url_clone)
|
||||
.caller_id(&caller_id)
|
||||
.build() {
|
||||
Ok(hero_dispatcher) => {
|
||||
hero_dispatcher
|
||||
Ok(hero_supervisor) => {
|
||||
hero_supervisor
|
||||
.new_job()
|
||||
.context_id(&circle_pk_clone)
|
||||
.script_type(hero_dispatcher::ScriptType::RhaiSAL)
|
||||
.script_type(hero_supervisor::ScriptType::RhaiSAL)
|
||||
.script(&script_content)
|
||||
.timeout(TASK_TIMEOUT_DURATION)
|
||||
.await_response()
|
||||
@@ -574,7 +574,7 @@ impl Server {
|
||||
fut.into_actor(self)
|
||||
.map(move |res, _act, ctx_inner| match res {
|
||||
Ok(output) => {
|
||||
// The dispatcher returns the actual string output from job execution
|
||||
// The supervisor returns the actual string output from job execution
|
||||
let result_value = PlayResult { output };
|
||||
let resp = JsonRpcResponse {
|
||||
jsonrpc: "2.0".to_string(),
|
||||
@@ -586,7 +586,7 @@ impl Server {
|
||||
}
|
||||
Err(e) => {
|
||||
let (code, message) = match e {
|
||||
DispatcherError::Timeout(task_id) => (
|
||||
SupervisorError::Timeout(task_id) => (
|
||||
-32002,
|
||||
format!(
|
||||
"Timeout waiting for Rhai script (task: {})",
|
||||
|
Reference in New Issue
Block a user