Skip to content

Commit 310648f

Browse files
committed
Pass through args and kwargs
1 parent 276e299 commit 310648f

File tree

5 files changed

+47
-4
lines changed

5 files changed

+47
-4
lines changed

ansible_base/task/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ def get_config():
1313
"pg_notify": psycopg_params,
1414
"channels": ["dab_broadcast"],
1515
},
16-
"scheduled": {},
16+
"scheduled": {
17+
"ansible_base.task.tasks.run_task_from_queue": {"schedule": 60},
18+
"ansible_base.task.tasks.manage_lost_tasks": {"schedule": 60*10}
19+
},
1720
},
1821
"pool": {"max_workers": 4}, # TODO: to settings
1922
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 4.2.16 on 2025-02-18 18:52
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('dab_tas', '0002_task_wrapper_uuid_alter_task_uuid'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='task',
15+
name='args',
16+
field=models.JSONField(blank=True, default=dict, help_text='The arguments for the task run.'),
17+
),
18+
migrations.AddField(
19+
model_name='task',
20+
name='kwargs',
21+
field=models.JSONField(blank=True, default=dict, help_text='The keyword arguments for the task run.'),
22+
),
23+
]

ansible_base/task/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ class Task(models.Model):
3434
)
3535
name = models.TextField(help_text=_('Importable path for class or method'))
3636

37+
args = models.JSONField(
38+
default=dict, null=False, blank=True, help_text=_("The arguments for the task run.")
39+
)
40+
kwargs = models.JSONField(
41+
default=dict, null=False, blank=True, help_text=_("The keyword arguments for the task run.")
42+
)
43+
3744
created = models.DateTimeField(
3845
auto_now_add=True, help_text=_('Time the publisher (submitter) of this task call created it, approximately the time of submission as well')
3946
)

ansible_base/task/publish.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def task_name(self):
1717

1818
def apply_async(self, args=None, kwargs=None):
1919
# this function may allow additional arguments in the future, but not now
20-
Task.objects.create(name=self.task_name)
20+
Task.objects.create(name=self.task_name, args=args, kwargs=kwargs)
2121

2222
# pg_notify message (probably) to wake up
2323
transaction.on_commit(run_task_from_queue.delay)

ansible_base/task/tasks.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
from dispatcher.utils import resolve_callable
44
from dispatcher.publish import task
5+
from dispatcher.control import Control
56

67
from django.db import transaction
7-
from django.utils.timezone import now
8+
from django.utils.timezone import now, timedelta
89

910
from ansible_base.task.models import TASK_STATES, Task
1011

@@ -29,13 +30,22 @@ def run_task_from_queue(dispatcher):
2930

3031
try:
3132
task_callable = resolve_callable(task.name)
32-
task_callable()
33+
task_callable(*task.args, **task.kwargs)
3334
except Exception:
3435
logger.traceback(f'Failed to run and complete {task.name}')
3536

3637
task.delete()
3738

3839

40+
@task(queue='dab_broadcast')
41+
def manage_lost_tasks(grace_period: int = 10):
42+
cutoff_time = now() - timedelta(minutes=grace_period)
43+
ctl = Control('dab_broadcast')
44+
for task in Task.objects.filter(state=TASK_STATES.RUNNING, started_at__lt=cutoff_time).iterator():
45+
running_tasks = ctl.control_with_reply('running', data={'uuid': task.wrapper_uuid})
46+
print(running_tasks)
47+
print(task.wrapper_uuid)
48+
3949
# TODO: Add a "reaper" fallback task, this is currently blocked on dispatcher issue
4050
# https://github.com/ansible/dispatcher/issues/6
4151
# When run_task_from_queue starts the task, the uuid needs to be setup to be discoverable

0 commit comments

Comments
 (0)