rosetta-lbry/lbry/node.go
Thomas Zarebczan 3ba4c37047
LBRY
LBRY Updates
2020-12-08 00:08:02 -05:00

103 lines
2.3 KiB
Go

// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lbry
import (
"bufio"
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/lbryio/rosetta-lbry/utils"
"golang.org/x/sync/errgroup"
)
const (
lbrycrddLogger = "lbrycrdd"
lbrycrddStdErrLogger = "lbrycrdd stderr"
)
func logPipe(ctx context.Context, pipe io.ReadCloser, identifier string) error {
logger := utils.ExtractLogger(ctx, identifier)
reader := bufio.NewReader(pipe)
for {
str, err := reader.ReadString('\n')
if err != nil {
logger.Warnw("closing logger", "error", err)
return err
}
message := strings.ReplaceAll(str, "\n", "")
messages := strings.SplitAfterN(message, " ", 2)
// Trim the timestamp from the log if it exists
if len(messages) > 1 {
message = messages[1]
}
// Print debug log if from lbrycrddLogger
if identifier == lbrycrddLogger {
logger.Debugw(message)
continue
}
logger.Warnw(message)
}
}
// Startlbrycrdd starts a lbrycrdd daemon in another goroutine
// and logs the results to the console.
func Startlbrycrdd(ctx context.Context, configPath string, g *errgroup.Group) error {
logger := utils.ExtractLogger(ctx, "lbrycrdd")
cmd := exec.Command(
"/app/lbrycrdd",
fmt.Sprintf("--conf=%s", configPath),
) // #nosec G204
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
g.Go(func() error {
return logPipe(ctx, stdout, lbrycrddLogger)
})
g.Go(func() error {
return logPipe(ctx, stderr, lbrycrddStdErrLogger)
})
if err := cmd.Start(); err != nil {
return fmt.Errorf("%w: unable to start lbrycrdd", err)
}
g.Go(func() error {
<-ctx.Done()
logger.Warnw("sending interrupt to lbrycrdd")
return cmd.Process.Signal(os.Interrupt)
})
return cmd.Wait()
}