taskstable.py 6.17 KB
Newer Older
1
2
3
4
5
6
from hashlib import md5
from time import sleep

from pkg_resources import iter_entry_points

from pml import HTML
7
from sqlhelp import select, insert
8
9
10
11
from rework.task import Task


def latest_table_hash(engine, domain):
12
13
14
15
16
17
18
    q = select(
        'hash'
    ).table('rework.taskstable'
    ).order('hash'
    ).limit(1
    ).where(domain=domain)
    return q.do(engine).scalar()
19
20
21
22
23
24
25


def refresh_tasks(engine, inithash, domain):
    taskstates = tasks_info(engine, domain)
    thash = md5(str(taskstates).encode('ascii')).hexdigest()
    if thash != inithash:
        htmltable = generate_tasks_table(engine, taskstates)
26
        q = insert('rework.taskstable').values(
27
28
29
30
            hash=thash,
            domain=domain,
            content=htmltable
        )
Aurélien Campéas's avatar
Aurélien Campéas committed
31
        with engine.begin() as cn:
32
            q.do(cn)
33
34
        inithash = thash
        # cleanup old tables
35
36
37
        sql = ('delete from rework.taskstable '
               'where hash != %(hash)s '
               'and   domain = %(domain)s')
Aurélien Campéas's avatar
Aurélien Campéas committed
38
        with engine.begin() as cn:
39
            cn.execute(sql, hash=thash, domain=domain)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    return inithash


def refresh_tasks_file(engine, loop=False, sleeptime=2):
    domains = [dom for dom, in engine.execute(
        'select domain from rework.operation group by domain order by domain'
    ).fetchall()]
    if len(domains) > 1:
        domains.insert(0, 'all')

    inithashes = {domain: latest_table_hash(engine, domain)
                 for domain in domains}
    inithashes = {domain: refresh_tasks(engine, inithashes[domain], domain)
                  for domain in domains}

    if loop:
        print('Looping. Type Ctrl-C to stop.')
    while loop:
        newhashes = {domain: refresh_tasks(engine, inithashes[domain], domain)
                     for domain in domains}
        if newhashes != inithashes:
            print('tasks set changed')
            inithashes = newhashes
        else:
            print('nothing changed')
        sleep(sleeptime)


def tasks_info(engine, domain):
Aurélien Campéas's avatar
Aurélien Campéas committed
69
    with engine.begin() as cn:
70
71
72
73
74
        q = select(
            't.id', 't.status', 'op.domain'
        ).table('rework.task as t'
        ).join('rework.operation as op on (op.id = t.operation)'
        ).order('t.id', 'desc')
75
        if domain != 'all':
76
77
            q.where(domain=domain)
        return q.do(cn).fetchall()
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92


MORE_TASKS_ACTIONS = set()
def add_plugin_actions():
    for ep in iter_entry_points('tasks_actions'):
        MORE_TASKS_ACTIONS.add(ep.load())

add_plugin_actions()


def generate_tasks_table(engine, taskstates):
    opsql = 'select id, name from rework.operation'
    ops = dict(engine.execute(opsql).fetchall())

    h = HTML()
93
    h.br()
94
95
96
97
98
99
    with h.table(klass='table table-sm table-bordered table-striped table-hover') as t:
        with t.thead(klass='thead-inverse') as th:
            with th.tr() as r:
                r.th('#')
                r.th('service')
                r.th('domain')
100
101
                r.th('queued')
                r.th('started')
102
                r.th('finished')
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
                r.th('user')
                r.th('worker')
                r.th('status')
                r.th('action')
        for row in taskstates:
            job = Task.byid(engine, row.id)
            with t.tr() as r:
                r.th(str(job.tid), scope='row')

                with r.td() as col:
                    with col.span() as sp:
                        sp.a(ops[job.operation],
                             title='show the tasks log (if any)',
                             target='_blank',
                             href='tasklogs/{}'.format(row.id))
                    if job.traceback:
                        with col.span() as sp:
                            sp(' ')
                            sp.a('[traceback]',
                                 title='show the error',
                                 target='_blank',
                                 href='taskerror/{}'.format(row.id))

                r.td(row.domain)
127
128
129
130
131
132
                r.td(job._propvalue('queued').strftime('%Y-%m-%d %H:%M:%S'))
                started = job._propvalue('started')
                if started is None:
                    r.td('')
                else:
                    r.td(started.strftime('%Y-%m-%d %H:%M:%S'))
133
134
135
136
137
                finished = job._propvalue('finished')
                if finished is None:
                    r.td('')
                else:
                    r.td(finished.strftime('%Y-%m-%d %H:%M:%S'))
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

                # user plus maybe run name
                meta = job.metadata
                user = meta.get('user', '<unknown>')
                run_name = meta.get('options', {}).get('run_name', None)
                if run_name:
                    user = '{} [{}]'.format(user, run_name)
                r.td(user)

                worker = job._propvalue('worker')
                r.td('#{}'.format(worker or ''))

                state = job.state
                stateattrs = {'klass': state}
                if state == 'failed':
                    stateattrs['title'] = job.traceback
                r.td(state, **stateattrs)

                with r.td() as col:
                    with col.button() as b:
                        if state == 'running':
                            b('abort', type='button', klass='btn btn-danger btn-sm',
                              onclick='abort_task({})'.format(job.tid))
                        elif state == 'aborting':
                            b('wait', klass='btn glyphicon glyphicon-ban-circle')
                        else:
                            b('delete', type='button', klass='btn btn-warning btn-sm',
                              onclick='delete_task({})'.format(job.tid))
                    if row.status == 'done':
                        col.span(' ')
                        with col.button() as b:
                            b('relaunch', type='button', klass='btn btn-primary btn-sm',
                              onclick='relaunch_task({})'.format(job.tid))
                    for action in MORE_TASKS_ACTIONS:
                        action(col, job, state, ops)

    return str(h)