github.com/kaydxh/golang@v0.0.131/pkg/binlog/binlog.archive.go (about)

     1  /*
     2   *Copyright (c) 2023, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package binlog
    23  
    24  import (
    25  	"context"
    26  	"encoding/json"
    27  
    28  	queue_ "github.com/kaydxh/golang/pkg/pool/taskqueue/queue"
    29  
    30  	io_ "github.com/kaydxh/golang/go/io"
    31  	s3_ "github.com/kaydxh/golang/pkg/storage/s3"
    32  	"github.com/sirupsen/logrus"
    33  )
    34  
    35  const (
    36  	ArchiveTaskScheme = "ArchiveTaskTask"
    37  )
    38  
    39  type ArchiveTaskArgs struct {
    40  	LocalFilePath  string
    41  	RemoteRootPath string
    42  }
    43  
    44  type ArchiveTask struct {
    45  	bucket *s3_.Storage
    46  }
    47  
    48  func (t ArchiveTask) Scheme() string {
    49  	return ArchiveTaskScheme
    50  }
    51  
    52  func (t ArchiveTask) TaskHandler(ctx context.Context, msg *queue_.Message) (*queue_.MessageResult, error) {
    53  	logger := logrus.WithField("message_id", msg.Id).
    54  		WithField("message_inner_id", msg.InnerId).
    55  		WithField("module", "TaskHandler")
    56  
    57  	result := &queue_.MessageResult{
    58  		Id:      msg.Id,
    59  		InnerId: msg.InnerId,
    60  		Name:    msg.Name,
    61  		Scheme:  msg.Scheme,
    62  	}
    63  
    64  	var args ArchiveTaskArgs
    65  	err := json.Unmarshal([]byte(msg.Args), &args)
    66  	if err != nil {
    67  		logger.WithError(err).Errorf("failed to unmarshal msg: %v", msg)
    68  		return result, err
    69  	}
    70  	data, err := io_.ReadFile(args.LocalFilePath)
    71  	if err != nil {
    72  		logger.WithError(err).Errorf("failed to read binlog[%v]", args.LocalFilePath)
    73  		return result, err
    74  	}
    75  	if len(data) == 0 {
    76  		logger.Infof("binlog[%v] is empty, not need to archive", args.LocalFilePath)
    77  		return result, nil
    78  	}
    79  
    80  	s3Path := args.RemoteRootPath
    81  	err = t.bucket.WriteAll(ctx, s3Path, data, nil)
    82  	if err != nil {
    83  		logger.WithError(err).Errorf("failed to upload data size[%v] to s3[%v]", len(data), s3Path)
    84  		return result, err
    85  	}
    86  
    87  	return result, nil
    88  }