| 1 | module mymodule |
| 2 | |
| 3 | import time |
| 4 | import os |
| 5 | import v.live |
| 6 | import math |
| 7 | |
| 8 | const some_constant = f64(5.5) |
| 9 | |
| 10 | fn append_to_file(fname string, s string) { |
| 11 | mut f := os.open_append(fname) or { |
| 12 | println('>>>> could not open file ${fname} for appending, err: ${err} ') |
| 13 | return |
| 14 | } |
| 15 | f.write_string(time.now().format_rfc3339_micro()) or {} |
| 16 | f.write_string(' || ') or {} |
| 17 | f.writeln(s) or { |
| 18 | println('>>>> could not write to ${fname}, err: ${err} ') |
| 19 | return |
| 20 | } |
| 21 | // info := live.info() |
| 22 | // f.writeln('>>> reloads: ${info.reloads} | ok reloads: ${info.reloads_ok}') |
| 23 | f.close() |
| 24 | } |
| 25 | |
| 26 | fn myprintln(s string) { |
| 27 | append_to_file('#OUTPUT_FILE#', s) |
| 28 | println(s) |
| 29 | os.flush() |
| 30 | } |
| 31 | |
| 32 | @[live] |
| 33 | fn pmessage() string { |
| 34 | _ := some_constant * math.sin(2.0 * math.pi * f64(time.ticks() % 6000) / 6000) |
| 35 | return 'ORIGINAL' |
| 36 | } |
| 37 | |
| 38 | const delay = 20 |
| 39 | |
| 40 | pub fn mymain() { |
| 41 | mut info := live.info() |
| 42 | info.recheck_period_ms = 5 |
| 43 | myprintln('START') |
| 44 | myprintln('DATE: ' + time.now().str()) |
| 45 | pmessage() |
| 46 | pmessage() |
| 47 | max_cycles := os.getenv_opt('LIVE_CYCLES') or { '1' }.int() |
| 48 | // NB: 1000 * 20 = maximum of ~20s runtime |
| 49 | for i := 0; i < max_cycles; i++ { |
| 50 | s := pmessage() |
| 51 | myprintln(s) |
| 52 | append_to_file(os.resource_abs_path(s + '.txt'), s) |
| 53 | if s == 'STOP' { |
| 54 | break |
| 55 | } |
| 56 | time.sleep(delay * time.millisecond) |
| 57 | } |
| 58 | pmessage() |
| 59 | pmessage() |
| 60 | myprintln('DATE: ' + time.now().str()) |
| 61 | myprintln('END') |
| 62 | } |
| 63 | |