fix: comprehensive bug fixes and architecture restructuring
Phase 1 — Bug fixes (7 bugs): - Bug 1: Pulsar mode data loss — always persist to SQLite regardless of mode - Bug 2: PulsarSend closing TCP client — use independent resetPulsarProducer() - Bug 3: Greedy regex — use non-greedy (?s)ZCZC.*?NNNN - Bug 4: Data after NNNN discarded — keep remaining buffer data - Bug 5: strings.Index > 0 boundary — use strings.Contains - Bug 6: Variable shadowing in PulsarSend — use = not := - Bug 7: Accept failure nil panic — add continue + retry logic Phase 2 — Architecture restructuring: - Split utils/ into config/, serial/, telegram/, storage/, transport/ - Define Sender, Repository, Reader interfaces - Introduce app/ layer with context.Context lifecycle - Replace spinlock with sync.Mutex - Unified Config struct replaces 15+ global vars Phase 3 — Testing & tooling: - telegram/parser_test.go (7 test cases) - storage/store_test.go (5 test cases) - transport/transport_test.go (4 test cases) - Taskfile: add test, test-race, test-cover tasks - Go version: 1.15 -> 1.21
This commit is contained in:
+14
-80
@@ -16,15 +16,10 @@ limitations under the License.
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"it2000.com.cn/tele-recv/utils"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"it2000.com.cn/tele-recv/app"
|
||||
)
|
||||
|
||||
// startCmd represents the start command
|
||||
@@ -33,85 +28,24 @@ var startCmd = &cobra.Command{
|
||||
Short: "start telegram receive service",
|
||||
Long: `open serial port
|
||||
start a tcp server for processing`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
start()
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return start()
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(startCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// startCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// startCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
}
|
||||
|
||||
func start() {
|
||||
// Go signal notification works by sending `os.Signal`
|
||||
// values on a channel. We'll create a channel to
|
||||
// receive these notifications (we'll also make one to
|
||||
// notify us when the program can exit).
|
||||
sigs := make(chan os.Signal, 1)
|
||||
done := make(chan bool, 1)
|
||||
|
||||
// `signal.Notify` registers the given channel to
|
||||
// receive notifications of the specified signals.
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
utils.ServerRunning = true
|
||||
|
||||
// This goroutine executes a blocking receive for
|
||||
// signals. When it gets one it'll print it out
|
||||
// and then notify the program that it can finish.
|
||||
go func() {
|
||||
sig := <-sigs
|
||||
logger.Info("got ", zap.Any("signal", sig))
|
||||
utils.ServerRunning = false
|
||||
if tcp {
|
||||
utils.StopSocketServer()
|
||||
}
|
||||
if pulsar {
|
||||
utils.ClosePulsar()
|
||||
}
|
||||
done <- true
|
||||
}()
|
||||
|
||||
// The program will wait here until it gets the
|
||||
// expected signal (as indicated by the goroutine
|
||||
// above sending a value on `done`) and then exit.
|
||||
logger.Info("awaiting signal")
|
||||
_ = utils.InitDb(dbFile, dbInit)
|
||||
if tcp {
|
||||
go utils.Listen(socketAddress)
|
||||
func start() error {
|
||||
if appCfg == nil {
|
||||
logger.Fatal("configuration not loaded")
|
||||
}
|
||||
if pulsar {
|
||||
utils.CreateProducer(pulsarUrl, topic, name)
|
||||
}
|
||||
for utils.ServerRunning {
|
||||
if !utils.IsPortOpen() {
|
||||
logger.Info("try to open port")
|
||||
err := utils.OpenPort(device, baudrate)
|
||||
if err != nil {
|
||||
logger.Fatal("error in open serial port ", zap.Error(err))
|
||||
}
|
||||
logger.Info("starting read")
|
||||
}
|
||||
buffer, err := utils.ReadPort()
|
||||
if err == nil {
|
||||
if lograw && len(buffer) > 0 {
|
||||
fmt.Println(buffer)
|
||||
}
|
||||
if utils.Append(buffer) {
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
}
|
||||
<-done
|
||||
logger.Info("exiting")
|
||||
|
||||
a, err := app.New(appCfg)
|
||||
if err != nil {
|
||||
logger.Fatal("failed to create app", zap.Error(err))
|
||||
}
|
||||
|
||||
return a.Run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user