|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "math/rand" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/telkomdev/tob" |
| 9 | + "github.com/telkomdev/tob/config" |
| 10 | + "github.com/telkomdev/tob/util" |
| 11 | +) |
| 12 | + |
| 13 | +func main() {} |
| 14 | + |
| 15 | +// TemplatePlugin service |
| 16 | +type TemplatePlugin struct { |
| 17 | + url string |
| 18 | + recovered bool |
| 19 | + lastDownTime string |
| 20 | + enabled bool |
| 21 | + verbose bool |
| 22 | + logger *log.Logger |
| 23 | + checkInterval int |
| 24 | + stopChan chan bool |
| 25 | + message string |
| 26 | + notificatorConfig config.Config |
| 27 | +} |
| 28 | + |
| 29 | +// NewDummy Dummy's constructor |
| 30 | +func NewTemplatePlugin(verbose bool, logger *log.Logger) *TemplatePlugin { |
| 31 | + stopChan := make(chan bool, 1) |
| 32 | + return &TemplatePlugin{ |
| 33 | + logger: logger, |
| 34 | + verbose: verbose, |
| 35 | + |
| 36 | + // by default service is recovered |
| 37 | + recovered: true, |
| 38 | + checkInterval: 0, |
| 39 | + stopChan: stopChan, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Name the name of the service |
| 44 | +func (d *TemplatePlugin) Name() string { |
| 45 | + return "plugin" |
| 46 | +} |
| 47 | + |
| 48 | +// Ping will try to ping the service |
| 49 | +func (d *TemplatePlugin) Ping() []byte { |
| 50 | + n := rand.Intn(100) |
| 51 | + if n < 50 { |
| 52 | + d.SetMessage("dummy plugin has an error") |
| 53 | + return []byte("NOT_OK") |
| 54 | + } |
| 55 | + |
| 56 | + return []byte("OK") |
| 57 | +} |
| 58 | + |
| 59 | +// SetURL will set the service URL |
| 60 | +func (d *TemplatePlugin) SetURL(url string) { |
| 61 | + d.url = url |
| 62 | +} |
| 63 | + |
| 64 | +// Connect to service if needed |
| 65 | +func (d *TemplatePlugin) Connect() error { |
| 66 | + if d.verbose { |
| 67 | + d.logger.Println("connect dummy") |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// Close will close the service resources if needed |
| 74 | +func (d *TemplatePlugin) Close() error { |
| 75 | + if d.verbose { |
| 76 | + d.logger.Println("close dummy plugin") |
| 77 | + } |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +// SetRecover will set recovered status |
| 83 | +func (d *TemplatePlugin) SetRecover(recovered bool) { |
| 84 | + d.recovered = recovered |
| 85 | +} |
| 86 | + |
| 87 | +// IsRecover will return recovered status |
| 88 | +func (d *TemplatePlugin) IsRecover() bool { |
| 89 | + return d.recovered |
| 90 | +} |
| 91 | + |
| 92 | +// LastDownTime will set last down time of service to current time |
| 93 | +func (d *TemplatePlugin) SetLastDownTimeNow() { |
| 94 | + if d.recovered { |
| 95 | + d.lastDownTime = time.Now().Format(util.YYMMDD) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// GetDownTimeDiff will return down time service difference in minutes |
| 100 | +func (d *TemplatePlugin) GetDownTimeDiff() string { |
| 101 | + return util.TimeDifference(d.lastDownTime, time.Now().Format(util.YYMMDD)) |
| 102 | +} |
| 103 | + |
| 104 | +// SetCheckInterval will set check interval to service |
| 105 | +func (d *TemplatePlugin) SetCheckInterval(interval int) { |
| 106 | + d.checkInterval = interval |
| 107 | +} |
| 108 | + |
| 109 | +// GetCheckInterval will return check interval to service |
| 110 | +func (d *TemplatePlugin) GetCheckInterval() int { |
| 111 | + return d.checkInterval |
| 112 | +} |
| 113 | + |
| 114 | +// Enable will set enabled status to service |
| 115 | +func (d *TemplatePlugin) Enable(enabled bool) { |
| 116 | + d.enabled = enabled |
| 117 | +} |
| 118 | + |
| 119 | +// IsEnabled will return enable status |
| 120 | +func (d *TemplatePlugin) IsEnabled() bool { |
| 121 | + return d.enabled |
| 122 | +} |
| 123 | + |
| 124 | +// SetMessage will set additional message |
| 125 | +func (d *TemplatePlugin) SetMessage(message string) { |
| 126 | + d.message = message |
| 127 | +} |
| 128 | + |
| 129 | +// GetMessage will return additional message |
| 130 | +func (d *TemplatePlugin) GetMessage() string { |
| 131 | + return d.message |
| 132 | +} |
| 133 | + |
| 134 | +// SetConfig will set config |
| 135 | +func (d *TemplatePlugin) SetConfig(configs config.Config) { |
| 136 | + |
| 137 | +} |
| 138 | + |
| 139 | +// SetNotificatorConfig will set config |
| 140 | +func (d *TemplatePlugin) SetNotificatorConfig(configs config.Config) { |
| 141 | + d.notificatorConfig = configs |
| 142 | +} |
| 143 | + |
| 144 | +// GetNotificators will return notificators |
| 145 | +func (d *TemplatePlugin) GetNotificators() []tob.Notificator { |
| 146 | + return tob.InitNotificatorFactory(d.notificatorConfig, d.verbose) |
| 147 | +} |
| 148 | + |
| 149 | +// Stop will receive stop channel |
| 150 | +func (d *TemplatePlugin) Stop() chan bool { |
| 151 | + if d.stopChan == nil { |
| 152 | + d.stopChan = make(chan bool, 1) |
| 153 | + } |
| 154 | + |
| 155 | + return d.stopChan |
| 156 | +} |
| 157 | + |
| 158 | +// Exported |
| 159 | +var Service TemplatePlugin |
0 commit comments