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

Sending message in bulk raising GCM error when it should not #683

Open
sauravkhakurel opened this issue Jul 3, 2023 · 3 comments
Open

Comments

@sauravkhakurel
Copy link

sauravkhakurel commented Jul 3, 2023

The docs of the package state that GCM Error is not raised when using bulk notifications. But the error is raised when I send bulk notifications using the following code from which I am not able to calculate the number of devices that the notification was delivered to.

devices = GCMDevice.objects.filter(user__in=users)
result = devices.send_message(
            notification.notification_text, tag=uuid4().hex, extra=extra
        )

Is there any possible fix for this? Thanks

@jamaalscarlett
Copy link
Member

@sauravkhakurel can you please post the error you are seeing?

@jamaalscarlett
Copy link
Member

@sauravkhakurel bump

@sauravkhakurel
Copy link
Author

sauravkhakurel commented Sep 30, 2023

Here is the error log from the Huey container. I have attached the full log as well.

INFO 2023-09-30 14:45:09,513 api 30 140351916693312 Executing super_krishak.notifications.tasks.notify_users_by_participation: ecd22786-0623-4108-90d5-018ed8cbeb0b @2023-09-30 09:00:00
ERROR 2023-09-30 14:45:09,525 tasks 31 140351916693312 GCMError occurred while sending message.

The code that is used for task enqueuing and execution

@huey.db_task(retries=0)
def notify_users_by_participation(
    title, extra, quiz_id=None, participation_status=None
):
    users = User.objects.all()

    if quiz_id and participation_status:
        if participation_status == QuizParticipationStatus.PARTICIPATED:
            users = users.filter(user_quiz_answers__quiz_id=quiz_id).distinct()
        elif participation_status == QuizParticipationStatus.NOT_PARTICIPATED:
            users = users.exclude(user_quiz_answers__quiz_id=quiz_id).distinct()

    devices = GCMDevice.objects.filter(user__in=users).distinct("user")

    try:
        devices.send_message(title, extra=extra)
    except GCMError:
        logging.error("GCMError occurred while sending message.")
        pass
    except Exception as e:
        logging.error(f"Error sending message: {e}")
        pass

Function to schedule daily notifications

@huey.periodic_task(crontab(hour=3))
def schedule_daily_notifications():
    notifications = ScheduledNotification.objects.all()
    now = timezone.now()
    quiz = Quiz.ongoing().first()

    if not quiz:
        return

    for notification in notifications:
        push_time = notification.push_time
        eta = now.replace(
            hour=push_time.hour, minute=push_time.minute, second=0, microsecond=0
        )
        extra = {
            "type": "quiz",
            "quiz_id": quiz.id,
            "status": notification.participation_status,
        }

        notify_users_by_participation.schedule(
            args=(
                notification.notification_text,
                extra,
                quiz.id,
                notification.participation_status,
            ),
            eta=eta,
        )

Another unusual behavior that I am getting is, even if there are only 4 notifications in the ScheduledNotifications model. The task gets enqueued and executed automatically multiple times within a very short amount of time. I still am unable to pinpoint the cause of this unexpected behavior. If there is something wrong with my code then please do point that out as well. I have attached the Huey logs as well if you want to have a look. Thanks
huey-logs.odt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants