Skip to content

Commit

Permalink
Merge pull request #148 from lsst-ts/tickets/DM-41082
Browse files Browse the repository at this point in the history
Tickets/dm 41082
  • Loading branch information
dsanmartim authored Oct 21, 2023
2 parents d4596e1 + 89def33 commit 115a6f7
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/news/DM-41082.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introduce the ``sleep.py`` script. This new addition allows users to sent a sleep command to the script queue for a desired duration.
1 change: 1 addition & 0 deletions python/lsst/ts/standardscripts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .base_script_test_case import *
from .run_command import *
from .set_summary_state import *
from .sleep import *
from .system_wide_shutdown import *
from .utils import *

Expand Down
27 changes: 27 additions & 0 deletions python/lsst/ts/standardscripts/data/scripts/sleep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python
# This file is part of ts_standardscripts
#
# Developed for the LSST Telescope and Site Systems.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import asyncio

from lsst.ts.standardscripts import Sleep

asyncio.run(Sleep.amain())
87 changes: 87 additions & 0 deletions python/lsst/ts/standardscripts/sleep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# This file is part of ts_standardscripts
#
# Developed for the LSST Telescope and Site Systems.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

__all__ = ["Sleep"]

import asyncio

import yaml
from lsst.ts.salobj import BaseScript


class Sleep(BaseScript):
"""Sleep for a given amount of time.
This script pauses the script queue for a specified amount of time.
Parameters
----------
index : `int`
Index of Script SAL component.
Notes
-----
**Details**
This script sends a sleep command to the script queue, which pauses
the script queue for a specified amount of time.
"""

def __init__(self, index):
super().__init__(index=index, descr="Pause the script queue.")

self.sleep_for = 0

@classmethod
def get_schema(cls):
schema_yaml = """
$schema: http://json-schema.org/draft-07/schema#
$id: https://github.com/lsst-ts/ts_standardscripts/sleep.yaml
title: Sleep v1
description: Configuration for Sleep command.
type: object
properties:
sleep_for:
description: >-
Duration of the sleep command in seconds.
type: number
minimum: 0
additionalProperties: false
"""
return yaml.safe_load(schema_yaml)

async def configure(self, config):
"""Configure the script.
Parameters
----------
config : `types.SimpleNamespace`
Configuration
"""
self.sleep_for = config.sleep_for

def set_metadata(self, metadata):
metadata.duration = self.sleep_for

async def run(self):
self.log.info(f"Sleep queue for {self.sleep_for} seconds...")
await asyncio.sleep(self.sleep_for)
60 changes: 60 additions & 0 deletions tests/test_sleep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# This file is part of ts_standardscripts
#
# Developed for the LSST Telescope and Site Systems.
# This product includes software developed by the LSST Project
# (https://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import unittest

from lsst.ts.standardscripts import BaseScriptTestCase, get_scripts_dir
from lsst.ts.standardscripts.sleep import Sleep


class TestSleep(BaseScriptTestCase, unittest.IsolatedAsyncioTestCase):
async def basic_make_script(self, index):
self.script = Sleep(index=index)
return (self.script,)

async def test_configure(self):
# Test that the sleep time is set correctly
async with self.make_script():
sleep_for = 5

await self.configure_script(sleep_for=sleep_for)

self.assertEqual(self.script.sleep_for, sleep_for)

async def test_run(self):
"""
Test that the script sleeps for the correct amount of time.
"""
async with self.make_script():
sleep_for = 5
await self.configure_script(sleep_for=sleep_for)

# Run the script
await self.run_script()

async def test_executable(self):
scripts_dir = get_scripts_dir()
script_path = scripts_dir / "sleep.py"
await self.check_executable(script_path)


if __name__ == "__main__":
unittest.main()

0 comments on commit 115a6f7

Please sign in to comment.