Skip to content

Commit

Permalink
Improve readability of State in taskrunner
Browse files Browse the repository at this point in the history
  • Loading branch information
oysols committed Nov 22, 2021
1 parent 45539a3 commit 627e9c8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ with Local() as e:

- Semaphore queue `semaphore.py` / `semaphore_subprocess.py`

```
```python
with semaphore_queue("user@remote_host:path/to/semaphore.queue"):
do_stuff_while_holding_semaphore()
```

- Tasks / Taskrunner `tasks.py` / `taskrunner.py`
- Tasks `tasks.py`

```python
class Setup(Task):
Expand All @@ -43,6 +43,26 @@ class Test(Task):
e.sh(f"echo Testing commit {self.state.commit}")
```

- Taskrunner `taskrunner.py`

```python
# Create a simple State object
state = State()

# or create a State object with additional context for the tasks
state = State(
commit="1234ca3d41a23d4e4f923",
branch="some/branch",
repo_name="MyRepo",
)

# Run Tasks directly
run_tasks([Task1, Task2], state)

# or run Tasks from a separate file
run_all_tasks_in_file("tasks.py", state)
```

# Docs

TODO
Expand Down
15 changes: 8 additions & 7 deletions minimalci/taskrunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ def run(self) -> None:
parser.add_argument('--file', help='Tasks file', default="tasks.py")
args = parser.parse_args()

state = State()
state.commit = args.commit
state.branch = args.branch
state.repo_name = args.repo_name
state.log_url = args.log_url
state.logdir = Path(args.logdir)
state.identifier = args.identifier
state = State(
commit=args.commit,
branch=args.branch,
repo_name=args.repo_name,
log_url=args.log_url,
identifier=args.identifier,
logdir=Path(args.logdir),
)

# Kill executor processes and cleanly exit on SIGTERM/SIGINT
set_sigterm_sigint_global_kill_signal_handler()
Expand Down
19 changes: 13 additions & 6 deletions minimalci/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
import enum
from dataclasses import dataclass
import secrets

from minimalci.executors import ProcessError, global_kill_signal
from minimalci import semaphore, util
Expand Down Expand Up @@ -75,16 +76,22 @@ def load(cls, filepath: Path) -> "StateSnapshot":


class State():
def __init__(self) -> None:
self.tasks: List[Task] = []
self.logdir = Path()

def __init__(
self,
commit: str = "",
branch: str = "",
repo_name: str = "",
log_url: str = "",
identifier: str = "",
logdir: Optional[Path] = None,
) -> None:
self.commit = ""
self.branch = ""
self.repo_name = ""
self.log_url = ""
self.identifier = ""

self.identifier = identifier if identifier else secrets.token_hex(16)
self.logdir = logdir if logdir else Path()
self.tasks: List[Task] = []
self.started = time.time()
self.finished: Optional[float] = None

Expand Down
9 changes: 5 additions & 4 deletions server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,11 @@ def start_taskrunner_in_docker(commit: str, branch: str) -> str:
external_workdir = config.EXTERNAL_DATA_MOUNT_POINT / workdir.relative_to(config.DATA_PATH)

# Create empty state to indicate build has been initiated
state = State()
state.branch = branch
state.commit = commit
state.identifier = identifier
state = State(
branch=branch,
commit=commit,
identifier=identifier,
)
state.snapshot().save(logdir / config.STATEFILE)

command = [
Expand Down

0 comments on commit 627e9c8

Please sign in to comment.