Sure, a task is just a function you need to call to start and if you've got a task_id
then you can revoke a task. More on that explain in this answer
I've got an example of starting a task which updates a search index.
@app.task
def update_search():
""" Update haystack """
try:
update_index.Command().handle(
remove=True, interactive=False, verbosity=2
)
except TransportError as e:
logger.error(e)
This is called from a view which is accessed using an action link in the admin;
class UpdateSearchView(View):
"""
View called from the admin that updates search indexes.
Checks the requesting user is a super-user before proceeding.
"""
admin_index = reverse_lazy('admin:index')
@staticmethod
def _update_search(request):
"""Update search index"""
update_search.delay()
messages.success(
request, _('Search index is now scheduled to be updated')
)
def get(self, request, *args, **kwargs):
"""Update search, re-direct back the the referring URL"""
if request.user.is_anonymous() or not request.user.is_superuser:
raise PermissionDenied
self._update_search(request)
# redirect back to the current page (of index if there is no
# current page)
return HttpResponseRedirect(request.GET.get('next', self.admin_index))