queues.rst 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. .. _redis-queue:
  2. Redis Queues
  3. =============================
  4. Requirements
  5. -------------
  6. The hard computation work (e.g. forecasting, scheduling) should happen outside of web requests (asynchronously), in job queues accessed by worker processes.
  7. This queueing relies on a Redis server, which has to be installed locally, or used on a separate host. In the latter case, configure :ref:`redis-config` details in your FlexMeasures config file.
  8. Here we assume you have access to a Redis server and configured it (see :ref:`redis-config`).
  9. The FlexMeasures unit tests use fakeredis to simulate this task queueing, with no configuration required.
  10. .. note:: See also :ref:`docker-compose` for usage of Redis via Docker and a more hands-on tutorial on the queues.
  11. Run workers
  12. -------------
  13. Here is how to run one worker for each kind of job (in separate terminals):
  14. .. code-block:: bash
  15. $ flexmeasures jobs run-worker --name our-only-worker --queue forecasting|scheduling
  16. Running multiple workers in parallel might be a great idea.
  17. .. code-block:: bash
  18. $ flexmeasures jobs run-worker --name forecaster --queue forecasting
  19. $ flexmeasures jobs run-worker --name scheduler --queue scheduling
  20. You can also clear the job queues:
  21. .. code-block:: bash
  22. $ flexmeasures jobs clear-queue --queue forecasting
  23. $ flexmeasures jobs clear-queue --queue scheduling
  24. When the main FlexMeasures process runs (e.g. by ``flexmeasures run``\ ), the queues of forecasting and scheduling jobs can be visited at ``http://localhost:5000/tasks/forecasting`` and ``http://localhost:5000/tasks/schedules``\ , respectively (by admins).
  25. Inspect the queue and jobs
  26. ------------------------------
  27. The first option to inspect the state of the ``forecasting`` queue should be via the formidable `RQ dashboard <https://github.com/Parallels/rq-dashboard>`_. If you have admin rights, you can access it at ``your-flexmeasures-url/rq/``\ , so for instance ``http://localhost:5000/rq/``. You can also start RQ dashboard yourself (but you need to know the redis server credentials):
  28. .. code-block:: bash
  29. $ pip install rq-dashboard
  30. $ rq-dashboard --redis-host my.ip.addr.ess --redis-password secret --redis-database 0
  31. RQ dashboard shows you ongoing and failed jobs, and you can see the error messages of the latter, which is very useful.
  32. Finally, you can also inspect the queue and jobs via a console (\ `see the nice RQ documentation <http://python-rq.org/docs/>`_\ ), which is more powerful. Here is an example of inspecting the finished jobs and their results:
  33. .. code-block:: python
  34. from redis import Redis
  35. from rq import Queue
  36. from rq.job import Job
  37. from rq.registry import FinishedJobRegistry
  38. r = Redis("my.ip.addr.ess", port=6379, password="secret", db=2)
  39. q = Queue("forecasting", connection=r)
  40. finished = FinishedJobRegistry(queue=q)
  41. finished_job_ids = finished.get_job_ids()
  42. print("%d jobs finished successfully." % len(finished_job_ids))
  43. job1 = Job.fetch(finished_job_ids[0], connection=r)
  44. print("Result of job %s: %s" % (job1.id, job1.result))
  45. Redis queues on Windows
  46. ---------------------------
  47. On Unix, the rq system is automatically set up as part of FlexMeasures's main setup (the ``rq`` dependency).
  48. However, rq is `not functional on Windows <http://python-rq.org/docs>`_ without the Windows Subsystem for Linux.
  49. On these versions of Windows, FlexMeasures's queuing system uses an extension of Redis Queue called ``rq-win``.
  50. This is also an automatically installed dependency of FlexMeasures.
  51. However, the Redis server needs to be set up separately. Redis itself does not work on Windows, so it might be easiest to commission a Redis server in the cloud (e.g. on kamatera.com).
  52. If you want to install Redis on Windows itself, it can be set up on a virtual machine as follows:
  53. * `Install Vagrant on Windows <https://www.vagrantup.com/intro/getting-started/>`_ and `VirtualBox <https://www.virtualbox.org/>`_
  54. * Download the `vagrant-redis <https://raw.github.com/ServiceStack/redis-windows/master/downloads/vagrant-redis.zip>`_ vagrant configuration
  55. * Extract ``vagrant-redis.zip`` in any folder, e.g. in ``c:\vagrant-redis``
  56. * Set ``config.vm.box = "hashicorp/precise64"`` in the Vagrantfile, and remove the line with ``config.vm.box_url``
  57. * Run ``vagrant up`` in Command Prompt
  58. * In case ``vagrant up`` fails because VT-x is not available, `enable it <https://www.howali.com/2017/05/enable-disable-intel-virtualization-technology-in-bios-uefi.html>`_ in your bios `if you can <https://www.intel.com/content/www/us/en/support/articles/000005486/processors.html>`_ (more debugging tips `here <https://forums.virtualbox.org/viewtopic.php?t=92111>`_ if needed)