Skip to content

Commit

Permalink
increase timeout and change logger
Browse files Browse the repository at this point in the history
  • Loading branch information
wh1te909 committed Nov 22, 2023
1 parent 3766fb1 commit 13e4b1a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
50 changes: 20 additions & 30 deletions api/tacticalrmm/autotasks/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import logging
import random
import string
from contextlib import suppress
Expand All @@ -13,12 +14,11 @@
from django.utils import timezone as djangotime

from core.utils import get_core_settings
from logs.models import BaseAuditModel, DebugLog
from logs.models import BaseAuditModel
from tacticalrmm.constants import (
FIELDS_TRIGGER_TASK_UPDATE_AGENT,
POLICY_TASK_FIELDS_TO_COPY,
AlertSeverity,
DebugLogType,
TaskStatus,
TaskSyncStatus,
TaskType,
Expand All @@ -45,6 +45,9 @@ def generate_task_name() -> str:
return "TacticalRMM_" + "".join(random.choice(chars) for i in range(35))


logger = logging.getLogger("trmm")


class AutomatedTask(BaseAuditModel):
objects = PermissionQuerySet.as_manager()

Expand Down Expand Up @@ -333,25 +336,22 @@ def create_task_on_agent(self, agent: "Optional[Agent]" = None) -> str:
"func": "schedtask",
"schedtaskpayload": self.generate_nats_task_payload(),
}
logger.debug(nats_data)

r = asyncio.run(task_result.agent.nats_cmd(nats_data, timeout=5))
r = asyncio.run(task_result.agent.nats_cmd(nats_data, timeout=10))

if r != "ok":
task_result.sync_status = TaskSyncStatus.INITIAL
task_result.save(update_fields=["sync_status"])
DebugLog.warning(
agent=agent,
log_type=DebugLogType.AGENT_ISSUES,
message=f"Unable to create scheduled task {self.name} on {task_result.agent.hostname}. It will be created when the agent checks in.",
logger.error(
f"Unable to create scheduled task {self.name} on {task_result.agent.hostname}: {r}"
)
return "timeout"
else:
task_result.sync_status = TaskSyncStatus.SYNCED
task_result.save(update_fields=["sync_status"])
DebugLog.info(
agent=agent,
log_type=DebugLogType.AGENT_ISSUES,
message=f"{task_result.agent.hostname} task {self.name} was successfully created",
logger.info(
f"{task_result.agent.hostname} task {self.name} was successfully created."
)

return "ok"
Expand All @@ -372,25 +372,22 @@ def modify_task_on_agent(self, agent: "Optional[Agent]" = None) -> str:
"func": "schedtask",
"schedtaskpayload": self.generate_nats_task_payload(),
}
logger.debug(nats_data)

r = asyncio.run(task_result.agent.nats_cmd(nats_data, timeout=5))
r = asyncio.run(task_result.agent.nats_cmd(nats_data, timeout=10))

if r != "ok":
task_result.sync_status = TaskSyncStatus.NOT_SYNCED
task_result.save(update_fields=["sync_status"])
DebugLog.warning(
agent=agent,
log_type=DebugLogType.AGENT_ISSUES,
message=f"Unable to modify scheduled task {self.name} on {task_result.agent.hostname}({task_result.agent.agent_id}). It will try again on next agent checkin",
logger.error(
f"Unable to modify scheduled task {self.name} on {task_result.agent.hostname}: {r}"
)
return "timeout"
else:
task_result.sync_status = TaskSyncStatus.SYNCED
task_result.save(update_fields=["sync_status"])
DebugLog.info(
agent=agent,
log_type=DebugLogType.AGENT_ISSUES,
message=f"{task_result.agent.hostname} task {self.name} was successfully modified",
logger.info(
f"{task_result.agent.hostname} task {self.name} was successfully modified."
)

return "ok"
Expand Down Expand Up @@ -419,20 +416,13 @@ def delete_task_on_agent(self, agent: "Optional[Agent]" = None) -> str:
with suppress(DatabaseError):
task_result.save(update_fields=["sync_status"])

DebugLog.warning(
agent=agent,
log_type=DebugLogType.AGENT_ISSUES,
message=f"{task_result.agent.hostname} task {self.name} will be deleted on next checkin",
logger.error(
f"Unable to delete task {self.name} on {task_result.agent.hostname}: {r}"
)
return "timeout"
else:
self.delete()
DebugLog.info(
agent=agent,
log_type=DebugLogType.AGENT_ISSUES,
message=f"{task_result.agent.hostname}({task_result.agent.agent_id}) task {self.name} was deleted",
)

logger.info(f"{task_result.agent.hostname} task {self.name} was deleted.")
return "ok"

def run_win_task(self, agent: "Optional[Agent]" = None) -> str:
Expand Down
2 changes: 2 additions & 0 deletions api/tacticalrmm/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ async def _handle_task_on_agent(
f"Unable to {action} scheduled task {task.name} on {hostname}: {r}"
)
else:
task_name = task.name
await task.adelete()
logger.info(f"{hostname} task {task_name} was deleted.")

async def _run():
opts = setup_nats_options()
Expand Down

0 comments on commit 13e4b1a

Please sign in to comment.