github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cmd/dm-worker/main.go (about)

     1  // Copyright 2019 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package main
    15  
    16  import (
    17  	"flag"
    18  	"fmt"
    19  	"os"
    20  	"os/signal"
    21  	"strings"
    22  	"syscall"
    23  
    24  	"github.com/pingcap/errors"
    25  	"github.com/pingcap/tidb/pkg/util/collate"
    26  	"github.com/pingcap/tiflow/dm/ctl/common"
    27  	"github.com/pingcap/tiflow/dm/pkg/log"
    28  	"github.com/pingcap/tiflow/dm/pkg/terror"
    29  	"github.com/pingcap/tiflow/dm/pkg/utils"
    30  	"github.com/pingcap/tiflow/dm/worker"
    31  	"github.com/pingcap/tiflow/pkg/version"
    32  	"go.uber.org/zap"
    33  )
    34  
    35  func main() {
    36  	// NOTE: the line is removed from TiDB repo in https://github.com/pingcap/tidb/pull/52191#issuecomment-2024836481.
    37  	collate.SetNewCollationEnabledForTest(false)
    38  
    39  	cfg := worker.NewConfig()
    40  	err := cfg.Parse(os.Args[1:])
    41  	switch errors.Cause(err) {
    42  	case nil:
    43  	case flag.ErrHelp:
    44  		os.Exit(0)
    45  	default:
    46  		common.PrintLinesf("parse cmd flags err: %s", terror.Message(err))
    47  		os.Exit(2)
    48  	}
    49  
    50  	err = log.InitLogger(&log.Config{
    51  		File:   cfg.LogFile,
    52  		Format: cfg.LogFormat,
    53  		Level:  strings.ToLower(cfg.LogLevel),
    54  	})
    55  	if err != nil {
    56  		common.PrintLinesf("init logger error %s", terror.Message(err))
    57  		os.Exit(2)
    58  	}
    59  
    60  	utils.LogHTTPProxies(true)
    61  
    62  	version.LogVersionInfo("dm-worker")
    63  	log.L().Info("", zap.Stringer("dm-worker config", cfg))
    64  
    65  	sc := make(chan os.Signal, 1)
    66  	signal.Notify(sc,
    67  		syscall.SIGHUP,
    68  		syscall.SIGINT,
    69  		syscall.SIGTERM,
    70  		syscall.SIGQUIT)
    71  
    72  	s := worker.NewServer(cfg)
    73  	err = s.JoinMaster(worker.GetJoinURLs(cfg.Join))
    74  	if err != nil {
    75  		log.L().Info("join the cluster meet error", zap.Error(err))
    76  		os.Exit(2)
    77  	}
    78  
    79  	go func() {
    80  		sig := <-sc
    81  		log.L().Info("got signal to exit", zap.Stringer("signal", sig))
    82  		s.Close()
    83  	}()
    84  	err = s.Start()
    85  	switch errors.Cause(err) {
    86  	case nil:
    87  	case terror.ErrWorkerServerClosed:
    88  		log.L().Info("server closed")
    89  	default:
    90  		log.L().Error("fail to start dm-worker", zap.Error(err))
    91  	}
    92  
    93  	s.Close() // wait until closed
    94  	log.L().Info("dm-worker exit")
    95  
    96  	syncErr := log.L().Sync()
    97  	if syncErr != nil {
    98  		fmt.Fprintln(os.Stderr, "sync log failed", syncErr)
    99  	}
   100  
   101  	if err != nil || syncErr != nil {
   102  		os.Exit(1)
   103  	}
   104  }