|
| 1 | +Use `softioc.autosave` in an IOC |
| 2 | +================================ |
| 3 | + |
| 4 | +`../tutorials/creating-an-ioc` shows how to create a pythonSoftIOC. |
| 5 | + |
| 6 | + |
| 7 | +Example IOC |
| 8 | +----------- |
| 9 | + |
| 10 | +.. literalinclude:: ../examples/example_autosave_ioc.py |
| 11 | + |
| 12 | +Records are instantiated as normal and configured for automatic loading and |
| 13 | +periodic saving to a backup file with use of the keyword argument ``autosave``. |
| 14 | +``autosave`` resolves to a list of strings, which are the names of fields to be |
| 15 | +tracked by autosave. By default ``autosave=False``, which disables autosave for that PV. |
| 16 | +Setting ``autosave=True`` is equivalent to passing ``["VAL"]``. Note that ``"VAL"`` must be |
| 17 | +explicitly passed when tracking other fields, e.g. ``["VAL", "LOPR", "HOPR"]``. |
| 18 | +``autosave`` can also accept a single string field name as an argument. |
| 19 | + |
| 20 | +The field values get written into a yaml-formatted file containing key-value pairs. |
| 21 | +By default the keys are the same as the full PV name, including any device name specified |
| 22 | +in :func:`~softioc.builder.SetDeviceName()`. |
| 23 | + |
| 24 | +Autosave is disabled until :func:`~softioc.autosave.configure()` is called. The first two arguments, |
| 25 | +``directory`` and ``name`` are required. Backup files are periodically written into |
| 26 | +``directory`` with the name ``<name>.softsav`` every ``save_period`` seconds, |
| 27 | +set to 30.0 by default. The directory must exist, and should be configured with the appropriate |
| 28 | +read/write permissions for the user running the IOC. |
| 29 | + |
| 30 | +IOC developers should only need to interface with autosave via the :func:`~softioc.autosave.configure()` |
| 31 | +method and the ``autosave`` keyword argument. Alternatively, |
| 32 | +PVs can be instantiated inside the :class:`~softioc.autosave.Autosave()` context manager, which |
| 33 | +automatically passes the ``autosave`` argument to any PVs created |
| 34 | +inside the context manager. If any fields are already specified by the ``autosave`` keyword |
| 35 | +argument of a PV's initialisation call the lists of fields to track get combined. |
| 36 | +All other module members are intended for internal use only. |
| 37 | + |
| 38 | +In normal operation, loading from a backup is performed once during the |
| 39 | +:func:`~softioc.builder.LoadDatabase()` call and periodic saving to the backup file begins when |
| 40 | +:func:`~softioc.softioc.iocInit()` is called, provided that any PVs are configured to be saved. |
| 41 | +Currently, manual loading from a backup at runtime after ioc initialisation is not supported. |
| 42 | +Saving only occurs when any of the saved field values have changed since the last save. |
| 43 | +Users are discouraged from manually editing the backup files while the |
| 44 | +IOC is running so that the internal state of the autosave thread is consistent with |
| 45 | +the backup file. |
| 46 | + |
| 47 | +If autosave is enabled and active, a timestamped copy of the latest existing autosave backup file is created |
| 48 | +when the IOC is restarted, e.g. ``<name>.softsav_240717-095004`` (timestamps are in the format yymmdd-HHMMSS). |
| 49 | +If you only wish to store one backup of the autosave file at a time, ``timestamped_backups=False`` |
| 50 | +can be passed to :func:`~softioc.autosave.configure()` when it is called, this will create a backup file |
| 51 | +named ``<name>.softsav.bu``. To disable any autosaving, comment out the |
| 52 | +:func:`~softioc.autosave.configure()` call or pass it the keyword argument |
| 53 | +``enabled=False``. |
| 54 | + |
| 55 | +The resulting backup file after running the example IOC for about 30 seconds is the following: |
| 56 | + |
| 57 | +.. code-block:: |
| 58 | +
|
| 59 | + MY-DEVICE-PREFIX:AI.EGU: '' |
| 60 | + MY-DEVICE-PREFIX:AI.PREC: '0' |
| 61 | + MY-DEVICE-PREFIX:AO: 0.0 |
| 62 | + MY-DEVICE-PREFIX:AUTOMATIC-AO: 0.0 |
| 63 | + MY-DEVICE-PREFIX:AUTOMATIC-AO.EGU: '' |
| 64 | + MY-DEVICE-PREFIX:AUTOMATIC-AO.HOPR: '0' |
| 65 | + MY-DEVICE-PREFIX:AUTOMATIC-AO.LOPR: '0' |
| 66 | + MY-DEVICE-PREFIX:SECONDSRUN: 29 |
| 67 | + MY-DEVICE-PREFIX:WAVEFORMIN: [0, 0, 0, 0] |
| 68 | +
|
| 69 | +
|
| 70 | +If the IOC is stopped and restarted, the SECONDSRUN record will load its saved |
| 71 | +value of 29 from the backup. |
| 72 | +All non-VAL fields are stored as strings. Waveform type records holding arrays |
| 73 | +are cast into lists before saving. |
| 74 | + |
| 75 | +This example IOC uses cothread, but autosave works identically when using |
| 76 | +an asyncio dispatcher. |
0 commit comments