Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
jobmanager
Browse files Browse the repository at this point in the history
  • Loading branch information
despiegk committed Dec 21, 2024
1 parent a88f139 commit 388b36f
Show file tree
Hide file tree
Showing 17 changed files with 1,075 additions and 61 deletions.
11 changes: 11 additions & 0 deletions crystallib/servers/jobmanager/example.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module main

import servers.rpcsocket

fn main() {
// Create a new RPC server on port 8080
mut server := rpcsocket.new_server(8080)!

// Start the server (this will block)
server.start()!
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,30 @@ pub fn (m AgentManager) find(params AgentFindParams) []Agent {

// Helper function to check if an agent matches the find parameters
fn matches_agent_params(agent Agent, params AgentFindParams) bool {
if params.id != none && params.id != agent.id {
return false
if id := params.id {
if id != agent.id {
return false
}
}
if params.name != none && params.name != agent.name {
return false
if name := params.name {
if name != agent.name {
return false
}
}
if params.ipaddr != none && params.ipaddr != agent.ipaddr {
return false
if ipaddr := params.ipaddr {
if ipaddr != agent.ipaddr {
return false
}
}
if params.location != none && params.location != agent.location {
return false
if location := params.location {
if location != agent.location {
return false
}
}
if params.pubkey != none && params.pubkey != agent.pubkey {
return false
if pubkey := params.pubkey {
if pubkey != agent.pubkey {
return false
}
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn (mut m ExecutorManager) set(mut executor Executor) !Executor {
// Get an executor by ID
pub fn (m ExecutorManager) get(id u32) !Executor {
if id in m.executors {
return m.executors[id]
return m.executors[id] or { return error('Failed to get executor') }
}
return error('Executor with ID ${id} not found')
}
Expand Down Expand Up @@ -90,17 +90,23 @@ pub fn (m ExecutorManager) find(params ExecutorFindParams) []Executor {

// Helper function to check if an executor matches the find parameters
fn matches_executor_params(executor Executor, params ExecutorFindParams) bool {
if params.id != none && params.id != executor.id {
return false
if id := params.id {
if id != executor.id {
return false
}
}
if params.name != none && name_fix(executor.name) != name_fix(params.name) {
return false
if name := params.name {
if name_fix(executor.name) != name_fix(name) {
return false
}
}
if params.state != none && params.state != executor.state {
return false
if state := params.state {
if state != executor.state {
return false
}
}
if params.actor_name != none {
actor_name_fixed := name_fix(params.actor_name)
if actor_name := params.actor_name {
actor_name_fixed := name_fix(actor_name)
if actor_name_fixed !in executor.actors {
return false
}
Expand All @@ -113,7 +119,7 @@ pub fn (mut m ExecutorManager) add_actor(executor_id u32, mut actor Actor) ! {
if executor_id !in m.executors {
return error('Executor with ID ${executor_id} not found')
}
mut executor := m.executors[executor_id]
mut executor := m.executors[executor_id] or { return error('Failed to get executor') }
executor.add_actor(actor)!
m.executors[executor_id] = executor
}
Expand All @@ -123,7 +129,7 @@ pub fn (m ExecutorManager) get_actor(executor_id u32, actor_name string) !&Actor
if executor_id !in m.executors {
return error('Executor with ID ${executor_id} not found')
}
executor := m.executors[executor_id]
executor := m.executors[executor_id] or { return error('Failed to get executor') }
return executor.get_actor(actor_name)
}

Expand All @@ -132,7 +138,7 @@ pub fn (mut m ExecutorManager) add_action(executor_id u32, actor_name string, mu
if executor_id !in m.executors {
return error('Executor with ID ${executor_id} not found')
}
mut executor := m.executors[executor_id]
mut executor := m.executors[executor_id] or { return error('Failed to get executor') }
mut actor := executor.get_actor(actor_name)!
actor.add_action(action)!
executor.actors[name_fix(actor_name)] = actor
Expand All @@ -144,7 +150,7 @@ pub fn (m ExecutorManager) get_action(executor_id u32, actor_name string, action
if executor_id !in m.executors {
return error('Executor with ID ${executor_id} not found')
}
executor := m.executors[executor_id]
executor := m.executors[executor_id] or { return error('Failed to get executor') }
actor := executor.get_actor(actor_name)!
return actor.get_action(action_name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,45 @@ pub fn (m JobManager) find(params JobFindParams) []Job {

// Helper function to check if a job matches the find parameters
fn matches_job_params(job Job, params JobFindParams) bool {
if params.id != none && params.id != job.id {
return false
if id := params.id {
if id != job.id {
return false
}
}
if params.actor != none && params.actor != job.actor {
return false
if actor := params.actor {
if actor != job.actor {
return false
}
}
if params.action != none && params.action != job.action {
return false
if action := params.action {
if action != job.action {
return false
}
}
if params.job_type != none && params.job_type != job.job_type {
return false
if job_type := params.job_type {
if job_type != job.job_type {
return false
}
}
if params.completed != none && params.completed != job.completed {
return false
if completed := params.completed {
if completed != job.completed {
return false
}
}
if params.state != none && params.state != job.state {
return false
if state := params.state {
if state != job.state {
return false
}
}
if params.agent != none && params.agent != job.agent {
return false
if agent := params.agent {
if agent != job.agent {
return false
}
}
if params.executor != none && params.executor != job.executor {
return false
if executor := params.executor {
if executor != job.executor {
return false
}
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,35 @@ pub fn (m JobLogManager) find(params JobLogFindParams) []JobLog {

// Helper function to check if a job log matches the find parameters
fn matches_job_log_params(log JobLog, params JobLogFindParams) bool {
if params.id != none && params.id != log.id {
return false
if id := params.id {
if id != log.id {
return false
}
}
if params.job != none && params.job != log.job {
return false
if job := params.job {
if job != log.job {
return false
}
}
if params.category != none && params.category != log.category {
return false
if category := params.category {
if category != log.category {
return false
}
}
if params.log_sequence != none && params.log_sequence != log.log_sequence {
return false
if log_sequence := params.log_sequence {
if log_sequence != log.log_sequence {
return false
}
}
if params.min_sequence != none && log.log_sequence < params.min_sequence {
return false
if min_sequence := params.min_sequence {
if log.log_sequence < min_sequence {
return false
}
}
if params.max_sequence != none && log.log_sequence > params.max_sequence {
return false
if max_sequence := params.max_sequence {
if log.log_sequence > max_sequence {
return false
}
}
return true
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
module rpcsocket

import freeflowuniverse.crystallib.data.ourtime

fn test_job_manager() {
mut manager := new_job_manager()

// Test creating new jobs
now := ourtime.OurTime{
unixt: i64(0)
}
mut job1 := Job{
id: 0 // Will be set by manager
actor: 'test_actor'
action: 'test_action'
job_type: 'test_type'
state: .init
create_date: now
schedule_date: now
}
mut job2 := Job{
id: 0 // Will be set by manager
actor: 'test_actor2'
action: 'test_action2'
job_type: 'test_type2'
state: .running
create_date: now
schedule_date: now
}

// Test set (create)
Expand Down Expand Up @@ -98,10 +109,13 @@ fn test_job_manager() {

// Test creating job after delete_all
mut job3 := Job{
id: 0 // Will be set by manager
actor: 'test_actor3'
action: 'test_action3'
job_type: 'test_type3'
state: .init
create_date: now
schedule_date: now
}
job3 = manager.set(mut job3)!
assert job3.id == 1 // ID should start from 1 again
Expand All @@ -111,33 +125,45 @@ fn test_job_manager_params() {
mut manager := new_job_manager()

// Create jobs with different states and parameters
now := ourtime.OurTime{
unixt: i64(0)
}
mut jobs := [
Job{
id: 0 // Will be set by manager
actor: 'actor1'
action: 'action1'
job_type: 'type1'
state: .init
completed: false
agent: 1
executor: 10
create_date: now
schedule_date: now
},
Job{
id: 0 // Will be set by manager
actor: 'actor1'
action: 'action2'
job_type: 'type1'
state: .running
completed: true
agent: 2
executor: 20
create_date: now
schedule_date: now
},
Job{
id: 0 // Will be set by manager
actor: 'actor2'
action: 'action1'
job_type: 'type2'
state: .completed
completed: true
agent: 1
executor: 30
create_date: now
schedule_date: now
}
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,25 @@ pub fn (m SignatureRequestManager) find(params SignatureRequestFindParams) []Sig

// Helper function to check if a signature request matches the find parameters
fn matches_signature_request_params(request SignatureRequest, params SignatureRequestFindParams) bool {
if params.id != none && params.id != request.id {
return false
if id := params.id {
if id != request.id {
return false
}
}
if params.job != none && params.job != request.job {
return false
if job := params.job {
if job != request.job {
return false
}
}
if params.pubkey != none && params.pubkey != request.pubkey {
return false
if pubkey := params.pubkey {
if pubkey != request.pubkey {
return false
}
}
if params.verified != none && params.verified != request.verified {
return false
if verified := params.verified {
if verified != request.verified {
return false
}
}
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ pub mut:
name string @[required]
description string
state ExecutorState @[required]
actors map[string]&Actor
actors map[string]&Actor = map[string]&Actor{}
}

fn (mut e Executor) cleanup() {
for _, mut actor in e.actors {
actor.actions.clear()
}
e.actors.clear()
}

pub enum ExecutorState {
Expand Down Expand Up @@ -42,7 +49,11 @@ pub mut:
executor string @[required] // References Executor.name
description string
mut:
actions map[string]&Action
actions map[string]&Action = map[string]&Action{}
}

fn (mut a Actor) cleanup() {
a.actions.clear()
}

pub fn (mut a Actor) add_action(action &Action) ! {
Expand Down
Loading

0 comments on commit 388b36f

Please sign in to comment.