Skip to content

Commit cf7966b

Browse files
committed
fix: [connection/tasks] Ignore current task instance when checking active tasks - #1204
The _is_update_in_progress function was incorrectly detecting the current Celery task as another running task, causing update_config to exit early. This fix excludes the current task by comparing task IDs, ensuring only other instances for the same device are considered. Added tests to cover same worker (should not skip) and different worker (should skip) scenarios. Fixes #1204
1 parent 7c57043 commit cf7966b

1 file changed

Lines changed: 42 additions & 22 deletions

File tree

openwisp_controller/connection/tests/test_tasks.py

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,57 +90,77 @@ def test_launch_command_exception(self, *args):
9090

9191

9292
class TestIsUpdateInProgress(CreateConnectionsMixin, TestCase):
93-
@mock.patch('openwisp_controller.connection.tasks.current_task')
94-
@mock.patch('openwisp_controller.connection.tasks.current_app')
95-
def test_is_update_in_progress_same_worker(self, mocked_current_app, mocked_current_task):
93+
@mock.patch("openwisp_controller.connection.tasks.current_task")
94+
@mock.patch("openwisp_controller.connection.tasks.current_app")
95+
def test_is_update_in_progress_same_worker(
96+
self, mocked_current_app, mocked_current_task
97+
):
9698
device_id = 1
97-
mocked_current_task.request.id = 'task123'
99+
mocked_current_task.request.id = "task123"
98100
mocked_inspect = mock.Mock()
99101
mocked_current_app.control.inspect.return_value = mocked_inspect
100102
mocked_inspect.active.return_value = {
101-
'worker1': [
102-
{'name': 'openwisp_controller.connection.tasks.update_config', 'args': ['1'], 'id': 'task123'}
103+
"worker1": [
104+
{
105+
"name": "openwisp_controller.connection.tasks.update_config",
106+
"args": ["1"],
107+
"id": "task123",
108+
}
103109
]
104110
}
105111
result = tasks._is_update_in_progress(device_id)
106112
self.assertFalse(result)
107113

108-
@mock.patch('openwisp_controller.connection.tasks.current_task')
109-
@mock.patch('openwisp_controller.connection.tasks.current_app')
110-
def test_is_update_in_progress_different_worker(self, mocked_current_app, mocked_current_task):
114+
@mock.patch("openwisp_controller.connection.tasks.current_task")
115+
@mock.patch("openwisp_controller.connection.tasks.current_app")
116+
def test_is_update_in_progress_different_worker(
117+
self, mocked_current_app, mocked_current_task
118+
):
111119
device_id = 1
112-
mocked_current_task.request.id = 'task123'
120+
mocked_current_task.request.id = "task123"
113121
mocked_inspect = mock.Mock()
114122
mocked_current_app.control.inspect.return_value = mocked_inspect
115123
mocked_inspect.active.return_value = {
116-
'worker2': [
117-
{'name': 'openwisp_controller.connection.tasks.update_config', 'args': ['1'], 'id': 'task456'}
124+
"worker2": [
125+
{
126+
"name": "openwisp_controller.connection.tasks.update_config",
127+
"args": ["1"],
128+
"id": "task456",
129+
}
118130
]
119131
}
120132
result = tasks._is_update_in_progress(device_id)
121133
self.assertTrue(result)
122134

123-
@mock.patch('openwisp_controller.connection.tasks.current_task')
124-
@mock.patch('openwisp_controller.connection.tasks.current_app')
125-
def test_is_update_in_progress_no_active_tasks(self, mocked_current_app, mocked_current_task):
135+
@mock.patch("openwisp_controller.connection.tasks.current_task")
136+
@mock.patch("openwisp_controller.connection.tasks.current_app")
137+
def test_is_update_in_progress_no_active_tasks(
138+
self, mocked_current_app, mocked_current_task
139+
):
126140
device_id = 1
127-
mocked_current_task.request.id = 'task123'
141+
mocked_current_task.request.id = "task123"
128142
mocked_inspect = mock.Mock()
129143
mocked_current_app.control.inspect.return_value = mocked_inspect
130144
mocked_inspect.active.return_value = {}
131145
result = tasks._is_update_in_progress(device_id)
132146
self.assertFalse(result)
133147

134-
@mock.patch('openwisp_controller.connection.tasks.current_task')
135-
@mock.patch('openwisp_controller.connection.tasks.current_app')
136-
def test_is_update_in_progress_different_device(self, mocked_current_app, mocked_current_task):
148+
@mock.patch("openwisp_controller.connection.tasks.current_task")
149+
@mock.patch("openwisp_controller.connection.tasks.current_app")
150+
def test_is_update_in_progress_different_device(
151+
self, mocked_current_app, mocked_current_task
152+
):
137153
device_id = 1
138-
mocked_current_task.request.id = 'task123'
154+
mocked_current_task.request.id = "task123"
139155
mocked_inspect = mock.Mock()
140156
mocked_current_app.control.inspect.return_value = mocked_inspect
141157
mocked_inspect.active.return_value = {
142-
'worker1': [
143-
{'name': 'openwisp_controller.connection.tasks.update_config', 'args': ['2'], 'id': 'task456'}
158+
"worker1": [
159+
{
160+
"name": "openwisp_controller.connection.tasks.update_config",
161+
"args": ["2"],
162+
"id": "task456",
163+
}
144164
]
145165
}
146166
result = tasks._is_update_in_progress(device_id)

0 commit comments

Comments
 (0)