Add basic models

Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
Lee Smet
2025-08-20 14:04:08 +02:00
parent b769fa63f7
commit acf875ed33
10 changed files with 225 additions and 0 deletions

2
src/lib.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod models;
mod time;

15
src/models.rs Normal file
View File

@@ -0,0 +1,15 @@
mod actor;
mod context;
mod flow;
mod job;
mod message;
mod runner;
mod script_type;
pub use actor::Actor;
pub use context::Context;
pub use flow::Flow;
pub use job::Job;
pub use message::Message;
pub use runner::Runner;
pub use script_type::ScriptType;

15
src/models/actor.rs Normal file
View File

@@ -0,0 +1,15 @@
use std::net::IpAddr;
use serde::{Deserialize, Serialize};
use crate::time::Timestamp;
#[derive(Serialize, Deserialize)]
pub struct Actor {
id: u32,
pubkey: String,
/// IP where the actor is reachable, can be mycelium but that is not mandatory
address: Vec<IpAddr>,
created_at: Timestamp,
updated_at: Timestamp,
}

17
src/models/context.rs Normal file
View File

@@ -0,0 +1,17 @@
use serde::{Deserialize, Serialize};
use crate::time::Timestamp;
#[derive(Serialize, Deserialize)]
pub struct Context {
/// Redis DB to use
id: u32,
/// Actor ids which have admin rights on this context
admins: Vec<u32>,
/// Actor ids which can read the context info
readers: Vec<u32>,
/// Actor ids which can execute jobs in this context
executors: Vec<u32>,
created_at: Timestamp,
upddated_at: Timestamp,
}

33
src/models/flow.rs Normal file
View File

@@ -0,0 +1,33 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::time::Timestamp;
#[derive(Serialize, Deserialize)]
pub struct Flow {
/// Job Id set tby the actor which created it
id: u32,
/// Actor Id who created this job
caller_id: u32,
/// The context in which this job is executed
context_id: u32,
/// List of jobs which make up the flow
jobs: Vec<u32>,
/// Environment variables, passed to every job when executed
env_vars: HashMap<String, String>,
/// The result of the flow
result: HashMap<String, String>,
created_at: Timestamp,
updated_at: Timestamp,
status: FlowStatus,
}
/// The status of a flow
#[derive(Serialize, Deserialize)]
pub enum FlowStatus {
Dispatched,
Started,
Error,
Finished,
}

38
src/models/job.rs Normal file
View File

@@ -0,0 +1,38 @@
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use crate::{models::ScriptType, time::Timestamp};
#[derive(Serialize, Deserialize)]
pub struct Job {
/// Job Id, this is given by the actor who created the job
id: u32,
/// Actor ID which created this job
caller_id: u32,
/// Context in which the job is executed
context_id: u32,
script: String,
script_type: ScriptType,
/// Timeout in seconds for this job
timeout: u32,
/// Max amount of times to retry this job
retries: u8,
env_vars: HashMap<String, String>,
result: HashMap<String, String>,
prerequisites: Vec<String>,
/// Ids of jobs this job depends on, i.e. this job can't start until those have finished
depends: Vec<u32>,
created_at: Timestamp,
updated_at: Timestamp,
status: JobStatus,
}
#[derive(Serialize, Deserialize)]
pub enum JobStatus {
Dispatched,
WaitingForPrerequisites,
Started,
Error,
Finished,
}

54
src/models/message.rs Normal file
View File

@@ -0,0 +1,54 @@
use serde::{Deserialize, Serialize};
use crate::{
models::{Job, ScriptType},
time::Timestamp,
};
#[derive(Serialize, Deserialize)]
pub struct Message {
/// Unique ID for the message, set by the caller
id: u32,
/// Id of the actor who sent this message
caller_id: u32,
/// Id of the context in which this message was sent
context_id: u32,
message: String,
message_type: ScriptType,
message_format_type: MessageFormatType,
/// Seconds for the message to arrive at the destination
timeout: u32,
/// Seconds for the receiver to acknowledge receipt of the message
timeout_ack: u32,
/// Seconds for the receiver to send us a reply
timeout_result: u32,
job: Vec<Job>,
logs: Vec<Log>,
created_at: Timestamp,
updated_at: Timestamp,
status: MessageStatus,
}
#[derive(Serialize, Deserialize)]
pub enum MessageType {
Job,
Chat,
Mail,
}
#[derive(Serialize, Deserialize)]
pub enum MessageStatus {
Dispatched,
Acknowledged,
Error,
Processed,
}
#[derive(Serialize, Deserialize)]
pub enum MessageFormatType {
Html,
Text,
Md,
}
type Log = String;

28
src/models/runner.rs Normal file
View File

@@ -0,0 +1,28 @@
use std::net::IpAddr;
use serde::{Deserialize, Serialize};
use crate::time::Timestamp;
#[derive(Serialize, Deserialize)]
pub struct Runner {
id: u32,
/// Mycelium public key
pubkey: String,
/// Mycelium address
address: IpAddr,
/// Needs to be set by the runner, usually `runner<runnerid`
topic: String,
/// If this is true, the runner also listens on a local redis queue
local: bool,
crated_at: Timestamp,
updated_at: Timestamp,
}
#[derive(Serialize, Deserialize)]
pub enum RunnerType {
V,
Python,
Osis,
Rust,
}

View File

@@ -0,0 +1,9 @@
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
pub enum ScriptType {
Osis,
Sal,
V,
Python,
}

14
src/time.rs Normal file
View File

@@ -0,0 +1,14 @@
use std::time::{SystemTime, UNIX_EPOCH};
/// A timestamp since the unix epoch
pub type Timestamp = i64;
/// Get the current system timestamp
pub fn current_timestamp() -> Timestamp {
let now = SystemTime::now();
// A duration is always positive so this returns an unsigned integer, while a timestamp can
// predate the unix epoch so we must cast to a signed integer.
now.duration_since(UNIX_EPOCH)
.expect("Time moves forward")
.as_secs() as i64
}