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

[Feature Request] add WorkerBuilder to simplify worker usage #821

Open
bxb100 opened this issue Sep 18, 2024 · 1 comment
Open

[Feature Request] add WorkerBuilder to simplify worker usage #821

bxb100 opened this issue Sep 18, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@bxb100
Copy link

bxb100 commented Sep 18, 2024

Is your feature request related to a problem? Please describe.

Describe the solution you'd like

#[derive(Default)]
pub struct WorkerBuilder {
    core_worker: Option<Arc<dyn CoreWorker>>,
    activities: Vec<(String, BoxActFn)>,
    workflows: Vec<(String, WorkflowFunction)>,
    data: Vec<Box<dyn Send + Sync + 'static>>
}

impl WorkerBuilder {
    pub fn builder() -> Self {
        Self {
            ..Default::default()
        }
    }

    pub fn register_activity<A, R, O>(mut self, activity_type: impl Into<String>, act_function: impl IntoActivityFunc<A, R, O>) -> Self {
        self.activities.push((activity_type.into(), act_function.into_activity_fn()));
        self
    }

    pub fn register_workflow(mut self, workflow_type: impl Into<String>, wf_function: impl Into<WorkflowFunction>) -> Self {
        self.workflows.push((workflow_type.into(), wf_function.into()));
        self
    }
    
    pub fn insert_app_data<T: Send + Sync + 'static>(mut self, data: T) -> Self {
        self.datum.push(Box::new(data));
        self
    }

    pub async fn build(self) -> anyhow::Result<Worker> {
        let core_worker = self.core_worker.ok_or_else(|| anyhow!("Core worker not set"))?;
        let task_queue = core_worker.get_config().task_queue.clone();
        let mut worker = Worker::new_from_core(core_worker, task_queue);
        for (activity_type, act_func) in self.activities {
            worker.activity_half.activity_fns.insert(
                activity_type,
                ActivityFunction {
                    act_func,
                },
            );
        }
        for (workflow_type, wf_fn) in self.workflows {
            worker.register_wf(workflow_type, wf_fn);
        }
        for datum in self.data {
            worker.insert_app_data(datum);
        }
        Ok(worker)
    }
}

Additional context

@bxb100 bxb100 added the enhancement New feature or request label Sep 18, 2024
@mjameswh
Copy link
Contributor

@bxb100 Looks like this was meant for Core SDK, right?

@mjameswh mjameswh transferred this issue from temporalio/sdk-typescript Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants