Set specific group of Django user as celery worker












0














I need to make a specific group of Django user(group : delivery person) as celery worker. Whenever any registered django user from that specific group logs in, they can pick task from the celery queue and complete. As soon as one user completes a task, it must be dequeued from the queue and asynchronously it should not be visible to the next logged in user from that group.










share|improve this question






















  • You can't apply absolute celery in such case, you need to put these task in database table's queue and then apply celery and dequeue those from table's row / column flag. Even I would suggest don't use celery at all in your case that will also work.
    – Anup Yadav
    Nov 21 '18 at 7:24










  • Ok.. I'm new with Django, so was wondering if I could make a user as celery worker
    – boom_itsme
    Nov 21 '18 at 8:01
















0














I need to make a specific group of Django user(group : delivery person) as celery worker. Whenever any registered django user from that specific group logs in, they can pick task from the celery queue and complete. As soon as one user completes a task, it must be dequeued from the queue and asynchronously it should not be visible to the next logged in user from that group.










share|improve this question






















  • You can't apply absolute celery in such case, you need to put these task in database table's queue and then apply celery and dequeue those from table's row / column flag. Even I would suggest don't use celery at all in your case that will also work.
    – Anup Yadav
    Nov 21 '18 at 7:24










  • Ok.. I'm new with Django, so was wondering if I could make a user as celery worker
    – boom_itsme
    Nov 21 '18 at 8:01














0












0








0







I need to make a specific group of Django user(group : delivery person) as celery worker. Whenever any registered django user from that specific group logs in, they can pick task from the celery queue and complete. As soon as one user completes a task, it must be dequeued from the queue and asynchronously it should not be visible to the next logged in user from that group.










share|improve this question













I need to make a specific group of Django user(group : delivery person) as celery worker. Whenever any registered django user from that specific group logs in, they can pick task from the celery queue and complete. As soon as one user completes a task, it must be dequeued from the queue and asynchronously it should not be visible to the next logged in user from that group.







django celery django-celery django-middleware






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 7:15









boom_itsmeboom_itsme

318




318












  • You can't apply absolute celery in such case, you need to put these task in database table's queue and then apply celery and dequeue those from table's row / column flag. Even I would suggest don't use celery at all in your case that will also work.
    – Anup Yadav
    Nov 21 '18 at 7:24










  • Ok.. I'm new with Django, so was wondering if I could make a user as celery worker
    – boom_itsme
    Nov 21 '18 at 8:01


















  • You can't apply absolute celery in such case, you need to put these task in database table's queue and then apply celery and dequeue those from table's row / column flag. Even I would suggest don't use celery at all in your case that will also work.
    – Anup Yadav
    Nov 21 '18 at 7:24










  • Ok.. I'm new with Django, so was wondering if I could make a user as celery worker
    – boom_itsme
    Nov 21 '18 at 8:01
















You can't apply absolute celery in such case, you need to put these task in database table's queue and then apply celery and dequeue those from table's row / column flag. Even I would suggest don't use celery at all in your case that will also work.
– Anup Yadav
Nov 21 '18 at 7:24




You can't apply absolute celery in such case, you need to put these task in database table's queue and then apply celery and dequeue those from table's row / column flag. Even I would suggest don't use celery at all in your case that will also work.
– Anup Yadav
Nov 21 '18 at 7:24












Ok.. I'm new with Django, so was wondering if I could make a user as celery worker
– boom_itsme
Nov 21 '18 at 8:01




Ok.. I'm new with Django, so was wondering if I could make a user as celery worker
– boom_itsme
Nov 21 '18 at 8:01












1 Answer
1






active

oldest

votes


















0














As @Anup Yadav noted, Celery is not a good fit for this use case. Storing your tasks in the database works fine, you just have to lock the row using select_for_update to ensure that a task can't be picked twice. Something like this should work:



class Task(models.Model):
is_available = models.BooleanField(default=True)

def pick_task():
"""
Selects a task and marks it as unavailable.

Returns Task or None, if no Task is available.
"""
task = Task.objects.select_for_update().filter(is_available=True).first()
if task is not None:
task.is_available = False
task.save()
return task


Checking if the user has the right group would be done in the view. Note that pick_task should probably be a method of a custom manager.






share|improve this answer





















  • I will try this way. For making it asynchronous across all users I possibly can use websockets.
    – boom_itsme
    Nov 21 '18 at 8:02










  • what if a user declines a task, and I set is_available back to True but it must not be visible to him? I mean, whever a user declines a task it will be available for other users to accept the task but not for the specific user whoever declined it.
    – boom_itsme
    Nov 22 '18 at 3:32










  • You have to store that information somewhere. The easiest way would probably be a field rejected_by = models.ManyToMany(get_user_model()), but if this is the best way depends on your exact requirements. This really should be a separate question.
    – Daniel Hepper
    Nov 22 '18 at 10:25










  • Alright. Thanks for the suggestion.
    – boom_itsme
    Nov 22 '18 at 11:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406987%2fset-specific-group-of-django-user-as-celery-worker%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














As @Anup Yadav noted, Celery is not a good fit for this use case. Storing your tasks in the database works fine, you just have to lock the row using select_for_update to ensure that a task can't be picked twice. Something like this should work:



class Task(models.Model):
is_available = models.BooleanField(default=True)

def pick_task():
"""
Selects a task and marks it as unavailable.

Returns Task or None, if no Task is available.
"""
task = Task.objects.select_for_update().filter(is_available=True).first()
if task is not None:
task.is_available = False
task.save()
return task


Checking if the user has the right group would be done in the view. Note that pick_task should probably be a method of a custom manager.






share|improve this answer





















  • I will try this way. For making it asynchronous across all users I possibly can use websockets.
    – boom_itsme
    Nov 21 '18 at 8:02










  • what if a user declines a task, and I set is_available back to True but it must not be visible to him? I mean, whever a user declines a task it will be available for other users to accept the task but not for the specific user whoever declined it.
    – boom_itsme
    Nov 22 '18 at 3:32










  • You have to store that information somewhere. The easiest way would probably be a field rejected_by = models.ManyToMany(get_user_model()), but if this is the best way depends on your exact requirements. This really should be a separate question.
    – Daniel Hepper
    Nov 22 '18 at 10:25










  • Alright. Thanks for the suggestion.
    – boom_itsme
    Nov 22 '18 at 11:41
















0














As @Anup Yadav noted, Celery is not a good fit for this use case. Storing your tasks in the database works fine, you just have to lock the row using select_for_update to ensure that a task can't be picked twice. Something like this should work:



class Task(models.Model):
is_available = models.BooleanField(default=True)

def pick_task():
"""
Selects a task and marks it as unavailable.

Returns Task or None, if no Task is available.
"""
task = Task.objects.select_for_update().filter(is_available=True).first()
if task is not None:
task.is_available = False
task.save()
return task


Checking if the user has the right group would be done in the view. Note that pick_task should probably be a method of a custom manager.






share|improve this answer





















  • I will try this way. For making it asynchronous across all users I possibly can use websockets.
    – boom_itsme
    Nov 21 '18 at 8:02










  • what if a user declines a task, and I set is_available back to True but it must not be visible to him? I mean, whever a user declines a task it will be available for other users to accept the task but not for the specific user whoever declined it.
    – boom_itsme
    Nov 22 '18 at 3:32










  • You have to store that information somewhere. The easiest way would probably be a field rejected_by = models.ManyToMany(get_user_model()), but if this is the best way depends on your exact requirements. This really should be a separate question.
    – Daniel Hepper
    Nov 22 '18 at 10:25










  • Alright. Thanks for the suggestion.
    – boom_itsme
    Nov 22 '18 at 11:41














0












0








0






As @Anup Yadav noted, Celery is not a good fit for this use case. Storing your tasks in the database works fine, you just have to lock the row using select_for_update to ensure that a task can't be picked twice. Something like this should work:



class Task(models.Model):
is_available = models.BooleanField(default=True)

def pick_task():
"""
Selects a task and marks it as unavailable.

Returns Task or None, if no Task is available.
"""
task = Task.objects.select_for_update().filter(is_available=True).first()
if task is not None:
task.is_available = False
task.save()
return task


Checking if the user has the right group would be done in the view. Note that pick_task should probably be a method of a custom manager.






share|improve this answer












As @Anup Yadav noted, Celery is not a good fit for this use case. Storing your tasks in the database works fine, you just have to lock the row using select_for_update to ensure that a task can't be picked twice. Something like this should work:



class Task(models.Model):
is_available = models.BooleanField(default=True)

def pick_task():
"""
Selects a task and marks it as unavailable.

Returns Task or None, if no Task is available.
"""
task = Task.objects.select_for_update().filter(is_available=True).first()
if task is not None:
task.is_available = False
task.save()
return task


Checking if the user has the right group would be done in the view. Note that pick_task should probably be a method of a custom manager.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 7:39









Daniel HepperDaniel Hepper

16.9k44660




16.9k44660












  • I will try this way. For making it asynchronous across all users I possibly can use websockets.
    – boom_itsme
    Nov 21 '18 at 8:02










  • what if a user declines a task, and I set is_available back to True but it must not be visible to him? I mean, whever a user declines a task it will be available for other users to accept the task but not for the specific user whoever declined it.
    – boom_itsme
    Nov 22 '18 at 3:32










  • You have to store that information somewhere. The easiest way would probably be a field rejected_by = models.ManyToMany(get_user_model()), but if this is the best way depends on your exact requirements. This really should be a separate question.
    – Daniel Hepper
    Nov 22 '18 at 10:25










  • Alright. Thanks for the suggestion.
    – boom_itsme
    Nov 22 '18 at 11:41


















  • I will try this way. For making it asynchronous across all users I possibly can use websockets.
    – boom_itsme
    Nov 21 '18 at 8:02










  • what if a user declines a task, and I set is_available back to True but it must not be visible to him? I mean, whever a user declines a task it will be available for other users to accept the task but not for the specific user whoever declined it.
    – boom_itsme
    Nov 22 '18 at 3:32










  • You have to store that information somewhere. The easiest way would probably be a field rejected_by = models.ManyToMany(get_user_model()), but if this is the best way depends on your exact requirements. This really should be a separate question.
    – Daniel Hepper
    Nov 22 '18 at 10:25










  • Alright. Thanks for the suggestion.
    – boom_itsme
    Nov 22 '18 at 11:41
















I will try this way. For making it asynchronous across all users I possibly can use websockets.
– boom_itsme
Nov 21 '18 at 8:02




I will try this way. For making it asynchronous across all users I possibly can use websockets.
– boom_itsme
Nov 21 '18 at 8:02












what if a user declines a task, and I set is_available back to True but it must not be visible to him? I mean, whever a user declines a task it will be available for other users to accept the task but not for the specific user whoever declined it.
– boom_itsme
Nov 22 '18 at 3:32




what if a user declines a task, and I set is_available back to True but it must not be visible to him? I mean, whever a user declines a task it will be available for other users to accept the task but not for the specific user whoever declined it.
– boom_itsme
Nov 22 '18 at 3:32












You have to store that information somewhere. The easiest way would probably be a field rejected_by = models.ManyToMany(get_user_model()), but if this is the best way depends on your exact requirements. This really should be a separate question.
– Daniel Hepper
Nov 22 '18 at 10:25




You have to store that information somewhere. The easiest way would probably be a field rejected_by = models.ManyToMany(get_user_model()), but if this is the best way depends on your exact requirements. This really should be a separate question.
– Daniel Hepper
Nov 22 '18 at 10:25












Alright. Thanks for the suggestion.
– boom_itsme
Nov 22 '18 at 11:41




Alright. Thanks for the suggestion.
– boom_itsme
Nov 22 '18 at 11:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406987%2fset-specific-group-of-django-user-as-celery-worker%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga