aboutsummaryrefslogtreecommitdiff
path: root/src_py/hatter/server.py
blob: bce58801d8d8affea6a9a56475687de0d3d0b61d (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import asyncio
import collections
import contextlib
import itertools
import multiprocessing
import subprocess
import sys
import time
import typing

from hat import aio
from hat import json

from hatter import common
import hatter.backend


async def create(conf: json.Data,
                 backend: hatter.backend.Backend
                 ) -> 'Server':
    server = Server()
    server._conf = conf
    server._backend = backend
    server._async_group = aio.Group()
    server._repos = set(conf['repos'].keys())
    server._run_queue = aio.Queue()
    server._sync_events = {}

    for repo, repo_conf in conf['repos'].items():
        sync_event = asyncio.Event()
        server._sync_events[repo] = sync_event
        server.async_group.spawn(server._sync_loop, repo, repo_conf,
                                 sync_event)

    for _ in range(multiprocessing.cpu_count()):
        server.async_group.spawn(server._run_loop)

    try:
        commits = await backend.get_commits(repo=None,
                                            statuses={common.Status.PENDING,
                                                      common.Status.RUNNING},
                                            order=common.Order.ASC)

        for commit in commits:
            commit = commit._replace(change=int(time.time()),
                                     status=common.Status.PENDING,
                                     output='')
            await backend.update_commit(commit)
            server._run_queue.put_nowait(commit)

    except BaseException:
        await aio.uncancellable(server.async_close())
        raise

    return server


class Server(aio.Resource):

    @property
    def async_group(self):
        return self._async_group

    @property
    def repos(self) -> typing.Set[str]:
        return self._repos

    async def get_commits(self,
                          repo: typing.Optional[str],
                          ) -> typing.List[common.Commit]:
        return await self._backend.get_commits(repo=repo,
                                               statuses=None,
                                               order=common.Order.DESC)

    async def get_commit(self,
                         repo: str,
                         commit_hash: str
                         ) -> typing.Optional[common.Commit]:
        return await self._backend.get_commit(repo, commit_hash)

    async def run_commit(self,
                         repo: str,
                         commit_hash: str
                         ) -> common.Commit:
        commit = common.Commit(repo=repo,
                               hash=commit_hash,
                               change=int(time.time()),
                               status=common.Status.PENDING,
                               output='')
        await self._backend.update_commit(commit)
        self._run_queue.put_nowait(commit)
        return commit

    def sync_repo(self, repo: str):
        self._sync_events[repo].set()

    async def remove_commit(self, commit: common.Commit):
        await self._backend.remove_commit(commit)

    async def _sync_loop(self, repo, repo_conf, sync_event):
        try:
            url = repo_conf['url']
            refs = repo_conf.get('refs', ['refs/heads/*'])
            min_sync_delay = repo_conf.get('min_sync_delay') or 0
            max_sync_delay = repo_conf.get('max_sync_delay')
            last_sync = time.monotonic() - min_sync_delay

            while True:
                dt = time.monotonic() - last_sync
                if dt < min_sync_delay:
                    await asyncio.sleep(min_sync_delay - dt)

                sync_event.clear()
                commit_hashes = await _git_ls_remote(url, refs)
                last_sync = time.monotonic()

                for commit_hash in commit_hashes:
                    commit = await self._backend.get_commit(repo, commit_hash)
                    if commit:
                        continue
                    await self.run_commit(repo, commit_hash)

                if max_sync_delay is None:
                    await sync_event.wait()

                else:
                    with contextlib.suppress(asyncio.TimeoutError):
                        await asyncio.wait_for(sync_event.wait(),
                                               max_sync_delay)

        finally:
            self.close()

    async def _run_loop(self):
        try:
            while True:
                commit = await self._run_queue.get()
                repo_conf = self._conf['repos'][commit.repo]
                action = repo_conf.get('action', '.hatter.yaml')
                env = {**self._conf.get('env', {}),
                       **repo_conf.get('env', {})}
                url = repo_conf['url']
                ref = commit.hash

                commit = commit._replace(change=int(time.time()),
                                         status=common.Status.RUNNING,
                                         output='')
                await self._backend.update_commit(commit)

                try:
                    output = await _execute(action=action,
                                            env=env,
                                            url=url,
                                            ref=ref)
                    status = common.Status.SUCCESS

                except Exception as e:
                    output = str(e)
                    status = common.Status.FAILURE

                commit = commit._replace(change=int(time.time()),
                                         status=status,
                                         output=output)
                await self._backend.update_commit(commit)

        finally:
            self.close()


async def _execute(action, env, url, ref):
    cmd = [sys.executable, '-m', 'hatter', 'execute',
           '--action', action,
           *itertools.chain.from_iterable(('--env', i) for i in env),
           url, ref]

    p = await asyncio.create_subprocess_exec(cmd,
                                             stdin=subprocess.DEVNULL,
                                             stdout=subprocess.PIPE,
                                             stderr=subprocess.STDOUT,
                                             env=env)

    try:
        output, _ = await p.communicate()
        output = str(output, encoding='utf-8', errors='ignore')

        if p.returncode:
            raise Exception(output)

        return output

    finally:
        if p.returncode is None:
            p.terminate()


async def _git_ls_remote(url, refs):
    cmd = ['git', 'ls-remote', url, *refs]

    p = await asyncio.create_subprocess_exec(cmd,
                                             stdin=subprocess.DEVNULL,
                                             stdout=subprocess.PIPE,
                                             stderr=subprocess.PIPE)

    try:
        stdout, stderr = await p.communicate()
        if p.returncode:
            stderr = str(stderr, encoding='utf-8', errors='ignore')
            raise Exception(stderr)

        result = collections.deque()
        stdout = str(stdout, encoding='utf-8', errors='ignore')

        for line in stdout.split('\n'):
            segments = line.split(maxsplit=1)
            if not segments:
                continue
            result.append(segments[0])

        return result

    except Exception:
        return []

    finally:
        if p.returncode is None:
            p.terminate()