v / cmd / tools / fast / fast_job.v
111 lines · 91 sloc · 3.35 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4import os
5import time
6
7// This program acts as a service/monitor/daemon, that runs in the background, checks for repo updates,
8// and runs fast.v, when there are changes. It should *never fail*, even if fast.v can fail. In that case,
9// this program should just loop, trying again on the next iteration of the loop, and hoping that the repo
10// was fixed enough, so that fast.v can succeed.
11
12// It is the responsibility of * fast.v * , to do all measurements, process them, and to pushes the HTML result
13// of that processing to the fast.vlang.io GH pages repo.
14
15const sleep_period = 60
16
17const fast_dir = os.dir(@FILE)
18
19const vdir = os.real_path(os.dir(os.dir(os.dir(fast_dir))))
20
21const vexe = os.real_path(os.join_path(vdir, 'v'))
22
23const old_path = os.getenv('PATH')
24
25fn elog(msg string) {
26 eprintln('${time.now().format_ss_micro()} ${msg}')
27}
28
29fn delay() {
30 elog('Sleeping for ${sleep_period} seconds...')
31 time.sleep(sleep_period * time.second)
32}
33
34fn check_output_repo(url string, branch string, folder string) {
35 if os.exists(folder) {
36 elog('Note: ${folder} already exists; using it.')
37 return
38 }
39 cmd := 'git clone --filter=blob:none --branch=${branch} ${url} ${folder}'
40 elog('Note: ${folder} is missing. Cloning to `${folder}`, with: `${cmd}` ...')
41 res := os.system(cmd)
42 elog('... cloning done, result: ${res}')
43}
44
45fn main() {
46 elog('fast_job start setup ...')
47 // ensure a more stable working environment for the used tools, independent on how this executable was started:
48 os.setenv('LANG', 'C', true)
49 os.setenv('PATH', '${vdir}:${old_path}', true)
50 elog('fast_job fast_dir: ${fast_dir}')
51 elog('fast_job vdir: ${vdir}')
52 elog('fast_job vexe: ${vexe}')
53 elog('fast_job PATH: ${os.getenv('PATH')}')
54
55 os.chdir(fast_dir)!
56 elog('fast_job start in os.getwd(): ${os.getwd()}')
57
58 defer {
59 elog('fast_job end')
60 }
61
62 elog('fast_job clone output repos...')
63 check_output_repo('https://github.com/vlang/website', 'gh-pages', 'fast.vlang.io/')
64 check_output_repo('https://github.com/vlang/docs/', 'main', 'docs.vlang.io/')
65 check_output_repo('https://github.com/vlang/docs/', 'generator',
66 'docs.vlang.io/docs_generator/')
67
68 mut i := 0
69 for {
70 i++
71 elog('------------------- Checking for updates, loop: ${i} ... -------------------')
72 os.chdir(fast_dir)!
73
74 res_pull := os.execute('git pull --rebase')
75 elog('> res_pull.output: ${res_pull.output}')
76 if res_pull.exit_code != 0 {
77 elog('Git pull failed. You may have uncommitted changes?')
78 delay()
79 continue
80 }
81 if res_pull.output.contains('Already up to date.') {
82 if os.args.contains('-force-update') {
83 elog('The repository was already updated, but -force-update was passed too.')
84 } else {
85 delay()
86 continue
87 }
88 }
89
90 elog('recompiling V')
91 os.system('${os.quoted_path(vexe)} self')
92 os.system('ls -la ${os.quoted_path(vexe)}')
93
94 elog('recompiling ./fast')
95 recompile_fast_v_code := os.system('${os.quoted_path(vexe)} -keepc -g fast.v')
96 if recompile_fast_v_code != 0 {
97 elog('WARNING: could not recompile fast.v')
98 delay()
99 continue
100 }
101 os.system('ls -la fast fast.v')
102
103 elog('running ./fast -upload')
104 fast_exit_code := os.system('./fast -upload')
105 if fast_exit_code != 0 {
106 println('fast_exit_code = ${fast_exit_code}, != 0')
107 }
108
109 delay()
110 }
111}
112