source: trunk/server/www/app/models/job.php @ 295

Last change on this file since 295 was 295, checked in by sander, 10 years ago

Link teh requested output formats to individual jobs, not the request as a whole

File size: 2.8 KB
Line 
1<?php
2/**
3 * Officeshots.org - Test your office documents in different applications
4 * Copyright (C) 2009 Stichting Lone Wolves
5 * Written by Sander Marechal <s.marechal@jejik.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21// We need to access the Request model statically for it's state constants
22App::import('Model', 'Request');
23
24/**
25 * The Job model
26 */
27class Job extends AppModel
28{
29        /**
30         * @var array All jobs belong to a request and factory and are associated with a certain application and platform
31         *
32         * The Result seems backwards but we need the foreign key in the jobs table otherwise it's not possible to
33         * search for jobs that have no result yet.
34         */
35        public $belongsTo = array('Request' => array('counterCache' => true),
36                                  'Platform', 'Application', 'Factory', 'Result', 'Format');
37
38        /** @var array Job queries can be quite complex, so use Containable */
39        public $actsAs = array('Containable');
40
41        /**
42         * Manually update the counterCaches for Request
43         *
44         * We use a manual counterCache because we need two counts on Request. CakePHP's counterCache can only take one.
45         *
46         * @param array $keys The Job model data
47         * @param boolean $created Ignored, like in the Model class
48         */
49        public function updateCounterCache($keys = array(), $created = false)
50        {
51                $request_id   = $this->field('request_id');
52                $job_count    = $this->find('count', array('conditions' => array('request_id' => $request_id)));
53                $result_count = $this->find('count', array('conditions' => array('request_id' => $request_id, 'result_id <>' => '')));
54
55                $result = $this->Request->updateAll(
56                        array(
57                                'Request.job_count' => $job_count,
58                                'Request.result_count' => $result_count
59                        ),
60                        array('Request.id' => $request_id)
61                );
62
63                // If this was the last job, change Request state to finished
64                $this->Request->id = $request_id;
65                if ($this->Request->field('state') == Request::STATE_QUEUED && $job_count == $result_count && $job_count > 0) {
66                        $this->Request->saveField('state', Request::STATE_FINISHED);
67                }
68        }
69
70        /**
71         * Remove the result before removing the Job
72         */
73        public function beforeDelete($cascade)
74        {
75                $result_id = $this->field('result_id');
76
77                if ($result_id) {
78                        $this->Result->delete($result_id);
79                }
80
81                return true;
82        }
83}
84
85?>
Note: See TracBrowser for help on using the repository browser.