Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a non-interactive function to use softioc in deamon mode #156

Closed
wants to merge 10 commits into from

Conversation

XavSPM
Copy link

@XavSPM XavSPM commented Apr 5, 2024

I propose this modification, following the initial request #155

Reminder of the initial proposal:

I propose adding the non_interactive_ioc function to allow the use of softioc in daemon mode. This mode can, for example, be used with systemd. It is compatible with asyncio and cothread.

To ensure that information is correctly logged in the journal, it is recommended to execute the Python script with the -u option or to set the environment variable PYTHONUNBUFFERED=TRUE.

Here is an example of a service:

[Unit]
Description=IOC
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/opt
ExecStart=/opt/softioc/bin/python -u /opt/example.py
Environment="PATH=/opt/softioc/bin"
Restart=always

[Install]
WantedBy=multi-user.target

And an example of a script:

# Import the basic framework components.
from softioc import softioc, builder
import cothread

# Set the record prefix
builder.SetDeviceName("MY-DEVICE-PREFIX")

# Create some records
ai = builder.aIn('AI', initial_value=5)
ao = builder.aOut('AO', initial_value=12.45, on_update=lambda v: ai.set(v))

# Boilerplate to get the IOC started
builder.LoadDatabase()
softioc.iocInit()

# Start processes required to be run after iocInit
def update():
    while True:
        ai.set(ai.get() + 1)
        print(ai.get())
        cothread.Sleep(1)

cothread.Spawn(update)

if __name__ == "__main__":
    softioc.non_interactive_ioc()

Copy link
Collaborator

@Araneidae Araneidae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wait_for_quit stuff looks straightforward enough (I've suggested a couple of minor code cleanups) ... but I don't see any mechanism for actually quitting (other than a signal).

softioc/cothread_dispatcher.py Outdated Show resolved Hide resolved
softioc/asyncio_dispatcher.py Outdated Show resolved Hide resolved
softioc/asyncio_dispatcher.py Outdated Show resolved Hide resolved
softioc/asyncio_dispatcher.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@AlexanderWells-diamond AlexanderWells-diamond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggested code. It's a useful feature that we've previously done without due to never actually running as a daemon process in any of our deployments. It will be useful to have this option going forward.

I've made some code change suggestions. There's also a couple of odds and ends that need cleaning up:

  • Please add an entry to the "Unreleased" section of the CHANGELOG.rst file, following the pattern (and rather picky syntax) of previous entries.
  • Please add to the documentation. This should probably go directly below the section for interactive_ioc, i.e. define it at about line 123 of the file docs/reference/api.rst. Docs can be built using pipenv run docs, and viewed at build/html/index.html
  • I would normally ask for a test or two to be written, but I think testing this will be tricky. I may add some tests at a later time.

softioc/softioc.py Outdated Show resolved Hide resolved
softioc/asyncio_dispatcher.py Outdated Show resolved Hide resolved
softioc/asyncio_dispatcher.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@AlexanderWells-diamond AlexanderWells-diamond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the delay in reviewing this. It's looking good, just a couple of minor cleanup requests.

When we merge this we will also want to clean up the commit history - you are welcome to do this as part of these last cleanups (i.e squash into a single commit) or we'll do it as part of the merge.

softioc/asyncio_dispatcher.py Outdated Show resolved Hide resolved
softioc/asyncio_dispatcher.py Outdated Show resolved Hide resolved
softioc/asyncio_dispatcher.py Outdated Show resolved Hide resolved
softioc/softioc.py Outdated Show resolved Hide resolved
Xavier GOIZIOU and others added 3 commits April 23, 2024 11:08
modified:   softioc/asyncio_dispatcher.py
modified:   softioc/cothread_dispatcher.py
modified:   softioc/softioc.py
XavSPM and others added 7 commits April 23, 2024 11:59
Update cothread_dispatcher.py
Update asyncio_dispatcher.py

Co-authored-by: Alexander Wells <79699091+AlexanderWells-diamond@users.noreply.github.com>
This ensures we only set INVALID_ALARM+UDF_ALARM when there is no value,
but otherwise both the severity and the status are set to NO_ALARM
Previously we did this as we wanted to spot the case where no value
was provided, either during record creation or by a set() call at some
later time, so that we could mark the record as being in an invalid
state.

Now that we store the severity and status alongside the value, we don't
need to handle this special case.
Added documentation for the non_interactive_ioc function.

Co-authored-by: Alexander Wells <79699091+AlexanderWells-diamond@users.noreply.github.com>

Update asyncio_dispatcher.py
Update softioc.py

Optimizing the wait_for_quit() function
Modify signal declaration
@XavSPM
Copy link
Author

XavSPM commented Apr 23, 2024

Hello, @AlexanderWells-diamond

I've made the requested changes and also reduced the number of commits.

Is there anything else I can do ?

@Araneidae
Copy link
Collaborator

I have squashed this work into a single commit, fixed a couple of trivial whitespace issues, and pushed to master. This is now accepted (the "conflicts" are just my whitespace fixes), so will close this PR as completed.

Thank you for this.

@Araneidae Araneidae closed this Apr 24, 2024
Araneidae pushed a commit that referenced this pull request Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants