github.com/team-ide/go-dialect@v1.9.20/worker/task.go (about)

     1  package worker
     2  
     3  import (
     4  	"database/sql"
     5  	"errors"
     6  	"fmt"
     7  	"github.com/team-ide/go-dialect/dialect"
     8  	"sync"
     9  )
    10  
    11  type Task struct {
    12  	TaskId     string `json:"taskId"`
    13  	StartTime  int64  `json:"startTime"`
    14  	EndTime    int64  `json:"endTime"`
    15  	UseTime    int64  `json:"useTime"`
    16  	Error      string `json:"error"`
    17  	PanicError string `json:"panicError"`
    18  	IsEnd      bool   `json:"isEnd"`
    19  	IsStop     bool   `json:"isStop"`
    20  
    21  	OwnerCount        int `json:"ownerCount"`
    22  	OwnerSuccessCount int `json:"ownerSuccessCount"`
    23  	OwnerErrorCount   int `json:"ownerErrorCount"`
    24  
    25  	TableCount        int `json:"tableCount"`
    26  	TableSuccessCount int `json:"tableSuccessCount"`
    27  	TableErrorCount   int `json:"tableErrorCount"`
    28  
    29  	DataCount        int `json:"dataCount"`
    30  	DataReadyCount   int `json:"dataReadyCount"`
    31  	DataSuccessCount int `json:"dataSuccessCount"`
    32  	DataErrorCount   int `json:"dataErrorCount"`
    33  
    34  	countLock sync.Mutex
    35  
    36  	Extend map[string]interface{} `json:"extend"`
    37  	Errors []string               `json:"errors"`
    38  
    39  	onProgress func(progress *TaskProgress)
    40  	dia        dialect.Dialect
    41  	db         *sql.DB
    42  	do         func() (err error)
    43  	Param      *dialect.ParamModel
    44  }
    45  
    46  type TaskProgress struct {
    47  	Title   string          `json:"title"`
    48  	Infos   []string        `json:"infos"`
    49  	Error   string          `json:"error"`
    50  	OnError func(err error) `json:"-"`
    51  }
    52  
    53  var (
    54  	taskCache     = make(map[string]*Task)
    55  	taskCacheLock sync.Mutex
    56  )
    57  
    58  func addTask(task *Task) {
    59  	taskCacheLock.Lock()
    60  	defer taskCacheLock.Unlock()
    61  
    62  	task.TaskId = dialect.UUID()
    63  	taskCache[task.TaskId] = task
    64  	return
    65  }
    66  
    67  func GetTask(taskId string) (task *Task) {
    68  	taskCacheLock.Lock()
    69  	defer taskCacheLock.Unlock()
    70  
    71  	task = taskCache[taskId]
    72  	return
    73  }
    74  
    75  func StopTask(taskId string) {
    76  	taskCacheLock.Lock()
    77  	defer taskCacheLock.Unlock()
    78  
    79  	task := taskCache[taskId]
    80  	if task != nil {
    81  		task.stop()
    82  	}
    83  	return
    84  }
    85  
    86  func ClearTask(taskId string) {
    87  	taskCacheLock.Lock()
    88  	defer taskCacheLock.Unlock()
    89  
    90  	task := taskCache[taskId]
    91  	if task != nil {
    92  		task.stop()
    93  	}
    94  	delete(taskCache, taskId)
    95  	return
    96  }
    97  
    98  func (this_ *Task) Start() (err error) {
    99  	this_.IsStop = false
   100  	if this_.Param == nil {
   101  		this_.Param = &dialect.ParamModel{}
   102  	}
   103  	addTask(this_)
   104  
   105  	defer func() {
   106  		if e := recover(); e != nil {
   107  			this_.PanicError = fmt.Sprint(e)
   108  			this_.Error = this_.PanicError
   109  		}
   110  		if err != nil {
   111  			this_.Error = err.Error()
   112  		}
   113  		this_.EndTime = NowTime()
   114  		this_.UseTime = this_.EndTime - this_.StartTime
   115  		this_.IsEnd = true
   116  	}()
   117  
   118  	this_.StartTime = NowTime()
   119  	if this_.do == nil {
   120  		err = errors.New("has nothing to do")
   121  		return
   122  	}
   123  	err = this_.do()
   124  	if err != nil {
   125  		return
   126  	}
   127  	return
   128  }
   129  
   130  func (this_ *Task) addProgress(progress *TaskProgress) {
   131  	if this_.onProgress != nil {
   132  		this_.onProgress(progress)
   133  	}
   134  	return
   135  }
   136  
   137  func (this_ *Task) addError(err string) {
   138  	this_.Errors = append(this_.Errors, err)
   139  	return
   140  }
   141  
   142  func (this_ *Task) stop() {
   143  	this_.IsStop = true
   144  }
   145  
   146  func (this_ *Task) countIncr(count *int, num int) {
   147  	this_.countLock.Lock()
   148  	defer this_.countLock.Unlock()
   149  	*count += num
   150  	return
   151  }