github.com/kaydxh/golang@v0.0.131/pkg/scheduler/worker.go (about)

     1  /*
     2   *Copyright (c) 2022, 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 scheduler
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  
    28  	"github.com/kaydxh/golang/pkg/scheduler/task"
    29  )
    30  
    31  type Worker struct {
    32  	id        int
    33  	stopCh    chan struct{}
    34  	working   bool
    35  	worksChCh chan chan *task.Task
    36  	taskCh    chan *task.Task
    37  }
    38  
    39  func NewWorker(id int, worksChCh chan chan *task.Task) *Worker {
    40  	w := &Worker{
    41  		id:        id,
    42  		worksChCh: worksChCh,
    43  		taskCh:    make(chan *task.Task),
    44  		stopCh:    make(chan struct{}),
    45  	}
    46  
    47  	return w
    48  }
    49  
    50  func (w *Worker) doProcess(ctx context.Context, task *task.Task) error {
    51  	fmt.Println(" doProcess")
    52  	if task.PreTaskHandler != nil {
    53  		task.PreTaskHandler(task)
    54  	}
    55  
    56  	//do task
    57  	_, err := task.Run()
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	if task.PostTaskHandler != nil {
    63  		task.PostTaskHandler(task)
    64  	}
    65  
    66  	return nil
    67  }
    68  
    69  func (w *Worker) Process(ctx context.Context) {
    70  	w.worksChCh <- w.taskCh
    71  
    72  	fmt.Println(" Process task")
    73  	for {
    74  		select {
    75  		case task := <-w.taskCh:
    76  			w.working = true
    77  			fmt.Println(" get task")
    78  			w.doProcess(ctx, task)
    79  			w.worksChCh <- w.taskCh
    80  
    81  		case <-w.stopCh:
    82  			return
    83  		}
    84  	}
    85  }
    86  
    87  func (w *Worker) Stop() {
    88  	if !w.working {
    89  		return
    90  	}
    91  
    92  	close(w.stopCh)
    93  	w.working = false
    94  }