1

When I send a programmed Task from my view in Django, I need to obtain immediately the task.id if I need to revoke in a second moment.

For example, my view:

@login_required
def program_task(request, pk):
    member = get_object_or_404(Post_Blog, pk=pk)
    if request.method == "POST":
        form = PostProgrammaForm(request.POST)
        if form.is_valid():
            action = form.save(commit=False)
            action.account_id = member.pk
            action.programmed = 1
            year= action.dataprogrammato.year
            month= action.dataprogrammato.month
            day= action.dataprogrammato.day
            hourz = action.oraprogrammato.hour
            hour= oraz - 2 ##this is for timezone :D
            minute= action.oraprogrammato.minute
            quando = datetime(year, month, day, hour, minute)
            action.save()

            if action.programmed == True:
                tasksend.apply_async(args=(action.id), eta=quando)
                ### here it send task and work, but I need to know here the task.id
            return redirect('blog_action', pk=member.pk)
    else:
        form = PostProgrammaForm(request.POST)
    return render(request, 'FBIsystem/post_program.html', {'form': form, 'member':member})

now, my celery py is somethig like this:

app = Celery()
@app.task(bind=True)
def tasksend(self, action_id):
    ###do somethig

everything work, I save form and I send task but... if I program a post for tomorrow and i need to revoke task before it run how can i do?

Please help Thank you

JuConte
  • 513
  • 2
  • 7
  • 18

1 Answers1

2

apply_async returns a task instance of celery.result.AsyncResult which has id

task = tasksend.apply_async(args=(action.id), eta=quando)
print(task.id)
Mohit Solanki
  • 2,122
  • 12
  • 20