From 9535a9fa3f675bcf86e412d70e1d1b3faff64617 Mon Sep 17 00:00:00 2001 From: sadnub Date: Sun, 1 Nov 2020 10:53:14 -0500 Subject: [PATCH 1/4] make run once task run as soon as agent is online --- api/tacticalrmm/autotasks/tasks.py | 1 + api/tacticalrmm/core/tasks.py | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/api/tacticalrmm/autotasks/tasks.py b/api/tacticalrmm/autotasks/tasks.py index d5583786..ad2002a1 100644 --- a/api/tacticalrmm/autotasks/tasks.py +++ b/api/tacticalrmm/autotasks/tasks.py @@ -61,6 +61,7 @@ def create_win_task_schedule(pk, pending_action=False): f'start_time="{task.run_time_date.strftime("%H:%M")}"', "ac_only=False", "stop_if_on_batteries=False", + "start_when_available=True", ], ) diff --git a/api/tacticalrmm/core/tasks.py b/api/tacticalrmm/core/tasks.py index b8f2a4cf..54f03e16 100644 --- a/api/tacticalrmm/core/tasks.py +++ b/api/tacticalrmm/core/tasks.py @@ -25,8 +25,9 @@ def core_maintenance_tasks(): # cleanup expired runonce tasks tasks = AutomatedTask.objects.filter( - task_type="runonce", remove_if_not_scheduled=True - ) + task_type="runonce", + remove_if_not_scheduled=True, + ).exclude(last_run=None) for task in tasks: agent_tz = pytz.timezone(task.agent.timezone) From 9fdbae986c299ca7b8ce3626fead1ff25bf145b3 Mon Sep 17 00:00:00 2001 From: sadnub Date: Sun, 1 Nov 2020 11:33:47 -0500 Subject: [PATCH 2/4] handle task times in the past --- api/tacticalrmm/autotasks/tasks.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/api/tacticalrmm/autotasks/tasks.py b/api/tacticalrmm/autotasks/tasks.py index ad2002a1..25d14eed 100644 --- a/api/tacticalrmm/autotasks/tasks.py +++ b/api/tacticalrmm/autotasks/tasks.py @@ -2,6 +2,9 @@ from loguru import logger from tacticalrmm.celery import app from django.conf import settings from datetime import datetime as dt +from datetime import timedelta +import pytz +from django.utils import timezone as djangotime from .models import AutomatedTask from logs.models import PendingAction @@ -46,6 +49,14 @@ def create_win_task_schedule(pk, pending_action=False): elif task.task_type == "runonce": + # check if scheduled time is in the past + agent_tz = pytz.timezone(task.agent.timezone) + task_time_utc = task.run_time_date.replace(tzinfo=agent_tz).astimezone(pytz.utc) + now = djangotime.now() + if task_time_utc < now: + task.run_time_date = now.astimezone(agent_tz) + timedelta(minutes=5) + task.save() + r = task.agent.salt_api_cmd( timeout=20, func="task.create_task", From 721ce8f91af7be7f518d0f07f5641645a819fc1c Mon Sep 17 00:00:00 2001 From: sadnub Date: Sun, 1 Nov 2020 16:26:37 -0500 Subject: [PATCH 3/4] fix time zone issues --- api/tacticalrmm/autotasks/tasks.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/tacticalrmm/autotasks/tasks.py b/api/tacticalrmm/autotasks/tasks.py index 25d14eed..6aff8d18 100644 --- a/api/tacticalrmm/autotasks/tasks.py +++ b/api/tacticalrmm/autotasks/tasks.py @@ -54,7 +54,9 @@ def create_win_task_schedule(pk, pending_action=False): task_time_utc = task.run_time_date.replace(tzinfo=agent_tz).astimezone(pytz.utc) now = djangotime.now() if task_time_utc < now: - task.run_time_date = now.astimezone(agent_tz) + timedelta(minutes=5) + task.run_time_date = now.astimezone(agent_tz).replace( + tzinfo=None + ) + timedelta(minutes=5) task.save() r = task.agent.salt_api_cmd( From 7b7164a9a24c1a590ed49c54354271ba7ecb6735 Mon Sep 17 00:00:00 2001 From: sadnub Date: Sun, 1 Nov 2020 17:16:58 -0500 Subject: [PATCH 4/4] fix warning message about tz --- api/tacticalrmm/autotasks/tasks.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/api/tacticalrmm/autotasks/tasks.py b/api/tacticalrmm/autotasks/tasks.py index 6aff8d18..a2a33822 100644 --- a/api/tacticalrmm/autotasks/tasks.py +++ b/api/tacticalrmm/autotasks/tasks.py @@ -1,8 +1,6 @@ from loguru import logger from tacticalrmm.celery import app from django.conf import settings -from datetime import datetime as dt -from datetime import timedelta import pytz from django.utils import timezone as djangotime @@ -55,8 +53,8 @@ def create_win_task_schedule(pk, pending_action=False): now = djangotime.now() if task_time_utc < now: task.run_time_date = now.astimezone(agent_tz).replace( - tzinfo=None - ) + timedelta(minutes=5) + tzinfo=pytz.utc + ) + djangotime.timedelta(minutes=5) task.save() r = task.agent.salt_api_cmd(