Skip to content

Commit

Permalink
change nats task payload for run once task change
Browse files Browse the repository at this point in the history
  • Loading branch information
sadnub committed Nov 22, 2023
1 parent e3c4a54 commit d1ab69d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 39 deletions.
55 changes: 23 additions & 32 deletions api/tacticalrmm/autotasks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def schedule(self) -> Optional[str]:
weeks = bitweeks_to_string(self.monthly_weeks_of_month)
days = bitdays_to_string(self.run_time_bit_weekdays)
return f"Runs on {months} on {weeks} on {days} at {run_time_nice}"
return None

@property
def fields_that_trigger_task_update_on_agent(self) -> List[str]:
Expand Down Expand Up @@ -236,14 +237,12 @@ def create_policy_task(
task.save()

# agent version >= 1.8.0
def generate_nats_task_payload(
self, agent: "Optional[Agent]" = None, editing: bool = False
) -> Dict[str, Any]:
def generate_nats_task_payload(self) -> Dict[str, Any]:
task = {
"pk": self.pk,
"type": "rmm",
"name": self.win_task_name,
"overwrite_task": editing,
"overwrite_task": True,
"enabled": self.enabled,
"trigger": self.task_type
if self.task_type != TaskType.CHECK_FAILURE
Expand All @@ -257,43 +256,35 @@ def generate_nats_task_payload(
else True,
}

if self.task_type in (
TaskType.RUN_ONCE,
if self.task_type == TaskType.RUN_ONCE and self.random_task_delay:
task["random_delay"] = convert_to_iso_duration(self.random_task_delay)

elif self.task_type in (
TaskType.DAILY,
TaskType.WEEKLY,
TaskType.MONTHLY,
TaskType.MONTHLY_DOW,
):
# set runonce task in future if creating and run_asap_after_missed is set
if (
not editing
and self.task_type == TaskType.RUN_ONCE
and self.run_asap_after_missed
and agent
and self.run_time_date.replace(tzinfo=ZoneInfo(agent.timezone))
< djangotime.now().astimezone(ZoneInfo(agent.timezone))
):
self.run_time_date = (
djangotime.now() + djangotime.timedelta(minutes=5)
).astimezone(ZoneInfo(agent.timezone))

task["start_year"] = int(self.run_time_date.strftime("%Y"))
task["start_month"] = int(self.run_time_date.strftime("%-m"))
task["start_day"] = int(self.run_time_date.strftime("%-d"))
task["start_hour"] = int(self.run_time_date.strftime("%-H"))
task["start_min"] = int(self.run_time_date.strftime("%-M"))
if not self.run_time_date:
self.run_time_date = djangotime.now()

task["start_year"] = self.run_time_date.year
task["start_month"] = self.run_time_date.month
task["start_day"] = self.run_time_date.day
task["start_hour"] = self.run_time_date.hour
task["start_min"] = self.run_time_date.minute

if self.expire_date:
task["expire_year"] = int(self.expire_date.strftime("%Y"))
task["expire_month"] = int(self.expire_date.strftime("%-m"))
task["expire_day"] = int(self.expire_date.strftime("%-d"))
task["expire_hour"] = int(self.expire_date.strftime("%-H"))
task["expire_min"] = int(self.expire_date.strftime("%-M"))
task["expire_year"] = self.expire_date.year
task["expire_month"] = self.expire_date.month
task["expire_day"] = self.expire_date.day
task["expire_hour"] = self.expire_date.hour
task["expire_min"] = self.expire_date.minute

if self.random_task_delay:
task["random_delay"] = convert_to_iso_duration(self.random_task_delay)

if self.task_repetition_interval:
if self.task_repetition_interval and self.task_repetition_duration:
task["repetition_interval"] = convert_to_iso_duration(
self.task_repetition_interval
)
Expand Down Expand Up @@ -341,7 +332,7 @@ def create_task_on_agent(self, agent: "Optional[Agent]" = None) -> str:

nats_data = {
"func": "schedtask",
"schedtaskpayload": self.generate_nats_task_payload(agent),
"schedtaskpayload": self.generate_nats_task_payload(),
}

r = asyncio.run(task_result.agent.nats_cmd(nats_data, timeout=5))
Expand Down Expand Up @@ -380,7 +371,7 @@ def modify_task_on_agent(self, agent: "Optional[Agent]" = None) -> str:

nats_data = {
"func": "schedtask",
"schedtaskpayload": self.generate_nats_task_payload(editing=True),
"schedtaskpayload": self.generate_nats_task_payload(),
}

r = asyncio.run(task_result.agent.nats_cmd(nats_data, timeout=5))
Expand Down
10 changes: 3 additions & 7 deletions api/tacticalrmm/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ def sync_scheduled_tasks(self) -> str:
"create",
task.id,
agent_obj,
task.generate_nats_task_payload(
agent=agent_obj, editing=False
),
task.generate_nats_task_payload(),
agent.agent_id,
agent.hostname,
)
Expand Down Expand Up @@ -209,9 +207,7 @@ def sync_scheduled_tasks(self) -> str:
"modify",
task.id,
agent_obj,
task.generate_nats_task_payload(
agent=None, editing=True
),
task.generate_nats_task_payload(),
agent.agent_id,
agent.hostname,
)
Expand Down Expand Up @@ -278,7 +274,7 @@ async def _handle_task_on_agent(
else:
await task.adelete()

async def _run() -> str | None:
async def _run():
opts = setup_nats_options()
try:
nc = await nats.connect(**opts)
Expand Down

0 comments on commit d1ab69d

Please sign in to comment.