DEVELOPMENT ENVIRONMENT

~liljamo/emerwen

91b99511153d9e5adb1ffba835cb0706a54676f0 — Jonni Liljamo 9 days ago 99bece8
feat(master): change to protocol Worker type

* also hella strange WorkerType for the redb table value
4 files changed, 48 insertions(+), 58 deletions(-)

M emerwen-master/src/db.rs
M emerwen-master/src/main.rs
M emerwen-master/src/server/mod.rs
D emerwen-master/src/worker.rs
M emerwen-master/src/db.rs => emerwen-master/src/db.rs +34 -4
@@ 7,13 7,12 @@

use std::path::Path;

use redb::{Database, ReadableTable, TableDefinition};
use emerwen_protocol::emerwen::shared::Worker;
use redb::{Database, ReadableTable, TableDefinition, TypeName, Value};
use tokio::sync::{mpsc, oneshot};
use tracing::warn;

use crate::worker::Worker;

const WORKER_TABLE: TableDefinition<u32, Worker> = TableDefinition::new("workers");
const WORKER_TABLE: TableDefinition<u32, WorkerType> = TableDefinition::new("workers");

pub enum DBMessage {
    ReadWorkers {


@@ 119,3 118,34 @@ impl DatabaseHandle {
        rx.await.expect("actor task has been killed") // FIXME: handle
    }
}

#[derive(Debug)]
struct WorkerType;

impl Value for WorkerType {
    type SelfType<'a> = Worker;
    type AsBytes<'a> = Vec<u8>;

    fn fixed_width() -> Option<usize> {
        None
    }

    fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a>
    where
        Self: 'a,
    {
        bincode::deserialize(data).unwrap()
    }

    fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a>
    where
        Self: 'a,
        Self: 'b,
    {
        bincode::serialize(value).unwrap()
    }

    fn type_name() -> TypeName {
        TypeName::new("emerwen_worker")
    }
}

M emerwen-master/src/main.rs => emerwen-master/src/main.rs +7 -5
@@ 8,7 8,7 @@
use clap::Parser;
use emerwen_protocol::emerwen::shared::{
    target::{MethodGet, MethodPing},
    Target,
    Target, Worker,
};
use tokio::sync::mpsc;
use tracing::{debug, info, level_filters::LevelFilter};


@@ 17,8 17,6 @@ use tracing_subscriber::{prelude::*, EnvFilter};
mod db;
use db::DatabaseHandle;
mod server;
mod worker;
use worker::Worker;

#[derive(Parser)]
#[command(version)]


@@ 31,9 29,13 @@ struct Args {
    #[arg(long, default_value = "./tmp/db.redb")]
    database_file: String,

    /// Location of the socket
    /// Location of the "IPC" socket
    #[arg(long, default_value = "./tmp/master.sock")]
    socket_location: String,
    ipc_socket_location: String,

    /// IPC auth token
    #[arg(long, default_value = "ipc_avain_prkl")]
    ipc_auth_token: String,
}

#[tokio::main]

M emerwen-master/src/server/mod.rs => emerwen-master/src/server/mod.rs +7 -4
@@ 7,16 7,19 @@

use std::sync::Arc;

use emerwen_protocol::emerwen::workertomaster::{
    worker_to_master_server::{WorkerToMaster, WorkerToMasterServer},
    SetTargetStateRequest, TargetsResponse,
use emerwen_protocol::emerwen::{
    shared::Worker,
    workertomaster::{
        worker_to_master_server::{WorkerToMaster, WorkerToMasterServer},
        SetTargetStateRequest, TargetsResponse,
    },
};
use tokio::sync::{mpsc, RwLock};
use tonic::{transport::Server, Request, Response, Status};
use tonic_async_interceptor::async_interceptor;
use tracing::info;

use crate::{db::DatabaseHandle, worker::Worker};
use crate::db::DatabaseHandle;

mod auth;
use auth::AuthInterceptor;

D emerwen-master/src/worker.rs => emerwen-master/src/worker.rs +0 -45
@@ 1,45 0,0 @@
/*
 * Copyright (C) 2024 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under GPL-3.0-or-later, see NOTICE and LICENSE for
 * more information.
 */

use emerwen_protocol::emerwen::shared::Target;
use redb::{TypeName, Value};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct Worker {
    pub id: u32,
    pub auth_token: String,
    pub targets: Vec<Target>,
}

impl Value for Worker {
    type SelfType<'a> = Worker;
    type AsBytes<'a> = Vec<u8>;

    fn fixed_width() -> Option<usize> {
        None
    }

    fn from_bytes<'a>(data: &'a [u8]) -> Self::SelfType<'a>
    where
        Self: 'a,
    {
        bincode::deserialize(data).unwrap()
    }

    fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> Self::AsBytes<'a>
    where
        Self: 'a,
        Self: 'b,
    {
        bincode::serialize(value).unwrap()
    }

    fn type_name() -> TypeName {
        TypeName::new("emerwen_master_worker")
    }
}