storj/crashcollect/crash/service.go
Qweder93 a5c1e4b4a5 crashcollect: process for receiving panics
Added crashcollect server for tracking panics
on parent process and saving them in file.

Change-Id: I7926f9a16594227a3262e05d216199b7c2857385
2021-04-27 18:57:47 +03:00

60 lines
1.2 KiB
Go

// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package crash
import (
"fmt"
"os"
"path"
"time"
"github.com/zeebo/errs"
"storj.io/common/storj"
)
// Config contains configurable values for crash collect service.
type Config struct {
StoringDir string `help:"directory to store crash reports" default:""`
}
// Error is a default error type for crash collect Service.
var Error = errs.Class("crashes service")
// Service exposes all crash-collect business logic.
//
// architecture: service
type Service struct {
config Config
}
// NewService is an constructor for Service.
func NewService(config Config) *Service {
return &Service{
config: config,
}
}
// Report receives report from crash-report client and saves it into .gz file.
func (s *Service) Report(nodeID storj.NodeID, gzippedPanic []byte) error {
now := time.Now().UTC()
filename := fmt.Sprintf("%s-%s.gz", nodeID.String(), now.Format(time.RFC3339))
f, err := os.Create(path.Join(s.config.StoringDir, filename))
if err != nil {
return Error.Wrap(err)
}
defer func() {
err = errs.Combine(err, f.Close())
}()
_, err = f.Write(gzippedPanic)
if err != nil {
return Error.Wrap(err)
}
return nil
}