2018-09-24 18:37:45 +00:00
|
|
|
package logmon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-02 15:33:05 +00:00
|
|
|
"time"
|
2018-09-24 18:37:45 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/client/logmon/proto"
|
2019-04-25 18:32:24 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/grpcutils"
|
2018-09-24 18:37:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type logmonClient struct {
|
|
|
|
client proto.LogMonClient
|
2019-04-25 18:32:24 +00:00
|
|
|
|
|
|
|
// doneCtx is closed when the plugin exits
|
|
|
|
doneCtx context.Context
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
2019-12-02 15:33:05 +00:00
|
|
|
const logmonRPCTimeout = 1 * time.Minute
|
|
|
|
|
2018-09-24 18:37:45 +00:00
|
|
|
func (c *logmonClient) Start(cfg *LogConfig) error {
|
|
|
|
req := &proto.StartRequest{
|
|
|
|
LogDir: cfg.LogDir,
|
|
|
|
StdoutFileName: cfg.StdoutLogFile,
|
|
|
|
StderrFileName: cfg.StderrLogFile,
|
|
|
|
MaxFiles: uint32(cfg.MaxFiles),
|
|
|
|
MaxFileSizeMb: uint32(cfg.MaxFileSizeMB),
|
|
|
|
StdoutFifo: cfg.StdoutFifo,
|
|
|
|
StderrFifo: cfg.StderrFifo,
|
|
|
|
}
|
2019-12-02 15:33:05 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), logmonRPCTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err := c.client.Start(ctx, req)
|
2019-04-25 18:32:24 +00:00
|
|
|
return grpcutils.HandleGrpcErr(err, c.doneCtx)
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *logmonClient) Stop() error {
|
|
|
|
req := &proto.StopRequest{}
|
2019-12-02 15:33:05 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), logmonRPCTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
_, err := c.client.Stop(ctx, req)
|
2019-04-25 18:32:24 +00:00
|
|
|
return grpcutils.HandleGrpcErr(err, c.doneCtx)
|
2018-09-24 18:37:45 +00:00
|
|
|
}
|