rusty-celery icon indicating copy to clipboard operation
rusty-celery copied to clipboard

Creating tasks from inside running task

Open retrry opened this issue 4 years ago • 12 comments

Is your feature request related to a problem? Please describe. Ability to create tasks from other tasks. Currently, as I understand, the only way to publish tasks is via Celery app. I would like to have ability to create one tasks from already running task.

Additional context I have a use case, where I would like to use Rust and Celery for distributing tasks. In this case I need to have a Rust task, which works with some data, splits it and passes it to separate tasks (could be up to 1k of separate small tasks), which are then processed separately.

retrry avatar Feb 03 '21 16:02 retrry

Hey @retrry, this is definitely an important feature to have. I'm going to put it 0.4.0 milestone so that this becomes a priority. Right now I think the best way to implement this would be to add a reference to the Celery app within the Request struct. That way you could write your task like this:

#[celery::task(bind = true)]
async fn create_more_tasks(task: &Self) {
    task.request.app.send_task(other_task::new()).await?;
}

I can work on this later this week unless someone else wants to tackle it.

epwalsh avatar Feb 08 '21 17:02 epwalsh

Does it work now ?

bingryan avatar Jul 14 '21 02:07 bingryan

How could we help on this part?

yageek avatar Jul 26 '21 13:07 yageek

@yageek are interested in trying to implement my proposed solution above? I can try to provide more guidance if you need it.

epwalsh avatar Aug 05 '21 22:08 epwalsh

I would be interested on more insights. I tried some changes but I did not succeed.

The difficulty I had was to carry the generic 'Broker' element from the 'Celery' struct into the 'Request' type and the 'Message' type along the way.

yageek avatar Aug 06 '21 04:08 yageek

Hmm maybe we need to make a trait (let's call it CeleryBase or something) that represents a celery app (the Celery struct would impl this trait). This trait would include the send_task method of course. Then the Request struct could hold a Box<&dyn CeleryBase>.

epwalsh avatar Aug 06 '21 15:08 epwalsh

Glas to hear it 😛 I tried using a trait also for the Celery (actually named it Dispatcher) and failed to implement it using static dispatch. I'll give another try using dynamic dispatch/trait object.

yageek avatar Aug 06 '21 16:08 yageek

Very much looking forward to someone to complete it :wave:

bingryan avatar Aug 08 '21 13:08 bingryan

@epwalsh do you have some tips about how to deal with trait object and the generics used inside the send_task method?

yageek avatar Aug 08 '21 13:08 yageek

@yageek are you running into a specific compiler error? If you've already started working on a branch, could you open up a draft PR so I can take a look?

epwalsh avatar Aug 09 '21 15:08 epwalsh

@epwalsh I made several attempts but I can not succeed to deal with either dynamic/static dispatch :(

Using aBox<&dyn CeleryBase>. in Request does not work because of the generics (See Object safety

I'm trying another approach implying spreading around the Dispatcher type but this require me at some point to create self-referential struct and I am too noob with Pin.

yageek avatar Sep 07 '21 19:09 yageek

Hmm I see :/. I'll take a stab at this as soon as I find some time.

epwalsh avatar Sep 13 '21 16:09 epwalsh

@epwalsh is there a working workaround for this nowadays? 0.4.0 was released but this issue is still open so i'm guessing not?

lynch-a avatar Feb 09 '23 00:02 lynch-a

@lynch-a I'm not aware of any workarounds yet :(

epwalsh avatar Feb 09 '23 00:02 epwalsh