Merge pull request #161 from sadnub/develop
make run once task run as soon as agent is online
This commit is contained in:
commit
cb71319ff0
|
@ -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",
|
||||
],
|
||||
)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue