Skip to content

Named logger support and various server management enhancements - #132

Merged
Carwyn Pelley (cpelley) merged 7 commits into
mainfrom
logger_enhancecment
Oct 27, 2026
Merged

Carwyn Pelley (cpelley) merged 7 commits into
mainfrom
logger_enhancecment

Conversation

@cpelley

@cpelley Carwyn Pelley (cpelley) commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator
  • Attaching socket handler to a named logger by the client. This means being able to selectively choose what's captured by the DAGrunner logging server.
  • Ensure that logging server operates independently to clients. This means that the server now finishes and completes properly now and also doesn't wait for the client to cleanup it's handler.
  • Make logger name configurable via the DAGrunner global configuration file.
  • Updated tests accordingly.
  • AI helped a lot putting this new test file together.

Closes #125

finally:
if server_proc.poll() is None:
server_proc.kill()
server_proc.wait(timeout=5)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks weird waiting on a subprocess you've just killed, but I've read a bit around this, and this looks like it is to avoid the subprocess becoming a zombie.

time.sleep(1) # wait for db write to complete
server_proc.terminate()
server_proc.wait(timeout=5)
assert server_proc.returncode == 0, "Server process did not exit normally"

@mo-robert-purvis Rob Purvis (mo-robert-purvis) Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This failed once when running pytest:

>       assert server_proc.returncode == 0, "Server process did not exit normally"
E       AssertionError: Server process did not exit normally
E       assert -15 == 0
E        +  where -15 = <Popen: returncode: -15 args: ['/data/users/eppdev/conda/envs/scitools-2025_...>.returncode

dagrunner/tests/utils/logging/test_integration.py:98: AssertionError

but a subsequent pytest run passed - weird.

Note that -15 means process was terminated with a SIGTERM

Maybe the assert should allow for 0 or -15 ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Thanks Rob Purvis (@mo-robert-purvis), I'll investigate.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it may be that the 5s timeout was not enough, so the OS had to SIGTERM it.

@cpelley Carwyn Pelley (cpelley) Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Multiple servers are being started concurrently by different tests. One of those tests intentionally sends a SIGTERM to its server, but because the servers are not distinguishable from one another, the signal can inadvertently affect servers belonging to other tests. Those tests then fail unexpectedly because their server has been terminated rather than being cleanly shut down.

Comment thread dagrunner/utils/logger.py
records and customise logging events.
"""

def _recv(self, size):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is not covered by ptests

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

_recv is tested indirectly through integration tests, rather than by calling the method directly.

test_sqlitedb and test_named_logger in test_integration.py send records to the server, terminate it, and verify that the records were persisted. More specifically, test_process_termination in test_start_logging_server.py keeps a client connected while testing shutdown with no data, an incomplete header, and an incomplete record body. It checks that the server exits and saves earlier complete records. Those cases exercise the shutdown behaviour implemented by _recv.

Comment thread dagrunner/utils/logger.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

verbose is not used in the function.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

Comment thread dagrunner/utils/logger.py Outdated
- `sqlite_filepath`: The file path to the SQLite database. Optional.
- `host`: The host name of the server. Optional.
- `port`: The port number the server is listening on. Optional.
- `verbose`: Whether to print verbose output. Optional.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

verbose is not used in the function and should be removed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

done

Comment thread dagrunner/config.py
# Logic around default values belong in the functions which use them.
_INI_PARAMETERS = {
"dagrunner_logging": {"host": None, "port": None, "enabled": None},
"dagrunner_logging": {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ruff suggests these shouldn't be defined here

@cpelley Carwyn Pelley (cpelley) Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

why? I'm not sure I agree in this particular case of a singleton which is instantiated within this same module. Either way not in scope here.

Ruff is run as part of pre-commit and is passed fine there:
https://github.com/MetOffice/dagrunner/actions/runs/39392634239/job/118527595887#step:9:1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Some comments to consider

@cpelley

Carwyn Pelley (cpelley) commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator Author

The non-zero error is I suspect caused by pytest running tests in parallel. Multiple tests start logging servers simultaneously, and those servers are not uniquely identifiable, leading to conflicts.

The fix is to configure the server port as 0, allowing the operating system to automatically allocate an available port. Each test can then retrieve the assigned port and connect its client to the corresponding logging server, ensuring that parallel test executions do not interfere with one another.

@cpelley

Carwyn Pelley (cpelley) commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator Author

put back into draft. This handling of the server with the tests is annoying, especially since this problem is characteristic of parallel the tests and not real-life usage 😮‍💨
EDIT: Fixed -- I didn't push a local change. I have no pushed an amended commit and is ready to go.

@cpelley
Carwyn Pelley (cpelley) marked this pull request as draft October 22, 2026 15:47
@cpelley
Carwyn Pelley (cpelley) force-pushed the logger_enhancecment branch 2 times, most recently from 474d545 to 6bb1f99 Compare October 22, 2026 15:50
@cpelley
Carwyn Pelley (cpelley) marked this pull request as ready for review October 22, 2026 15:52

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good.

@cpelley
Carwyn Pelley (cpelley) merged commit 3fc9be3 into main Oct 27, 2026
6 checks passed
@cpelley
Carwyn Pelley (cpelley) deleted the logger_enhancecment branch October 27, 2026 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logger: Investigate limiting logging to a specified name

2 participants