1#!/usr/bin/env python
2#
3# Copyright (C) 2018 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18from webapp.src import vtslab_status as Status
19from webapp.src.utils import email_util
20
21
22def UpdateParentSchedule(job, status):
23    """Updates a parent schedule of the given job with status.
24
25    Args:
26        job: a JobModel entity.
27        status: an integer, job status value.
28    """
29    if status not in [
30        Status.JOB_STATUS_DICT["complete"],
31        Status.JOB_STATUS_DICT["infra-err"],
32        Status.JOB_STATUS_DICT["expired"],
33        Status.JOB_STATUS_DICT["bootup-err"]
34    ]:
35        return
36
37    if job.parent_schedule:
38        schedule = job.parent_schedule.get()
39        if schedule:
40            previous_suspended = schedule.suspended
41            if schedule.error_count is None:
42                schedule.error_count = 0
43            if status == Status.JOB_STATUS_DICT["complete"]:
44                schedule.error_count = 0
45                schedule.suspended = False
46            elif status in [
47                Status.JOB_STATUS_DICT["infra-err"],
48                Status.JOB_STATUS_DICT["expired"],
49                Status.JOB_STATUS_DICT["bootup-err"]
50            ]:
51                schedule.error_count += 1
52                if schedule.error_count >= Status.NUM_ERRORS_FOR_SUSPENSION:
53                    schedule.suspended = True
54            schedule.put()
55            if previous_suspended != schedule.suspended:
56                email_util.send_schedule_suspension_notification(schedule)
57