diff --git a/api/tacticalrmm/autotasks/tasks.py b/api/tacticalrmm/autotasks/tasks.py index d5583786..a2a33822 100644 --- a/api/tacticalrmm/autotasks/tasks.py +++ b/api/tacticalrmm/autotasks/tasks.py @@ -1,7 +1,8 @@ from loguru import logger from tacticalrmm.celery import app from django.conf import settings -from datetime import datetime as dt +import pytz +from django.utils import timezone as djangotime from .models import AutomatedTask from logs.models import PendingAction @@ -46,6 +47,16 @@ 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).replace( + tzinfo=pytz.utc + ) + djangotime.timedelta(minutes=5) + task.save() + r = task.agent.salt_api_cmd( timeout=20, func="task.create_task", @@ -61,6 +72,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)