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