Skip to content

Commit 0e58aeb

Browse files
committed
workers: revision worker implementation
PREVIOUS versions (most recent first): - 8e73e5a - 403e760 - 262f7a5 WIP DO NOT MERGE Commit message TBD - add main worker flag and capacity/throttle flags - add method to parse diff and list affected files - add more test coverage for revision_worker.py - add mots integration (bug 1740107) - add new RevisionWorker that pre-processes revisions (bug 1788728) - add new start/stop commands to manage workers - add new flags to stop workers gracefully (*_WORKER_STOPPED) - add repo.use_revision_worker feature flag (bug 1788732) - add mots hashes check - include new Lando revision info via API endpoint - refactor dependency and stack fetching and parsing using networkx - rename old command lando-cli landing-worker to lando-cli start-landing-worker - run pre/post mots query - store mots output in revision model TODO: - detect stack change on page load - add tests for new warnings
1 parent e53bf92 commit 0e58aeb

32 files changed

+1721
-148
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[flake8]
22
max-line-length = 88
33
select = C,E,F,W,B,B9
4-
ignore = E203, E501, W503, B006
4+
ignore = E203, E501, W503, B006, E712, E711
55
exclude =
66
.hg,
77
.git,

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ RUN cd / && pip install --no-cache /app
5454
ENV PYTHONPATH /app
5555
RUN chown -R app:app /app
5656

57-
# Create repos directory for transplanting in landing-worker
57+
# Create repos directory for landing-worker and revision worker.
5858
RUN mkdir /repos
59+
RUN chown -R app:app /repos
5960

6061
# Run as a non-privileged user
6162
USER app

Dockerfile-dev

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ENV PYTHONUNBUFFERED=1
2020
ENV FLASK_RUN_PORT=9000
2121
ENV FLASK_RUN_HOST=0.0.0.0
2222
ENV FLASK_DEBUG=1
23+
ENV HTTP_ALLOWED=1
2324

2425
ENTRYPOINT ["lando-cli"]
2526
CMD ["run"]
@@ -48,6 +49,7 @@ RUN cd / && pip install --no-cache /app
4849
ENV PYTHONPATH /app
4950
RUN chown -R app:app /app
5051

52+
# Create repos directory for landing worker and revision worker.
5153
RUN mkdir /repos
5254
RUN chown -R app:app /repos
5355

docker-compose.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,24 @@ services:
111111
- smtp
112112
lando-api.landing-worker:
113113
image: lando-api
114-
command: ["landing-worker"]
114+
command: ["start-landing-worker"]
115115
environment:
116-
- ENV=localdev
117-
- DATABASE_URL=postgresql://postgres:password@lando-api.db/lando_api_dev
118-
- SENTRY_DSN=
119-
# See http://docs.celeryproject.org/en/stable/getting-started/brokers/redis.html#configuration
120-
# for the full URL format.
121-
- CELERY_BROKER_URL=redis://redis.queue/0
122-
- OIDC_IDENTIFIER=https://lando-api.test
123-
- OIDC_DOMAIN=https://auth0.test
124-
- LANDO_UI_URL=https://lando.test
125-
- REPO_CLONES_PATH=/repos
126-
- REPOS_TO_LAND=localdev
116+
CELERY_BROKER_URL: "redis://redis.queue/0"
117+
DATABASE_URL: "postgresql://postgres:password@lando-api.db/lando_api_dev"
118+
ENV: "localdev"
119+
LANDO_UI_URL: "https://lando.test"
120+
OIDC_DOMAIN: "https://auth0.test"
121+
OIDC_IDENTIFIER: "https://lando-api.test"
122+
REPOS_TO_LAND: "localdev"
123+
REPO_CLONES_PATH: "/repos"
124+
SENTRY_DSN: ""
127125
user: root
128126
volumes:
129127
- ./:/app
130128
- ./migrations/:/migrations/
131129
# Prevent writing python cache to the host.
132130
- caches_cache:/app/.cache/
131+
- repos:/repos
133132
depends_on:
134133
- lando-api.db
135134
- redis.queue
@@ -157,3 +156,4 @@ volumes:
157156
caches_pycache:
158157
caches_cache:
159158
caches_pytest_cache:
159+
repos:

landoapi/api/landing_jobs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ def put(landing_job_id: str, data: dict):
6363

6464
if landing_job.status in (LandingJobStatus.SUBMITTED, LandingJobStatus.DEFERRED):
6565
landing_job.transition_status(LandingJobAction.CANCEL)
66+
for revision in landing_job.revisions:
67+
# Unlock patches so they can be modified in the future.
68+
revision.patch_locked = False
6669
db.session.commit()
6770
return {"id": landing_job.id}, 200
6871
else:

landoapi/api/transplants.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def post(phab: PhabricatorClient, data: dict):
365365
}
366366

367367
raw_diff = phab.call_conduit("differential.getrawdiff", diffID=diff["id"])
368-
lando_revision.set_patch(raw_diff, patch_data)
368+
lando_revision.set_patch(raw_diff, patch_data, final=True)
369369
db.session.commit()
370370
lando_revisions.append(lando_revision)
371371

@@ -446,11 +446,9 @@ def get_list(phab: PhabricatorClient, stack_revision_id: str):
446446
limit=len(revision_phids),
447447
)
448448

449-
# Return both transplants and landing jobs, since for repos that were switched
450-
# both or either of these could be populated.
451-
452449
rev_ids = [phab.expect(r, "id") for r in phab.expect(revs, "data")]
453450

451+
# Find landing jobs based on related revisions or legacy revision_to_diff_id field.
454452
landing_jobs = LandingJob.revisions_query(rev_ids).all()
455453

456454
return [job.serialize() for job in landing_jobs], 200

landoapi/cli.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,64 @@ def worker(celery_arguments):
6868
celery.worker_main((sys.argv[0],) + celery_arguments)
6969

7070

71-
@cli.command(name="landing-worker")
72-
def landing_worker():
71+
@cli.command(name="start-landing-worker")
72+
def start_landing_worker():
7373
from landoapi.app import auth0_subsystem, lando_ui_subsystem
74+
from landoapi.workers.landing_worker import LandingWorker
7475

7576
exclusions = [auth0_subsystem, lando_ui_subsystem]
7677
for system in get_subsystems(exclude=exclusions):
7778
system.ensure_ready()
7879

79-
from landoapi.workers.landing_worker import LandingWorker
80+
ConfigurationVariable.set(LandingWorker.STOP_KEY, VariableType.BOOL, "0")
8081

8182
worker = LandingWorker()
8283
worker.start()
8384

8485

86+
@cli.command(name="stop-landing-worker")
87+
def stop_landing_worker():
88+
from landoapi.workers.landing_worker import LandingWorker
89+
from landoapi.storage import db_subsystem
90+
91+
db_subsystem.ensure_ready()
92+
ConfigurationVariable.set(LandingWorker.STOP_KEY, VariableType.BOOL, "1")
93+
94+
95+
@cli.command(name="start-revision-worker")
96+
@click.argument("role")
97+
def start_revision_worker(role):
98+
from landoapi.app import auth0_subsystem, lando_ui_subsystem
99+
from landoapi.workers.revision_worker import RevisionWorker, Supervisor, Processor
100+
101+
roles = {
102+
"processor": Processor,
103+
"supervisor": Supervisor,
104+
}
105+
106+
if role not in roles:
107+
raise ValueError(f"Unknown worker role specified ({role}).")
108+
109+
exclusions = [auth0_subsystem, lando_ui_subsystem]
110+
for system in get_subsystems(exclude=exclusions):
111+
system.ensure_ready()
112+
113+
ConfigurationVariable.set(RevisionWorker.STOP_KEY, VariableType.BOOL, "0")
114+
115+
worker = roles[role]()
116+
worker.start()
117+
118+
119+
@cli.command(name="stop-revision-worker")
120+
def stop_revision_worker():
121+
"""Stops all revision workers (supervisor and processors)."""
122+
from landoapi.workers.revision_worker import RevisionWorker
123+
from landoapi.storage import db_subsystem
124+
125+
db_subsystem.ensure_ready()
126+
RevisionWorker.stop()
127+
128+
85129
@cli.command(name="run-pre-deploy-sequence")
86130
def run_pre_deploy_sequence():
87131
"""Runs the sequence of commands required before a deployment."""

landoapi/commit_message.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
IRC_NICK = r"[a-zA-Z0-9\-\_.]*[a-zA-Z0-9\-\_]+"
4646

4747
# fmt: off
48-
REVIEWERS_RE = re.compile( # noqa: E131
48+
REVIEWERS_RE = re.compile(
4949
r"([\s\(\.\[;,])" # before "r" delimiter
5050
+ r"(" + SPECIFIER + r")" # flag
5151
+ r"(" # capture all reviewers
@@ -209,3 +209,6 @@ def bug_list_to_commit_string(bug_ids: Iterable[str]) -> str:
209209
return "No bug"
210210

211211
return f"Bug {', '.join(sorted(set(bug_ids)))}"
212+
213+
214+
# flake8: noqa: E131

landoapi/hg.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# This Source Code Form is subject to the terms of the Mozilla Public
22
# License, v. 2.0. If a copy of the MPL was not distributed with this
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4-
import copy
54
import configparser
5+
import copy
66
import logging
77
import os
88
import shlex
@@ -650,3 +650,13 @@ def read_checkout_file(self, path: str) -> str:
650650

651651
with checkout_file_path.open() as f:
652652
return f.read()
653+
654+
def has_incoming(self, source: str) -> bool:
655+
"""Check if there are any incoming changes from the remote repo."""
656+
try:
657+
self.run_hg(["incoming", source, "--limit", "1"])
658+
except hglib.error.CommandError as e:
659+
if b"no changes found" not in e.out:
660+
logger.error(e)
661+
return False
662+
return True

landoapi/models/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55
from landoapi.models.landing_job import LandingJob
6-
from landoapi.models.revisions import Revision
76
from landoapi.models.secapproval import SecApprovalRequest
87
from landoapi.models.transplant import Transplant
98
from landoapi.models.configuration import ConfigurationVariable
10-
from landoapi.models.revisions import DiffWarning
9+
from landoapi.models.revisions import DiffWarning, Revision
1110

1211
__all__ = [
1312
"LandingJob",

0 commit comments

Comments
 (0)