mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 20:16:44 +00:00
Merged run_task code into offload_task function
Added option to force synchronous operation Use that option for update_exchange_rates
This commit is contained in:
parent
1b79ef940e
commit
fbdf11e6e7
@ -51,32 +51,17 @@ def schedule_task(taskname, **kwargs):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def offload_task(taskname, *args, **kwargs):
|
def offload_task(taskname, force_sync=False, *args, **kwargs):
|
||||||
"""
|
|
||||||
Create an AsyncTask.
|
|
||||||
This is different to a 'scheduled' task,
|
|
||||||
in that it only runs once!
|
|
||||||
"""
|
"""
|
||||||
|
First check if the task method pointed
|
||||||
|
by taskname is implemented inside this file.
|
||||||
|
|
||||||
try:
|
Then create an AsyncTask if workers are running.
|
||||||
from django_q.tasks import AsyncTask
|
This is different to a 'scheduled' task,
|
||||||
except (AppRegistryNotReady):
|
in that it only runs once!
|
||||||
logger.warning("Could not offload task - app registry not ready")
|
|
||||||
return
|
|
||||||
|
|
||||||
task = AsyncTask(taskname, *args, **kwargs)
|
If workers are not running or force_sync flag
|
||||||
|
is set then the task is ran synchronously.
|
||||||
task.run()
|
|
||||||
|
|
||||||
|
|
||||||
def run_task(taskname):
|
|
||||||
"""
|
|
||||||
1. Check if task is implemented
|
|
||||||
- yes: proceed
|
|
||||||
- no: return
|
|
||||||
2. Check if worker cluster is running
|
|
||||||
- yes: add task to queue
|
|
||||||
- no: run it as blocking process
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Get task list
|
# Get task list
|
||||||
@ -84,20 +69,26 @@ def run_task(taskname):
|
|||||||
|
|
||||||
# Check if task exists
|
# Check if task exists
|
||||||
if taskname not in tasks:
|
if taskname not in tasks:
|
||||||
logger.warning(f'Task "{taskname}" is not implemented')
|
logger.warning(f'Task "{taskname}" is not implemented in InvenTree/tasks.py')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
from django_q.tasks import AsyncTask
|
||||||
|
except (AppRegistryNotReady):
|
||||||
|
logger.warning("Could not offload task - app registry not ready")
|
||||||
|
return
|
||||||
from InvenTree.status import is_worker_running
|
from InvenTree.status import is_worker_running
|
||||||
|
|
||||||
if is_worker_running():
|
if is_worker_running() and not force_sync:
|
||||||
# Append module path
|
# Append module path
|
||||||
taskname = 'InvenTree.tasks.' + taskname
|
taskname = 'InvenTree.tasks.' + taskname
|
||||||
# Running as task
|
# Running as asynchronous task
|
||||||
offload_task(taskname)
|
task = AsyncTask(taskname, *args, **kwargs)
|
||||||
|
task.run()
|
||||||
else:
|
else:
|
||||||
# Retrieve local method from task name
|
# Retrieve local method from task name
|
||||||
_func = eval(taskname)
|
_func = eval(taskname)
|
||||||
# Run it as blocking process
|
# Run it as synchronous task
|
||||||
_func()
|
_func()
|
||||||
|
|
||||||
|
|
||||||
|
@ -815,13 +815,13 @@ class CurrencyRefreshView(RedirectView):
|
|||||||
On a POST request we will attempt to refresh the exchange rates
|
On a POST request we will attempt to refresh the exchange rates
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from InvenTree.tasks import run_task
|
from InvenTree.tasks import offload_task
|
||||||
|
|
||||||
# Define associated task from InvenTree.tasks list of methods
|
# Define associated task from InvenTree.tasks list of methods
|
||||||
taskname = 'update_exchange_rates'
|
taskname = 'update_exchange_rates'
|
||||||
|
|
||||||
# Run it
|
# Run it
|
||||||
run_task(taskname)
|
offload_task(taskname, force_sync=True)
|
||||||
|
|
||||||
return redirect(reverse_lazy('settings'))
|
return redirect(reverse_lazy('settings'))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user