[PATCH 0/8]
cover [PATCH 0/8]
Series to improve process handling of runbuilds and one additional patch for fixing checking out HEAD as a reference. Changes reviewed on toaster mailing list.
Copy of process handling patchset cover letter:
This patchset introduced a bit lighter way of handling builds. Instead of polling database every second we process builds only when runbuilds receives SIGUSR1. The signal is sent when build is either scheduled, canceled or finished. A little bit of code cleanup is also included into this patchset.
Test instructions:
- Create multiple builds in UI. Check if they'll be built sequentially
- Create multiple builds in UI, cancel one. Check if the next one starts building.
- Repeat first two test cases when command line build is run. Only one UI build should be run in parallel with command line build.
- Test any other combinations of running and cancelling builds to make sure there are no regressions.
Ed Bartosh (8):
toaster: fix checking of repository url
toaster: implement signal_runbuilds function
toaster: notify runbuilds when build status changes
runbuilds: process builds on SIGUSR1
runbuilds: process builds on start
runbuilds: code cleanup - whitespaces, long lines
runbuilds: code cleanup - remove unused imports
toaster: unlock BuildEnvirnoment when build is done
lib/bb/ui/buildinfohelper.py | 12 ++++-- lib/toaster/bldcontrol/localhostbecontroller.py | 2 +- .../bldcontrol/management/commands/runbuilds.py | 49 ++++++++++------------ lib/toaster/orm/models.py | 10 ++++- 4 files changed, 42 insertions(+), 31 deletions(-)
Signature · 1 line
2.7.4
PATCH 1/8 toaster: fix checking of repository url
From: Ed Bartosh <[email protected]>
Toaster checks gir repository url is the same as locally cloned before checking it out to existing local clone. This check can be skipped if commit is 'HEAD' as in this case repository is not hard reset to commit, so the local clone won't be changed.
[YOCTO #10163]
--- lib/toaster/bldcontrol/localhostbecontroller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/toaster/bldcontrol/localhostbecontroller.py +1 -1
@@ -156,7 +156,7 @@ class LocalhostBEController(BuildEnvironmentController): try: localremotes = self._shellcmd("git remote -v", localdirname) - if not giturl in localremotes: + if not giturl in localremotes and commit != 'HEAD': raise BuildSetupException("Existing git repository at %s, but with different remotes ('%s', expected '%s'). Toaster will not continue out of fear of damaging something." % (localdirname, ", ".join(localremotes.split("\n")), giturl)) except ShellCmdException: # our localdirname might not be a git repository
Signature · 1 line
2.7.4
PATCH 2/8 toaster: implement signal_runbuilds function
In reply to Michael WoodFrom: Ed Bartosh <[email protected]>
This function reads pid of runbuilds process from BUILDDIR/.runbuilds.pid and sends SIGUSR1 to it. signal_runbuilds function will be used in Toaster code to notify runbuilds when build is scheduled, finished or cancelled.
--- lib/toaster/orm/models.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
lib/toaster/orm/models.py +7 -1
@@ -33,9 +33,10 @@ from django.conf import settings import django.db.models.signals import sys -import os.path +import os import re import itertools +from signal import SIGUSR1 import logging logger = logging.getLogger("toaster")
@@ -1736,6 +1737,11 @@ def invalidate_cache(**kwargs): except Exception as e: logger.warning("Problem with cache backend: Failed to clear cache: %s" % e) +def signal_runbuilds(): + """Send SIGUSR1 to runbuilds process""" + with open(os.path.join(os.getenv('BUILDDIR'), '.runbuilds.pid')) as pidf: + os.kill(int(pidf.read()), SIGUSR1) + django.db.models.signals.post_save.connect(invalidate_cache) django.db.models.signals.post_delete.connect(invalidate_cache) django.db.models.signals.m2m_changed.connect(invalidate_cache)
Signature · 1 line
2.7.4
PATCH 3/8 toaster: notify runbuilds when build status changes
In reply to Michael WoodFrom: Ed Bartosh <[email protected]>
Called signal_runbuilds API when build is scheduled, cancelled or finished to notify runbuilds process about builds status change.
[YOCTO #8918]
--- lib/bb/ui/buildinfohelper.py | 4 ++++ lib/toaster/orm/models.py | 2 ++ 2 files changed, 6 insertions(+)
lib/bb/ui/buildinfohelper.py +4 -0
@@ -43,6 +43,7 @@ from orm.models import Package, Package_File, Target_Installed_Package, Target_F from orm.models import Task_Dependency, Package_Dependency from orm.models import Recipe_Dependency, Provides from orm.models import Project, CustomImagePackage, CustomImageRecipe +from orm.models import signal_runbuilds from bldcontrol.models import BuildEnvironment, BuildRequest
@@ -234,6 +235,7 @@ class ORMWrapper(object): build.completed_on = timezone.now() build.outcome = outcome build.save() + signal_runbuilds() def update_target_set_license_manifest(self, target, license_manifest_path): target.license_manifest_path = license_manifest_path
@@ -1354,6 +1356,7 @@ class BuildInfoHelper(object): self._ensure_build() self.internal_state['build'].outcome = Build.CANCELLED self.internal_state['build'].save() + signal_runbuilds() def store_dependency_information(self, event): assert '_depgraph' in vars(event)
@@ -1540,6 +1543,7 @@ class BuildInfoHelper(object): else: br.state = BuildRequest.REQ_FAILED br.save() + signal_runbuilds() def store_log_error(self, text): mockevent = MockEvent()
lib/toaster/orm/models.py +2 -0
@@ -377,6 +377,8 @@ class Project(models.Model): except ProjectVariable.DoesNotExist: pass br.save() + signal_runbuilds() + except Exception: # revert the build request creation since we're not done cleanly br.delete()
Signature · 1 line
2.7.4
PATCH 4/8 runbuilds: process builds on SIGUSR1
In reply to Michael WoodFrom: Ed Bartosh <[email protected]>
Run main processing function 'runbuild' only if SIGUSR1 is received. This signal is sent by Toaster when build status is changed (either started, cancelled or finished).
This should stop continuous database polling as run_builds function will be called only when needed, i.e. after build status is changed.
[YOCTO #8918]
--- lib/toaster/bldcontrol/management/commands/runbuilds.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
lib/toaster/bldcontrol/management/commands/runbuilds.py +4 -2
@@ -11,9 +11,9 @@ from orm.models import Build, ToasterSetting, LogMessage, Target import os import logging -import time import sys import traceback +import signal logger = logging.getLogger("toaster")
@@ -175,6 +175,8 @@ class Command(NoArgsCommand): logger.warn("runbuilds: schedule exception %s" % str(e)) def handle_noargs(self, **options): + signal.signal(signal.SIGUSR1, lambda sig, frame: None) + while True: + signal.pause() self.runbuild() - time.sleep(1)
Signature · 1 line
2.7.4
PATCH 5/8 runbuilds: process builds on start
In reply to Michael WoodFrom: Ed Bartosh <[email protected]>
If Toaster is stopped incorrectly there could be some
build requests and builds in incorrect state left from the previous run. Running main processing function on start should take care of those.
--- lib/toaster/bldcontrol/management/commands/runbuilds.py | 2 ++ 1 file changed, 2 insertions(+)
lib/toaster/bldcontrol/management/commands/runbuilds.py +2 -0
@@ -175,6 +175,8 @@ class Command(NoArgsCommand): logger.warn("runbuilds: schedule exception %s" % str(e)) def handle_noargs(self, **options): + self.runbuild() + signal.signal(signal.SIGUSR1, lambda sig, frame: None) while True:
Signature · 1 line
2.7.4
PATCH 6/8 runbuilds: code cleanup - whitespaces, long lines
In reply to Michael WoodFrom: Ed Bartosh <[email protected]>
Fixed following pylint warnings:
C0330(bad-continuation): Wrong hanging indentation before block. C0326(bad-whitespace): No space allowed around keyword argument assignment C0326(bad-whitespace): Exactly one space required before assignment C0301(line-too-long): Line too long
--- .../bldcontrol/management/commands/runbuilds.py | 34 ++++++++++------------ 1 file changed, 15 insertions(+), 19 deletions(-)
lib/toaster/bldcontrol/management/commands/runbuilds.py +15 -19
@@ -18,14 +18,13 @@ import signal logger = logging.getLogger("toaster") class Command(NoArgsCommand): - args = "" - help = "Schedules and executes build requests as possible." - "Does not return (interrupt with Ctrl-C)" - + args = "" + help = "Schedules and executes build requests as possible. "\ + "Does not return (interrupt with Ctrl-C)" @transaction.atomic def _selectBuildEnvironment(self): - bec = getBuildEnvironmentController(lock = BuildEnvironment.LOCK_FREE) + bec = getBuildEnvironmentController(lock=BuildEnvironment.LOCK_FREE) bec.be.lock = BuildEnvironment.LOCK_LOCK bec.be.save() return bec
@@ -75,17 +74,15 @@ class Command(NoArgsCommand): else: errmsg = str(e) - BRError.objects.create(req = br, - errtype = str(type(e)), - errmsg = errmsg, - traceback = traceback.format_exc()) + BRError.objects.create(req=br, errtype=str(type(e)), errmsg=errmsg, + traceback=traceback.format_exc()) br.state = BuildRequest.REQ_FAILED br.save() bec.be.lock = BuildEnvironment.LOCK_FREE bec.be.save() def archive(self): - for br in BuildRequest.objects.filter(state = BuildRequest.REQ_ARCHIVE): + for br in BuildRequest.objects.filter(state=BuildRequest.REQ_ARCHIVE): if br.build == None: br.state = BuildRequest.REQ_FAILED else:
@@ -102,14 +99,13 @@ class Command(NoArgsCommand): BuildRequest.REQ_COMPLETED, BuildRequest.REQ_CANCELLING]) & Q(lock=BuildEnvironment.LOCK_LOCK) & - Q(updated__lt=timezone.now() - timedelta(seconds = 30)) + Q(updated__lt=timezone.now() - timedelta(seconds=30)) ).update(lock=BuildEnvironment.LOCK_FREE) # update all Builds that were in progress and failed to start - for br in BuildRequest.objects.filter( - state=BuildRequest.REQ_FAILED, - build__outcome=Build.IN_PROGRESS): + for br in BuildRequest.objects.filter(state=BuildRequest.REQ_FAILED, + build__outcome=Build.IN_PROGRESS): # transpose the launch errors in ToasterExceptions br.build.outcome = Build.FAILED for brerror in br.brerror_set.all():
@@ -126,7 +122,7 @@ class Command(NoArgsCommand): # update all BuildRequests without a build created - for br in BuildRequest.objects.filter(build = None): + for br in BuildRequest.objects.filter(build=None): br.build = Build.objects.create(project=br.project, completed_on=br.updated, started_on=br.created)
@@ -151,10 +147,10 @@ class Command(NoArgsCommand): # Make sure the LOCK is removed for builds which have been fully # cancelled - for br in BuildRequest.objects.filter( - Q(build__outcome=Build.CANCELLED) & - Q(state=BuildRequest.REQ_CANCELLING) & - ~Q(environment=None)): + for br in BuildRequest.objects.filter(\ + Q(build__outcome=Build.CANCELLED) & + Q(state=BuildRequest.REQ_CANCELLING) & + ~Q(environment=None)): br.environment.lock = BuildEnvironment.LOCK_FREE br.environment.save()
Signature · 1 line
2.7.4
PATCH 7/8 runbuilds: code cleanup - remove unused imports
In reply to Michael WoodFrom: Ed Bartosh <[email protected]>
Fixed pylint warning: W0611(unused-import): Unused import
--- lib/toaster/bldcontrol/management/commands/runbuilds.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
lib/toaster/bldcontrol/management/commands/runbuilds.py +2 -5
@@ -1,17 +1,14 @@ -from django.core.management.base import NoArgsCommand, CommandError +from django.core.management.base import NoArgsCommand from django.db import transaction from django.db.models import Q from bldcontrol.bbcontroller import getBuildEnvironmentController -from bldcontrol.bbcontroller import ShellCmdException, BuildSetupException from bldcontrol.models import BuildRequest, BuildEnvironment from bldcontrol.models import BRError, BRVariable -from orm.models import Build, ToasterSetting, LogMessage, Target +from orm.models import Build, LogMessage, Target -import os import logging -import sys import traceback import signal
Signature · 1 line
2.7.4
PATCH 8/8 toaster: unlock BuildEnvirnoment when build is done
In reply to Michael WoodFrom: Ed Bartosh <[email protected]>
There is no need to lock build environment before changing build status as this operation is very fast. However, there is a need to unlock it after changing build status.
Explicitly unlocked BuildEnvironment after build reaches final status SUCCEEDED, FAILED or CANCELLED. This should allow runbuilds process to pickup next build faster.
--- lib/bb/ui/buildinfohelper.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
lib/bb/ui/buildinfohelper.py +5 -3
@@ -1524,9 +1524,7 @@ class BuildInfoHelper(object): return br_id, be_id = self.brbe.split(":") - be = BuildEnvironment.objects.get(pk = be_id) - be.lock = BuildEnvironment.LOCK_LOCK - be.save() + br = BuildRequest.objects.get(pk = br_id) # if we're 'done' because we got cancelled update the build outcome
@@ -1543,6 +1541,10 @@ class BuildInfoHelper(object): else: br.state = BuildRequest.REQ_FAILED br.save() + + be = BuildEnvironment.objects.get(pk = be_id) + be.lock = BuildEnvironment.LOCK_FREE + be.save() signal_runbuilds() def store_log_error(self, text):
Signature · 1 line
2.7.4