1 | sclass TailFile implements AutoCloseable { |
2 | File file; |
3 | int interval; |
4 | O onData; |
5 | long l; |
6 | gettable long currentFileSize; |
7 | new Flag stopped; |
8 | java.util.Timer timer; |
9 | ReliableSingleThread thread; |
10 | volatile bool hasIdled, debug; |
11 | |
12 | *() {} |
13 | *(final File *file, int *interval, O *onData) { |
14 | thread = new ReliableSingleThread(r { update() }); |
15 | } |
16 | |
17 | void update { |
18 | long l2 = currentFileSize = l(file); |
19 | if (l2 < l) l = 0; // file shrunk (log rotated!) - start over from beginning of file |
20 | if (l2 == l) hasIdled = true; |
21 | else pcall { |
22 | if (debug) print("New data in " + f2s(file) + ": " + n2(l2-l, "byte")); |
23 | for (S s : loadTextFilePart_iterator(file, l, l2)) |
24 | pcallF(onData, s); |
25 | l = l2; |
26 | } |
27 | } |
28 | |
29 | void start { |
30 | timer = doEvery(interval, r { |
31 | if (!stopped.isUp()) thread.trigger() |
32 | }); |
33 | } |
34 | |
35 | void stop { |
36 | timer.cancel(); |
37 | timer = null; |
38 | if (stopped.raise()) |
39 | thread.triggerAndWait(); // One last time baby |
40 | } |
41 | |
42 | public void close { stop(); } |
43 | |
44 | bool started() { |
45 | ret timer != null; |
46 | } |
47 | |
48 | void debugOn { |
49 | if (debug) ret; |
50 | debug = true; |
51 | print("Watching file: " + f2s(file)); |
52 | } |
53 | |
54 | toString { ret "Tailing file " + file; } |
55 | } |
Began life as a copy of #1013001
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1036375 |
Snippet name: | TailFile backup using java.util.Timer |
Eternal ID of this version: | #1036375/1 |
Text MD5: | 24961a79b69ae9985ac87d1569ad5a4f |
Author: | stefan |
Category: | javax / i.o. |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-11-28 20:43:40 |
Source code size: | 1277 bytes / 55 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 110 / 123 |
Referenced in: | [show references] |