summaryrefslogtreecommitdiff
path: root/lorrycontroller/jobupdate.py
blob: 4a54c7330b9a73cba39ff75e8f19dbc9c97092a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Copyright (C) 2014  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import logging
import time

import bottle

import lorrycontroller


class JobUpdate(lorrycontroller.LorryControllerRoute):

    http_method = 'POST'
    path = '/1.0/job-update'

    def run(self, **kwargs):
        logging.info('%s %s called', self.http_method, self.path)

        job_id = int(bottle.request.forms.job_id)
        exit = bottle.request.forms.exit
        stdout = bottle.request.forms.stdout
        stderr = bottle.request.forms.stderr

        logging.info('Job %s updated (exit=%s)', job_id, exit)

        statedb = self.open_statedb()
        with statedb:
            if stdout:
                statedb.append_to_job_output(job_id, stdout)
            if stderr:
                statedb.append_to_job_output(job_id, stderr)

            path = statedb.find_lorry_running_job(job_id)
            if exit is not None and exit != 'no':
                lorry_info = statedb.get_lorry_info(path)
                statedb.set_lorry_last_run(path, int(time.time()))
                statedb.set_running_job(path, None)
                statedb.set_job_exit(job_id, exit)
            return statedb.get_lorry_info(path)