Merge pull request #1858 from eeintech/exchange_rate_task

Exchange rate as worker task
This commit is contained in:
Oliver 2021-08-10 08:23:46 +10:00 committed by GitHub
commit 0a1ce59dfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 10 deletions

View File

@ -51,11 +51,14 @@ 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. Create an AsyncTask if workers are running.
This is different to a 'scheduled' task, This is different to a 'scheduled' task,
in that it only runs once! in that it only runs once!
If workers are not running or force_sync flag
is set then the task is ran synchronously.
""" """
try: try:
@ -63,10 +66,48 @@ def offload_task(taskname, *args, **kwargs):
except (AppRegistryNotReady): except (AppRegistryNotReady):
logger.warning("Could not offload task - app registry not ready") logger.warning("Could not offload task - app registry not ready")
return return
import importlib
from InvenTree.status import is_worker_running
if is_worker_running() and not force_sync:
# Running as asynchronous task
try:
task = AsyncTask(taskname, *args, **kwargs) task = AsyncTask(taskname, *args, **kwargs)
task.run() task.run()
except ImportError:
logger.warning(f"WARNING: '{taskname}' not started - Function not found")
else:
# Split path
try:
app, mod, func = taskname.split('.')
app_mod = app + '.' + mod
except ValueError:
logger.warning(f"WARNING: '{taskname}' not started - Malformed function path")
return
# Import module from app
try:
_mod = importlib.import_module(app_mod)
except ModuleNotFoundError:
logger.warning(f"WARNING: '{taskname}' not started - No module named '{app_mod}'")
return
# Retrieve function
try:
_func = getattr(_mod, func)
except AttributeError:
# getattr does not work for local import
_func = None
try:
if not _func:
_func = eval(func)
except NameError:
logger.warning(f"WARNING: '{taskname}' not started - No function named '{func}'")
return
# Workers are not running: run it as synchronous task
_func()
def heartbeat(): def heartbeat():

View File

@ -31,8 +31,6 @@ from stock.models import StockLocation, StockItem
from common.models import InvenTreeSetting, ColorTheme from common.models import InvenTreeSetting, ColorTheme
from users.models import check_user_role, RuleSet from users.models import check_user_role, RuleSet
import InvenTree.tasks
from .forms import DeleteForm, EditUserForm, SetPasswordForm from .forms import DeleteForm, EditUserForm, SetPasswordForm
from .forms import SettingCategorySelectForm from .forms import SettingCategorySelectForm
from .helpers import str2bool from .helpers import str2bool
@ -827,8 +825,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
""" """
# Will block for a little bit from InvenTree.tasks import offload_task
InvenTree.tasks.update_exchange_rates()
# Define associated task from InvenTree.tasks list of methods
taskname = 'InvenTree.tasks.update_exchange_rates'
# Run it
offload_task(taskname, force_sync=True)
return redirect(reverse_lazy('settings')) return redirect(reverse_lazy('settings'))