-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial work * Revert default exposure times * Merge branch 'main' into albireox/abort-exposures * Full implementation * Merge branch 'main' into albireox/abort-exposures * Make abort-exposures unique * Various fixes and wait for abort to complete * Add tests * Update changelog * More tests
- Loading branch information
Showing
8 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# @Author: José Sánchez-Gallego ([email protected]) | ||
# @Date: 2024-05-26 | ||
# @Filename: abort_exposures.py | ||
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause) | ||
|
||
from __future__ import annotations | ||
|
||
import asyncio | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from clu.parsers.click import unique | ||
|
||
from hal.macros.expose import ExposeMacro | ||
|
||
from . import hal_command_parser | ||
|
||
|
||
if TYPE_CHECKING: | ||
from hal.actor import HALCommandType | ||
|
||
|
||
__all__ = ["abort_exposures"] | ||
|
||
|
||
async def wait_until_idle(command: HALCommandType): | ||
"""Waits until all cameras are idle.""" | ||
|
||
while True: | ||
await asyncio.sleep(0.5) | ||
|
||
if command.actor.helpers.apogee.is_exposing(): | ||
continue | ||
|
||
if command.actor.helpers.boss.is_exposing(reading_ok=False): | ||
continue | ||
|
||
break | ||
|
||
|
||
@hal_command_parser.command(name="abort-exposures") | ||
@unique() | ||
async def abort_exposures(command: HALCommandType): | ||
"""Aborts ongoing exposures..""" | ||
|
||
expose_macro = command.actor.helpers.macros["expose"] | ||
assert isinstance(expose_macro, ExposeMacro) | ||
|
||
if expose_macro.running: | ||
command.warning("Cancelling the expose macro.") | ||
expose_macro.cancel(now=True) | ||
|
||
command.warning("Aborting ongoing exposures.") | ||
|
||
tasks = [ | ||
command.actor.helpers.apogee.abort(command), | ||
command.actor.helpers.boss.abort(command), | ||
] | ||
|
||
results = await asyncio.gather(*tasks, return_exceptions=True) | ||
for iresult, result in enumerate(results): | ||
instrument = ["APOGEE", "BOSS"][iresult] | ||
if isinstance(result, Exception): | ||
return command.fail(f"Failed to abort {instrument} exposure: {result!s}") | ||
elif result is not True: | ||
return command.fail(f"Unknown error while aborting {instrument} exposure.") | ||
else: | ||
continue | ||
|
||
command.info("Waiting until cameras are idle.") | ||
await wait_until_idle(command) | ||
|
||
return command.finish(text="Exposures have been aborted.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# @Author: José Sánchez-Gallego ([email protected]) | ||
# @Date: 2024-05-29 | ||
# @Filename: test_command_abort_exposures.py | ||
# @License: BSD 3-clause (http://www.opensource.org/licenses/BSD-3-Clause) | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
|
||
from hal.exceptions import HALError | ||
|
||
|
||
if TYPE_CHECKING: | ||
from pytest_mock import MockerFixture | ||
|
||
from hal.actor import HALActor | ||
|
||
|
||
@pytest.mark.parametrize("observatory", ["LCO", "APO"]) | ||
async def test_abort_exposures( | ||
actor: HALActor, | ||
mocker: MockerFixture, | ||
monkeypatch: pytest.MonkeyPatch, | ||
observatory: str, | ||
): | ||
apogee = actor.helpers.apogee | ||
boss = actor.helpers.boss | ||
|
||
expose_macro = actor.helpers.macros["expose"] | ||
monkeypatch.setattr(expose_macro, "_running", True) | ||
cancel_mock = mocker.patch.object(expose_macro, "cancel") | ||
|
||
monkeypatch.setattr(actor, "observatory", observatory) | ||
|
||
mocker.patch.object(apogee, "is_exposing", side_effect=[True, True, False, False]) | ||
mocker.patch.object(boss, "is_exposing", side_effect=[True, True, False]) | ||
|
||
cmd = await actor.invoke_mock_command("abort-exposures") | ||
await cmd | ||
|
||
assert cmd.status.did_succeed | ||
cancel_mock.assert_called_once() | ||
|
||
|
||
async def test_abort_exposures_no_exposure_to_abort( | ||
actor: HALActor, | ||
mocker: MockerFixture, | ||
): | ||
apogee = actor.helpers.apogee | ||
boss = actor.helpers.boss | ||
|
||
mocker.patch.object(apogee, "is_exposing", side_effect=[False, False]) | ||
mocker.patch.object(boss, "is_exposing", side_effect=[False, False]) | ||
|
||
cmd = await actor.invoke_mock_command("abort-exposures") | ||
await cmd | ||
|
||
assert cmd.status.did_succeed | ||
|
||
|
||
async def test_abort_exposures_abort_fails(actor: HALActor, mocker: MockerFixture): | ||
apogee = actor.helpers.apogee | ||
boss = actor.helpers.boss | ||
|
||
mocker.patch.object(apogee, "is_exposing", side_effect=[True, False]) | ||
mocker.patch.object(boss, "is_exposing", side_effect=[True, False]) | ||
|
||
mocker.patch.object(apogee, "abort", side_effect=HALError("abort failed")) | ||
|
||
cmd = await actor.invoke_mock_command("abort-exposures") | ||
await cmd | ||
|
||
assert cmd.status.did_fail | ||
error = cmd.replies[-1].message["error"] | ||
assert "Failed to abort" in error | ||
|
||
|
||
async def test_abort_exposures_abort_fails_unknown( | ||
actor: HALActor, | ||
mocker: MockerFixture, | ||
): | ||
apogee = actor.helpers.apogee | ||
boss = actor.helpers.boss | ||
|
||
mocker.patch.object(apogee, "is_exposing", return_value=True) | ||
mocker.patch.object(boss, "is_exposing", return_value=True) | ||
|
||
mocker.patch.object(boss, "abort", return_value=False) | ||
|
||
cmd = await actor.invoke_mock_command("abort-exposures") | ||
await cmd | ||
|
||
assert cmd.status.did_fail | ||
error = cmd.replies[-1].message["error"] | ||
assert "Unknown error" in error |