Skip to content

Commit c15d774

Browse files
committed
Improve query from feedback
1 parent 164d17b commit c15d774

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

readthedocs/projects/tasks.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from readthedocs.core.symlink import PublicSymlink, PrivateSymlink
4444
from readthedocs.core.utils import send_email, broadcast
4545
from readthedocs.doc_builder.config import load_yaml_config
46+
from readthedocs.doc_builder.constants import DOCKER_LIMITS
4647
from readthedocs.doc_builder.environments import (LocalEnvironment,
4748
DockerEnvironment)
4849
from readthedocs.doc_builder.exceptions import BuildEnvironmentError
@@ -1011,22 +1012,44 @@ def sync_callback(_, version_pk, commit, *args, **kwargs):
10111012

10121013
@app.task()
10131014
def finish_inactive_builds():
1014-
delta = datetime.timedelta(hours=1, minutes=30)
1015+
"""
1016+
Finish inactive builds.
1017+
1018+
A build is consider inactive if it's not in ``FINISHED`` state and it has been
1019+
"running" for more time that the allowed one (``Project.container_time_limit``
1020+
or ``DOCKER_LIMITS['time']`` plus a 20% of it).
1021+
1022+
These inactive builds will be marked as ``success`` and ``FINISHED`` with an
1023+
``error`` to be communicated to the user.
1024+
"""
1025+
time_limit = int(DOCKER_LIMITS['time'] * 1.2)
1026+
delta = datetime.timedelta(minutes=time_limit)
10151027
query = (~Q(state=BUILD_STATE_FINISHED) &
10161028
Q(date__lte=datetime.datetime.now() - delta))
10171029

1030+
builds_finished = 0
10181031
builds = Build.objects.filter(query)[:50]
10191032
for build in builds:
1033+
1034+
if build.project.container_time_limit:
1035+
custom_delta = datetime.timedelta(
1036+
minutes=int(build.project.container_time_limit))
1037+
if build.date + custom_delta > datetime.datetime.now():
1038+
# Do not mark as FINISHED builds with a custom time limit that wasn't
1039+
# expired yet (they are still building the project version)
1040+
continue
1041+
10201042
build.success = False
10211043
build.state = BUILD_STATE_FINISHED
1022-
build.error = (
1044+
build.error = _(
10231045
'This build was terminated due to inactivity. If you '
10241046
'continue to encounter this error, file a support '
10251047
'request with and reference this build id ({0}).'.format(build.pk)
10261048
)
10271049
build.save()
1050+
builds_finished += 1
10281051

10291052
log.info(
10301053
'Builds marked as "Terminated due inactivity": %s',
1031-
builds.count(),
1054+
builds_finished,
10321055
)

0 commit comments

Comments
 (0)