[BUG] :checkered_flag: Plugin scheduled tasks: Schedule not updated

This issue has been tracked since 2023-01-21.

Please verify that this bug has NOT been raised before.

  • I checked and didn't find similar issue

Describe the bug*

The scheduling time of a task is not updated after change in the source code.

Steps to Reproduce

I enabled the scheduled tasks plugin from samples
I did some slight modification to the code:

def print_hello():
    now = datetime.now()
    print(now, ": Hello")  # pragma: no cover


def print_world():
    now = datetime.now()
    print(now,": World")  # pragma: no cover

class ScheduledTaskPlugin(ScheduleMixin, SettingsMixin, InvenTreePlugin):

    NAME = "ScheduledTasksPlugin"
    SLUG = "schedule"
    TITLE = "Scheduled Tasks"

    SCHEDULED_TASKS = {
          'hello': {
            'func': 'plugin.samples.integration.scheduled_task.print_hello',
            'schedule': 'I',
            'minutes': 5,
        },
        'world': {
            'func': 'plugin.samples.integration.scheduled_task.print_world',
            'schedule': 'H',
        },
    }

Here is what I get from the log after sorting a bit.

2023-01-20 22:50 : World
2023-01-20 23:51 : World
2023-01-21 00:50 : World
2023-01-21 01:50 : World
2023-01-21 02:50 : World
2023-01-21 04:51 : World
2023-01-21 05:50 : World
2023-01-21 06:51 : World

2023-01-20 22:50 : Hello
2023-01-20 23:36 : Hello
2023-01-21 00:20 : Hello
2023-01-21 01:05 : Hello
2023-01-21 01:50 : Hello
2023-01-21 02:36 : Hello
2023-01-21 03:20 : Hello

So Hello is executed every 45 minutes instead of five. I took a look at the database and found the 45:

schedule

The problem seems to be in ../plugin/base/integration/mixins.py

  def register_tasks(self):
        """Register the tasks with the database."""
        try:
            from django_q.models import Schedule

            for key, task in self.scheduled_tasks.items():

                task_name = self.get_task_name(key)

                if Schedule.objects.filter(name=task_name).exists():
                    # Scheduled task already exists - continue!
                    continue  # pragma: no cover

                logger.info(f"Adding scheduled task '{task_name}'")

                func_name = task['func'].strip()

As I do not see the log message I think create is not executed because it already exists. Or do I miss something?

Michael

Expected behavior

I would expect the scheduling times to change when I edit the source of the plugin.

Deployment Method

  • Docker
  • Bare metal

Version Information

InvenTree Version 0.10.0 dev
Development Version
Commit Hash 4bfc1f7
Commit Date 2023-01-11

Relevant log output

please see above
matmair wrote this answer on 2023-01-23

Dang, this is probably related to #2726, I will take a look.

matmair wrote this answer on 2023-01-23

Thanks for the report @SergeoLacruz

matmair wrote this answer on 2023-01-23

@SergeoLacruz how is your plugin integrated? package or just a file in /plugins . And have you reloaded the server after changing the number?

There is a WIP branch that should work, would be great if you can test if that fixes it for you: #4243

SergeoLacruz wrote this answer on 2023-01-23

Just file. I just edited /src/InvenTree/plugin/samples/integration/scheduled_task.py to try something.
Yes I do a server restart but the for loop in /plugin/base/integration/mixins.py exits with continue.
I can solve this when I disable the plugin in the admin interface and enable it again.

matmair wrote this answer on 2023-01-23

Ok I have a working fix with tests now. we basically ignored changes to existing schedules, with the path it updates them.

The activating/deactivating thing works because the schedules of deactivated plugins get deleted.

SergeoLacruz wrote this answer on 2023-01-23

Thanks for the help :-)
Will give it a try soon

More Details About Repo
Owner Name inventree
Repo Name InvenTree
Full Name inventree/InvenTree
Language Python
Created Date 2017-03-23
Updated Date 2023-03-31
Star Count 2586
Watcher Count 61
Fork Count 411
Issue Count 141

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date