aboutsummaryrefslogtreecommitdiff
path: root/gn_libs/scripts/worker.py
blob: 2479aef1b0a878ec49593664aa82fa4231de4807 (about) (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
import sys
import shlex
import argparse
import traceback

from gn_libs import jobs, sqlite3


def run_job(conn, job_id):
    """Run the job."""
    try:
        pass
    except:
        jobs.update_metadata(conn, args.job_id, "status", "error")
        jobs.push_to_stream(conn, args.job_id, "stderr", traceback.format_exc())
        return 4


def parse_args():
    """Define and parse CLI args."""
    parser = argparse.ArgumentParser(
        prog="GN-Libs Worker",
        description = (
            "Generic worker to launch and manage jobs defined with gn-libs"))
    parser.add_argument(
        "jobs_db_uri",
        help="The URI to the SQLite3 database holding the jobs' details")
    parser.add_argument("job_id", help="The id of the job being processed")
    return parser.parse_args()

def main():
    """Entry-point to this program."""
    args = parse_args()
    with (sqlite3.connection(args.jobs_db_uri) as conn,
          sqlite3.cursor(conn) as cursor):
        job = jobs.job(conn, args.job_id)
        if job:
            return run_job(conn, args.job_id)

        jobs.update_metadata(conn, args.job_id, "status", "error")
        jobs.push_to_stream(conn, args.job_id, "stderr", "Job not found!")
        return 2

    return 3



if __name__ == "__main__":
    sys.exit(main())