Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the correct python version when running jobs #620

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions bennettbot/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ def run_command(self):
stdout_path=self.stdout_path,
stderr_path=self.stdout_path,
)

env = {**os.environ, "PYTHONPATH": settings.APPLICATION_ROOT}
bin_path = os.getenv("ABSOLUTE_BIN")
if bin_path and not env["PATH"].startswith(bin_path): # pragma: no cover
# If we have an ABSOLUTE_BIN env variable, ensure that it's set first in the path
env["PATH"] = f"{bin_path}:{env['PATH']}"
with open(self.stdout_path, "w") as stdout, open(
self.stderr_path, "w"
) as stderr:
Expand All @@ -99,7 +103,7 @@ def run_command(self):
cwd=self.cwd,
stdout=stdout,
stderr=stderr,
env={**os.environ, "PYTHONPATH": settings.APPLICATION_ROOT},
env=env,
shell=True,
)
rc = rv.returncode
Expand Down
6 changes: 6 additions & 0 deletions docker/entrypoints/prod.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#!/bin/bash

# Note: this is set as the default CMD in the Dockerfile
# In production, dokku extracts the Procfile and uses the commands defined
# there as the entrypoints, so this script is ignored in production. It is
# used only if we're running the production service with docker compose
# https://dokku.com/docs/processes/process-management/#procfile

set -euo pipefail

just run bot
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export PIP := BIN + if os_family() == "unix" { "/python -m pip" } else { "/pytho

export DEFAULT_PYTHON := if os_family() == "unix" { "python3.12" } else { "python" }

export ABSOLUTE_BIN := `pwd` + "/" + BIN

set dotenv-load := true


Expand Down
3 changes: 3 additions & 0 deletions tests/job_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
raw_config = {
"test": {
"jobs": {
"python_version": {
"run_args_template": 'python -c "import platform; print(platform.python_version())"',
},
"good_job": {
"run_args_template": "cat poem",
},
Expand Down
19 changes: 19 additions & 0 deletions tests/test_dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import os
import platform
import shutil
from pathlib import Path
from unittest.mock import Mock, patch

import httpretty
Expand Down Expand Up @@ -658,3 +660,20 @@ def test_message_checker_matched_messages(keyword, support_channel, reaction):
"timestamp": ["100.3"],
},
]


def test_python_version():
# check that we are using the same python version in jobs as we are
# in this test
# this would previously fail in local test runs if the user's system
# python version was different from the venv python and they were
# running `just test` without manually activating the venv first
log_dir = build_log_dir("test_python_version")

scheduler.schedule_job("test_python_version", {}, "channel", TS, 0)
job = scheduler.reserve_job()

do_job(slack_web_client(), job)

version_in_job = (Path(log_dir) / "stdout").read_text().strip()
assert version_in_job == platform.python_version()