triggers.sqlite 4.58 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*

DESCRIPTION

    Triggers for automatic synchronization (currently off by default)


LICENSE

10
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
nwillhoft's avatar
nwillhoft committed
11
    Copyright [2016-2021] EMBL-European Bioinformatics Institute
12 13 14 15 16 17 18 19 20 21

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.

22 23 24 25
CONTACT

    Please subscribe to the Hive mailing list:  http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users  to discuss Hive-related questions or to be notified of our updates

26 27
*/

28 29 30 31 32

CREATE TRIGGER add_job AFTER INSERT ON job
FOR EACH ROW
BEGIN
    UPDATE analysis_stats SET
33 34 35 36 37
        total_job_count         = total_job_count       + 1,
        semaphored_job_count    = semaphored_job_count  + (CASE NEW.status WHEN 'SEMAPHORED'    THEN 1                         ELSE 0 END),
        ready_job_count         = ready_job_count       + (CASE NEW.status WHEN 'READY'         THEN 1                         ELSE 0 END),
        done_job_count          = done_job_count        + (CASE NEW.status WHEN 'DONE'          THEN 1 WHEN 'PASSED_ON' THEN 1 ELSE 0 END),
        failed_job_count        = failed_job_count      + (CASE NEW.status WHEN 'FAILED'        THEN 1                         ELSE 0 END),
38 39 40 41 42 43 44 45
        status              = (CASE WHEN status!='BLOCKED' THEN 'LOADING' ELSE 'BLOCKED' END)
    WHERE analysis_id = NEW.analysis_id;
END;

CREATE TRIGGER delete_job AFTER DELETE ON job
FOR EACH ROW
BEGIN
    UPDATE analysis_stats SET
46 47 48 49 50
        total_job_count         = total_job_count       - 1,
        semaphored_job_count    = semaphored_job_count  - (CASE OLD.status WHEN 'SEMAPHORED'    THEN 1                         ELSE 0 END),
        ready_job_count         = ready_job_count       - (CASE OLD.status WHEN 'READY'         THEN 1                         ELSE 0 END),
        done_job_count          = done_job_count        - (CASE OLD.status WHEN 'DONE'          THEN 1 WHEN 'PASSED_ON' THEN 1 ELSE 0 END),
        failed_job_count        = failed_job_count      - (CASE OLD.status WHEN 'FAILED'        THEN 1                         ELSE 0 END)
51 52 53 54 55 56 57
    WHERE analysis_id = OLD.analysis_id;
END;

CREATE TRIGGER update_job AFTER UPDATE ON job
FOR EACH ROW
BEGIN
    UPDATE analysis_stats SET
58 59 60 61 62 63

        total_job_count         = total_job_count       - 1,
        semaphored_job_count    = semaphored_job_count  - (CASE OLD.status WHEN 'SEMAPHORED'    THEN 1                         ELSE 0 END),
        ready_job_count         = ready_job_count       - (CASE OLD.status WHEN 'READY'         THEN 1                         ELSE 0 END),
        done_job_count          = done_job_count        - (CASE OLD.status WHEN 'DONE'          THEN 1 WHEN 'PASSED_ON' THEN 1 ELSE 0 END),
        failed_job_count        = failed_job_count      - (CASE OLD.status WHEN 'FAILED'        THEN 1                         ELSE 0 END)
64 65
    WHERE analysis_id = OLD.analysis_id;
    UPDATE analysis_stats SET
66 67 68 69 70
        total_job_count         = total_job_count       + 1,
        semaphored_job_count    = semaphored_job_count  + (CASE NEW.status WHEN 'SEMAPHORED'    THEN 1                         ELSE 0 END),
        ready_job_count         = ready_job_count       + (CASE NEW.status WHEN 'READY'         THEN 1                         ELSE 0 END),
        done_job_count          = done_job_count        + (CASE NEW.status WHEN 'DONE'          THEN 1 WHEN 'PASSED_ON' THEN 1 ELSE 0 END),
        failed_job_count        = failed_job_count      + (CASE NEW.status WHEN 'FAILED'        THEN 1                         ELSE 0 END)
71 72 73 74 75 76 77 78 79
    WHERE analysis_id = NEW.analysis_id;
END;


CREATE TRIGGER add_worker AFTER INSERT ON worker
FOR EACH ROW
BEGIN
    UPDATE analysis_stats SET
        num_running_workers = num_running_workers + 1
80 81 82
    WHERE analysis_id = NEW.analysis_id;
END;

83 84 85 86 87 88 89 90 91 92 93 94
CREATE TRIGGER update_worker AFTER UPDATE ON worker
FOR EACH ROW
BEGIN
    UPDATE analysis_stats SET
        num_running_workers = num_running_workers - 1
    WHERE analysis_id = NEW.analysis_id
      AND OLD.status <> 'DEAD'
      AND NEW.status =  'DEAD';
END;


    -- inform the runtime part of the system that triggers are in place:
95
INSERT INTO hive_meta (meta_key, meta_value) VALUES ('hive_use_triggers', '1');