Skip to content

Commit

Permalink
raise an error if job_name is still set to the default
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Oct 27, 2024
1 parent f96ebfd commit 2835dbb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
- fix for updating over the internet when a Pioreactor is on a `A.devX` or `B.rcY` release
- `pio kill --all-jobs` will no longer kill long-running jobs from plugins (specifically, `logs2x` jobs.)
- updating the UI software won't prematurely stop any currently running activities
- correct eth mac address on RPi5s
- correct ethernet mac address on RPi5s
- We weren't passing all the OS environment variables when jobs were started from the UI. This is fixed now.


### 24.10.1
Expand Down
18 changes: 14 additions & 4 deletions pioreactor/background_jobs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ class _BackgroundJob(metaclass=PostInitCaller):
SLEEPING: pt.JobState = "sleeping"
LOST: pt.JobState = "lost"

# initial state is disconnected
state: pt.JobState = DISCONNECTED
job_name: str = "background_job"
_is_cleaned_up: bool = False # mqtt connections closed, logger closed, etc.
# initial state is disconnected, set other metadata
state = DISCONNECTED
job_name = "_background_job" # this should be overwritten in subclasses
_is_cleaned_up = False # mqtt connections closed, JM cache is empty, logger closed, etc.
_IS_LONG_RUNNING = False # by default, jobs aren't long running (persistent over experiments)

# published_settings is typically overwritten in the subclasses. Attributes here will
Expand Down Expand Up @@ -999,6 +999,11 @@ class BackgroundJobContrib(_BackgroundJob):
Plugin jobs should inherit from this class.
"""

def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
if cls.job_name == "_background_job":
raise NameError(f"must provide a job_name property to this BackgroundJob class {cls}.")

def __init__(self, unit: str, experiment: str, plugin_name: str) -> None:
super().__init__(unit, experiment, source=plugin_name)

Expand Down Expand Up @@ -1198,5 +1203,10 @@ class BackgroundJobWithDodgingContrib(BackgroundJobWithDodging):
Plugin jobs should inherit from this class.
"""

def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
if cls.job_name == "_background_job":
raise NameError(f"must provide a job_name property to this BackgroundJob class {cls}.")

def __init__(self, unit: str, experiment: str, plugin_name: str) -> None:
super().__init__(unit=unit, experiment=experiment, source=plugin_name)
2 changes: 1 addition & 1 deletion pioreactor/background_jobs/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import subprocess
from contextlib import suppress
from pathlib import Path
from threading import Thread
from time import sleep
from typing import Callable
from typing import Optional
from pathlib import Path

import click

Expand Down
5 changes: 3 additions & 2 deletions pioreactor/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ def __getattr__(attr): # type: ignore
return get_leader_address()
elif attr == "mqtt_address":
return get_mqtt_address()
elif attr == "config":
return get_config()
else:
raise AttributeError

Expand Down Expand Up @@ -167,6 +165,9 @@ def get_config() -> ConfigParserMod:
return config


config = get_config()


@cache
def get_leader_hostname() -> str:
return get_config().get("cluster.topology", "leader_hostname", fallback="localhost")
Expand Down
15 changes: 15 additions & 0 deletions pioreactor/tests/test_background_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from pioreactor.background_jobs.base import BackgroundJob
from pioreactor.background_jobs.base import BackgroundJobContrib
from pioreactor.background_jobs.base import BackgroundJobWithDodging
from pioreactor.background_jobs.od_reading import ODReader
from pioreactor.background_jobs.od_reading import start_od_reading
Expand Down Expand Up @@ -519,3 +520,17 @@ def action_to_do_after_od_reading(self) -> None:

od.clean_up()
jp.clean_up()


def test_subclasses_provide_a_unique_job_name_for_contrib():
with pytest.raises(NameError):

class TestJobBad(BackgroundJobContrib):
def __init__(self, unit: str, experiment: str) -> None:
super(TestJobBad, self).__init__(unit=unit, experiment=experiment, plugin_name="test")

class TestJobOkay(BackgroundJobContrib):
job_name = "test_job"

def __init__(self, unit: str, experiment: str) -> None:
super(TestJobOkay, self).__init__(unit=unit, experiment=experiment, plugin_name="test")
1 change: 1 addition & 0 deletions update_scripts/upcoming/huey.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ After=network.target firstboot.service
Before=lighttpd.service

[Service]
EnvironmentFile=/etc/environment
User=pioreactor
WorkingDirectory=/var/www/pioreactorui
ExecStart=huey_consumer pioreactorui.tasks.huey -n -b 1.0 -w 6 -f -C -d 0.05
Expand Down

0 comments on commit 2835dbb

Please sign in to comment.