Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of actors #94

Closed
wants to merge 13 commits into from

Conversation

danielgerlag
Copy link
Contributor

Actor Runtime

To author an actor,

  1. Create a struct with your custom actor methods and annotate your input and output types as serializable. The SDK will automatically deserialize the incoming parameters from JSON and then serialize your result back to JSON.

    #[derive(Serialize, Deserialize)]
    pub struct MyRequest {
        pub name: String,
    }
    
    #[derive(Serialize, Deserialize)]
    pub struct MyResponse {
        pub available: bool,
    }   
    
    impl MyActor {
        fn do_stuff(&mut self, data: MyRequest) -> Result<MyResponse, actor::ActorError> {        
            println!("doing stuff with {}", data.name);        
            Ok(MyResponse { 
                available: true 
            })
        }    
    }
  2. Implement the Actor trait. This trait exposes the following methods:

    • on_activate - Called when an actor is activated on a host
    • on_deactivate - Called when an actor is deactivated on a host
    • on_reminder - Called when a reminder is recieved from the Dapr sidecar
    • on_timer - Called when a timer is recieved from the Dapr sidecar
    impl Actor for MyActor {
        
        async fn on_activate(&mut self) -> Result<(), ActorError> {
            println!("on_activate {}", self.id);
            Ok(())
        }
    
        fn on_deactivate(&mut self) -> Result<(), ActorError> {
            println!("on_deactivate");
            Ok(())
        }
    }
  3. An actor host requires an Http server to recieve callbacks from the Dapr sidecar. The DaprHttpServer object implements this functionality and also encapsulates the actor runtime to service any hosted actors. Use the register_actor method to register an actor type to be serviced, this method takes an ActorTypeRegistration which specifies

    • The actor type name
    • A factory to construct a new instance of that actor type when one is required to be activated by the runtime. The parameters passed to the factory will be the actor type, actor ID, and a Dapr client for managing state, timers and reminders for the actor.
    • The methods that you would like to expose to external clients.
    let mut dapr_server = dapr::server::DaprHttpServer::new();
    
    dapr_server.register_actor(ActorTypeRegistration::new("MyActor", 
        |actor_type, id, client| Box::new(MyActor{
            actor_type, 
            id, 
            client
        }))
        .register_method("do_stuff", MyActor::do_stuff)
        .register_method("do_other_stuff", MyActor::do_other_stuff));
    
    dapr_server.start(None, None).await?;

Signed-off-by: Daniel Gerlag <daniel@gerlag.ca>
Signed-off-by: Daniel Gerlag <daniel@gerlag.ca>
Signed-off-by: Daniel Gerlag <daniel@gerlag.ca>
Signed-off-by: Daniel Gerlag <daniel@gerlag.ca>
Signed-off-by: Daniel Gerlag <daniel@gerlag.ca>
Signed-off-by: Daniel Gerlag <daniel@gerlag.ca>
Signed-off-by: Daniel Gerlag <daniel@gerlag.ca>
commit 8bbf343
Author: Daniel Gerlag <daniel@gerlag.ca>
Date:   Mon May 15 19:01:50 2023 -0700

    async methods
commit 8bbf343
Author: Daniel Gerlag <daniel@gerlag.ca>
Date:   Mon May 15 19:01:50 2023 -0700

    async methods
@tqq1994516
Copy link

When will this PR be incorporated into the master, I am looking forward to the actor related functions

@danielgerlag
Copy link
Contributor Author

Closing in favor of #99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants