Skip to content

Commit

Permalink
Merge pull request #5 from eigr-labs/feat/try-again
Browse files Browse the repository at this point in the history
Feat/try again
  • Loading branch information
sleipnir authored Sep 18, 2023
2 parents 61e65b7 + 2999a78 commit d675c31
Show file tree
Hide file tree
Showing 24 changed files with 1,261 additions and 798 deletions.
1,221 changes: 726 additions & 495 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions spawn-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ version = "0.1.0"

[dependencies]
env_logger = "0.10.0"
prost-types = "0.11"
log = {version = "0.4.8", features = ["std"]}
prost = "0.12.1"
prost-types = "0.12.1"
rocket = "=0.5.0-rc.3"
spawn-rs = {path = "../spawn-rs"}
tokio = {version = "1.25.0", features = ["full"]}

[build-dependencies]
tonic-build = "0.8"
7 changes: 7 additions & 0 deletions spawn-examples/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub(crate) fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::configure()
.build_server(false)
.out_dir("src/domain")
.compile(&["proto/domain.proto"], &["proto"])?;
Ok(())
}
15 changes: 15 additions & 0 deletions spawn-examples/proto/domain.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package domain;

message State {
repeated string languages = 1;
}

message Request {
string language = 1;
}

message Reply {
string response = 1;
}
18 changes: 18 additions & 0 deletions spawn-examples/src/domain/domain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct State {
#[prost(string, repeated, tag = "1")]
pub languages: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Request {
#[prost(string, tag = "1")]
pub language: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Reply {
#[prost(string, tag = "1")]
pub response: ::prost::alloc::string::String,
}
1 change: 1 addition & 0 deletions spawn-examples/src/domain/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod domain;
64 changes: 20 additions & 44 deletions spawn-examples/src/joe.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,26 @@
use prost_types::Any;
use spawn_examples::domain::domain::{Reply, Request};
use spawn_rs::{context::Context, value::Value, Message};

use spawn_rs::{
action::Action,
actor::{Actor, ActorSettings, Kind},
context::Context,
serializer::Serializer,
value::Value,
Message,
};
use log::info;

pub struct Joe;
pub fn set_language(msg: Message, ctx: Context) -> Value {
info!("Actor msg: {:?}", msg);
let value: Value = match msg.body::<Request>() {
Ok(request) => {
let lang = request.language;
info!("Setlanguage To: {:?}", lang);
let reply = Reply::default();

impl Serializer for Joe {
fn decode(&mut self, _msg: prost_types::Any) -> Box<dyn std::any::Any> {
todo!()
}

fn encode(&mut self, _msg: Box<dyn std::any::Any>) -> prost_types::Any {
todo!()
}
}

impl Actor for Joe {
fn settings(&mut self) -> ActorSettings {
ActorSettings::new()
.name("joe".to_owned())
.kind(Kind::SINGLETON)
.stateful(true)
.actions(vec!["sum".to_string()])
.deactivated_timeout(30000)
.snapshot_timeout(10000)
.to_owned()
}
}

impl Action for Joe {
fn handle(&mut self, msg: Message, ctx: &mut Context) -> Value {
match msg.action() {
"sum" => Value::new()
Value::new()
.state(ctx.state().clone())
.response(Any::default())
.to_owned(),
_ => Value::new()
.state(Any::default())
.response(Any::default())
.to_owned(),
.response(&Reply::default())
.to_owned()
}
}
Err(e) => Value::new()
.state(ctx.state().clone())
//.response(Any::default())
.to_owned(),
};

return value;
}
5 changes: 5 additions & 0 deletions spawn-examples/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[macro_use]
extern crate log;
extern crate prost_types;

pub mod domain;
31 changes: 22 additions & 9 deletions spawn-examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
extern crate env_logger;
extern crate prost_types;
extern crate tokio;
extern crate rocket;

mod joe;

use joe::set_language;
use spawn_rs::actor::{ActorDefinition, ActorSettings, Kind};
use spawn_rs::spawn::Spawn;

#[tokio::main]
async fn main() {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("debug"));

#[rocket::main]
async fn main() -> Result<(), rocket::Error> {
Spawn::new()
.system("spawn-system".to_string())
.port(8091)
.add_actor(Box::new(joe::Joe {}))
.create("spawn-system".to_string())
.with_actor(
ActorDefinition::new()
.with_settings(
ActorSettings::new()
.name("joe".to_owned())
.kind(Kind::NAMED)
.stateful(true)
.deactivated_timeout(30000)
.snapshot_timeout(10000)
.to_owned(),
)
.with_action("sum".to_owned(), set_language),
)
.start()
.await;
.await?;

Ok(())
}
9 changes: 4 additions & 5 deletions spawn-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ name = "spawn-rs"
version = "0.1.0"

[dependencies]
actix-protobuf = "0.9.0"
actix-server = "2.2.0"
actix-web = "4"
env_logger = "0.10.0"
prost = "0.11"
prost-types = "0.11"
log = {version = "0.4.8", features = ["std"]}
prost = "0.12.1"
prost-types = "0.12.1"
rocket = "=0.5.0-rc.3"

[build-dependencies]
tonic-build = "0.8"
46 changes: 23 additions & 23 deletions spawn-rs/proto/actor.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@ message TimeoutStrategy {
int64 timeout = 1;
}

// A command represents an action that the user can perform on an Actor.
// Commands in supporting languages are represented by functions or methods.
// An Actor command has nothing to do with the semantics of Commands in a CQRS/EventSourced system.
// A action represents an action that the user can perform on an Actor.
// Actions in supporting languages are represented by functions or methods.
// An Actor action has nothing to do with the semantics of Actions in a CQRS/EventSourced system.
// It just represents an action that supporting languages can invoke.
message Command {
message Action {

// The name of the function or method in the supporting language that has been registered in Ator.
string name = 1;
}

// A FixedTimerCommand is similar to a regular Command, its main differences are that it is scheduled to run at regular intervals
// A FixedTimerAction is similar to a regular Action, its main differences are that it is scheduled to run at regular intervals
// and only takes the actor's state as an argument.
// Timer Commands are good for executing loops that manipulate the actor's own state.
// Timer Actions are good for executing loops that manipulate the actor's own state.
// In Elixir or other languages in BEAM it would be similar to invoking Process.send_after(self(), atom, msg, timeout)
message FixedTimerCommand {
message FixedTimerAction {

// The time to wait until the command is triggered
// The time to wait until the action is triggered
int32 seconds = 1;

// See Command description Above
Command command = 2;
// See Action description Above
Action action = 2;
}

message ActorState {
Expand All @@ -68,7 +68,7 @@ message ActorState {

// TODO doc here
message Metadata {
// A channel group represents a way to send commands to various actors
// A channel group represents a way to send actions to various actors
// that belong to a certain semantic group.
string channel_group = 1;

Expand All @@ -79,17 +79,17 @@ message Metadata {
// Regardless of the type of actor it is important that
// all actors are registered during the proxy and host initialization phase.
enum Kind {
// When no type is informed, the default to be assumed will be the Singleton pattern.
// When no type is informed, the default to be assumed will be the Named pattern.
UNKNOW_KIND = 0;

// Abstract actors are used to create children of this based actor at runtime
ABSTRACT = 1;
// NAMED actors are used to create children of this based actor at runtime
NAMED = 1;

// Singleton actors as the name suggests have only one real instance of themselves running
// during their entire lifecycle. That is, they are the opposite of the Abstract type Actors.
SINGLETON = 2;
// UNAMED actors as the name suggests have only one real instance of themselves running
// during their entire lifecycle. That is, they are the opposite of the NAMED type Actors.
UNAMED = 2;

// Pooled Actors are similar to abstract actors, but unlike them,
// Pooled Actors are similar to Unamed actors, but unlike them,
// their identifying name will always be the one registered at the system initialization stage.
// The great advantage of Pooled actors is that they have multiple instances of themselves
// acting as a request service pool.
Expand Down Expand Up @@ -134,7 +134,7 @@ message ActorId {
// Name of a ActorSystem
string system = 2;

// When the Actor is of the Abstract type,
// When the Actor is of the Unamed type,
// the name of the parent Actor must be informed here.
string parent = 3;
}
Expand All @@ -152,9 +152,9 @@ message Actor {
// Actor settings.
ActorSettings settings = 3;

// The commands registered for an actor
repeated Command commands = 4;
// The actions registered for an actor
repeated Action actions = 4;

// The registered timer commands for an actor.
repeated FixedTimerCommand timer_commands = 5;
// The registered timer actions for an actor.
repeated FixedTimerAction timer_actions = 5;
}
Loading

0 comments on commit d675c31

Please sign in to comment.