Merge pull request #161 from sadnub/develop

make run once task run as soon as agent is online
This commit is contained in:
wh1te909 2020-11-01 14:26:03 -08:00 committed by GitHub
commit cb71319ff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -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",
],
)

View File

@ -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)