github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/jobs/jobs_watcher.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package jobs
     5  
     6  import (
     7  	"math/rand"
     8  	"time"
     9  
    10  	"github.com/masterhung0112/hk_server/v5/model"
    11  	"github.com/masterhung0112/hk_server/v5/shared/mlog"
    12  )
    13  
    14  // Default polling interval for jobs termination.
    15  // (Defining as `var` rather than `const` allows tests to lower the interval.)
    16  var DefaultWatcherPollingInterval = 15000
    17  
    18  type Watcher struct {
    19  	srv     *JobServer
    20  	workers *Workers
    21  
    22  	stop            chan struct{}
    23  	stopped         chan struct{}
    24  	pollingInterval int
    25  }
    26  
    27  func (srv *JobServer) MakeWatcher(workers *Workers, pollingInterval int) *Watcher {
    28  	return &Watcher{
    29  		stop:            make(chan struct{}),
    30  		stopped:         make(chan struct{}),
    31  		pollingInterval: pollingInterval,
    32  		workers:         workers,
    33  		srv:             srv,
    34  	}
    35  }
    36  
    37  func (watcher *Watcher) Start() {
    38  	mlog.Debug("Watcher Started")
    39  
    40  	// Delay for some random number of milliseconds before starting to ensure that multiple
    41  	// instances of the jobserver  don't poll at a time too close to each other.
    42  	rand.Seed(time.Now().UTC().UnixNano())
    43  	<-time.After(time.Duration(rand.Intn(watcher.pollingInterval)) * time.Millisecond)
    44  
    45  	defer func() {
    46  		mlog.Debug("Watcher Finished")
    47  		close(watcher.stopped)
    48  	}()
    49  
    50  	for {
    51  		select {
    52  		case <-watcher.stop:
    53  			mlog.Debug("Watcher: Received stop signal")
    54  			return
    55  		case <-time.After(time.Duration(watcher.pollingInterval) * time.Millisecond):
    56  			watcher.PollAndNotify()
    57  		}
    58  	}
    59  }
    60  
    61  func (watcher *Watcher) Stop() {
    62  	mlog.Debug("Watcher Stopping")
    63  	close(watcher.stop)
    64  	<-watcher.stopped
    65  }
    66  
    67  func (watcher *Watcher) PollAndNotify() {
    68  	jobs, err := watcher.srv.Store.Job().GetAllByStatus(model.JOB_STATUS_PENDING)
    69  	if err != nil {
    70  		mlog.Error("Error occurred getting all pending statuses.", mlog.Err(err))
    71  		return
    72  	}
    73  
    74  	for _, job := range jobs {
    75  		if job.Type == model.JOB_TYPE_DATA_RETENTION {
    76  			if watcher.workers.DataRetention != nil {
    77  				select {
    78  				case watcher.workers.DataRetention.JobChannel() <- *job:
    79  				default:
    80  				}
    81  			}
    82  		} else if job.Type == model.JOB_TYPE_MESSAGE_EXPORT {
    83  			if watcher.workers.MessageExport != nil {
    84  				select {
    85  				case watcher.workers.MessageExport.JobChannel() <- *job:
    86  				default:
    87  				}
    88  			}
    89  		} else if job.Type == model.JOB_TYPE_ELASTICSEARCH_POST_INDEXING {
    90  			if watcher.workers.ElasticsearchIndexing != nil {
    91  				select {
    92  				case watcher.workers.ElasticsearchIndexing.JobChannel() <- *job:
    93  				default:
    94  				}
    95  			}
    96  		} else if job.Type == model.JOB_TYPE_ELASTICSEARCH_POST_AGGREGATION {
    97  			if watcher.workers.ElasticsearchAggregation != nil {
    98  				select {
    99  				case watcher.workers.ElasticsearchAggregation.JobChannel() <- *job:
   100  				default:
   101  				}
   102  			}
   103  		} else if job.Type == model.JOB_TYPE_BLEVE_POST_INDEXING {
   104  			if watcher.workers.BleveIndexing != nil {
   105  				select {
   106  				case watcher.workers.BleveIndexing.JobChannel() <- *job:
   107  				default:
   108  				}
   109  			}
   110  		} else if job.Type == model.JOB_TYPE_LDAP_SYNC {
   111  			if watcher.workers.LdapSync != nil {
   112  				select {
   113  				case watcher.workers.LdapSync.JobChannel() <- *job:
   114  				default:
   115  				}
   116  			}
   117  		} else if job.Type == model.JOB_TYPE_MIGRATIONS {
   118  			if watcher.workers.Migrations != nil {
   119  				select {
   120  				case watcher.workers.Migrations.JobChannel() <- *job:
   121  				default:
   122  				}
   123  			}
   124  		} else if job.Type == model.JOB_TYPE_PLUGINS {
   125  			if watcher.workers.Plugins != nil {
   126  				select {
   127  				case watcher.workers.Plugins.JobChannel() <- *job:
   128  				default:
   129  				}
   130  			}
   131  		} else if job.Type == model.JOB_TYPE_EXPIRY_NOTIFY {
   132  			if watcher.workers.ExpiryNotify != nil {
   133  				select {
   134  				case watcher.workers.ExpiryNotify.JobChannel() <- *job:
   135  				default:
   136  				}
   137  			}
   138  		} else if job.Type == model.JOB_TYPE_PRODUCT_NOTICES {
   139  			if watcher.workers.ProductNotices != nil {
   140  				select {
   141  				case watcher.workers.ProductNotices.JobChannel() <- *job:
   142  				default:
   143  				}
   144  			}
   145  		} else if job.Type == model.JOB_TYPE_ACTIVE_USERS {
   146  			if watcher.workers.ActiveUsers != nil {
   147  				select {
   148  				case watcher.workers.ActiveUsers.JobChannel() <- *job:
   149  				default:
   150  				}
   151  			}
   152  		} else if job.Type == model.JOB_TYPE_IMPORT_PROCESS {
   153  			if watcher.workers.ImportProcess != nil {
   154  				select {
   155  				case watcher.workers.ImportProcess.JobChannel() <- *job:
   156  				default:
   157  				}
   158  			}
   159  		} else if job.Type == model.JOB_TYPE_IMPORT_DELETE {
   160  			if watcher.workers.ImportDelete != nil {
   161  				select {
   162  				case watcher.workers.ImportDelete.JobChannel() <- *job:
   163  				default:
   164  				}
   165  			}
   166  		} else if job.Type == model.JOB_TYPE_EXPORT_PROCESS {
   167  			if watcher.workers.ExportProcess != nil {
   168  				select {
   169  				case watcher.workers.ExportProcess.JobChannel() <- *job:
   170  				default:
   171  				}
   172  			}
   173  		} else if job.Type == model.JOB_TYPE_EXPORT_DELETE {
   174  			if watcher.workers.ExportDelete != nil {
   175  				select {
   176  				case watcher.workers.ExportDelete.JobChannel() <- *job:
   177  				default:
   178  				}
   179  			}
   180  		} else if job.Type == model.JOB_TYPE_CLOUD {
   181  			if watcher.workers.Cloud != nil {
   182  				select {
   183  				case watcher.workers.Cloud.JobChannel() <- *job:
   184  				default:
   185  				}
   186  			}
   187  		} else if job.Type == model.JOB_TYPE_RESEND_INVITATION_EMAIL {
   188  			if watcher.workers.ResendInvitationEmail != nil {
   189  				select {
   190  				case watcher.workers.ResendInvitationEmail.JobChannel() <- *job:
   191  				default:
   192  				}
   193  			}
   194  		}
   195  	}
   196  }