1 | package com.framsticks.diagnostics; |
---|
2 | |
---|
3 | import com.framsticks.core.Instance; |
---|
4 | import com.framsticks.dumping.PrintWriterSink; |
---|
5 | import com.framsticks.dumping.SaveStream; |
---|
6 | import com.framsticks.observers.Endpoint; |
---|
7 | import com.framsticks.remote.RecursiveFetcher; |
---|
8 | import com.framsticks.util.Logging; |
---|
9 | import com.framsticks.util.PeriodicTask; |
---|
10 | import com.framsticks.util.StateFunctor; |
---|
11 | import com.framsticks.util.io.Encoding; |
---|
12 | |
---|
13 | import org.apache.log4j.Logger; |
---|
14 | |
---|
15 | import java.io.File; |
---|
16 | import java.io.FileOutputStream; |
---|
17 | import java.io.IOException; |
---|
18 | import java.io.OutputStreamWriter; |
---|
19 | import java.io.PrintWriter; |
---|
20 | import java.text.SimpleDateFormat; |
---|
21 | import java.util.Date; |
---|
22 | |
---|
23 | /** |
---|
24 | * @author Piotr Sniegowski |
---|
25 | */ |
---|
26 | public class DiagnosticsEndpoint extends Endpoint { |
---|
27 | |
---|
28 | private final static Logger log = Logger.getLogger(DiagnosticsEndpoint.class.getName()); |
---|
29 | |
---|
30 | public DiagnosticsEndpoint() { |
---|
31 | super(); |
---|
32 | } |
---|
33 | |
---|
34 | @Override |
---|
35 | public Diagnostics getObserver() { |
---|
36 | return (Diagnostics)observer; |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | @Override |
---|
41 | public void start() { |
---|
42 | super.start(); |
---|
43 | |
---|
44 | if (getObserver().dumpsInterval != null) { |
---|
45 | new PeriodicTask<Instance>(instance, getObserver().dumpsInterval * 1000) { |
---|
46 | @Override |
---|
47 | public void run() { |
---|
48 | |
---|
49 | log.info("starting periodic dump"); |
---|
50 | new RecursiveFetcher(instance, instance.getRootPath(), new StateFunctor() { |
---|
51 | @Override |
---|
52 | public void call(Exception e) { |
---|
53 | if (Logging.log(log, "recursively fetch", instance, e)) { |
---|
54 | again(); |
---|
55 | return; |
---|
56 | } |
---|
57 | log.info("instance resolved, saving"); |
---|
58 | try { |
---|
59 | final String fileName = getObserver().dumpsPath + "/" + instance + "_" + new SimpleDateFormat(getObserver().dumpsFormat).format(new Date()) + ".param"; |
---|
60 | File file = new File(fileName); |
---|
61 | new SaveStream(new PrintWriterSink(new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), Encoding.getFramsticksCharset()))), instance, instance.getRootPath(), new StateFunctor() { |
---|
62 | @Override |
---|
63 | public void call(Exception e) { |
---|
64 | Logging.log(log, "periodic dump in " + fileName + " of", DiagnosticsEndpoint.this.instance, e); |
---|
65 | again(); |
---|
66 | } |
---|
67 | }); |
---|
68 | } catch (IOException ex) { |
---|
69 | log.info("failed to initiate dump: " + ex); |
---|
70 | again(); |
---|
71 | } |
---|
72 | |
---|
73 | } |
---|
74 | }); |
---|
75 | |
---|
76 | |
---|
77 | } |
---|
78 | }; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /* |
---|
83 | @Override |
---|
84 | public void onChange(Path path) { |
---|
85 | super.onChange(path); //To change body of overridden methods use File | Settings | File Templates. |
---|
86 | } |
---|
87 | */ |
---|
88 | } |
---|