github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/model/job.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  	"net/http"
    10  	"time"
    11  )
    12  
    13  const (
    14  	JOB_TYPE_DATA_RETENTION                 = "data_retention"
    15  	JOB_TYPE_MESSAGE_EXPORT                 = "message_export"
    16  	JOB_TYPE_ELASTICSEARCH_POST_INDEXING    = "elasticsearch_post_indexing"
    17  	JOB_TYPE_ELASTICSEARCH_POST_AGGREGATION = "elasticsearch_post_aggregation"
    18  	JOB_TYPE_LDAP_SYNC                      = "ldap_sync"
    19  
    20  	JOB_STATUS_PENDING          = "pending"
    21  	JOB_STATUS_IN_PROGRESS      = "in_progress"
    22  	JOB_STATUS_SUCCESS          = "success"
    23  	JOB_STATUS_ERROR            = "error"
    24  	JOB_STATUS_CANCEL_REQUESTED = "cancel_requested"
    25  	JOB_STATUS_CANCELED         = "canceled"
    26  )
    27  
    28  type Job struct {
    29  	Id             string            `json:"id"`
    30  	Type           string            `json:"type"`
    31  	Priority       int64             `json:"priority"`
    32  	CreateAt       int64             `json:"create_at"`
    33  	StartAt        int64             `json:"start_at"`
    34  	LastActivityAt int64             `json:"last_activity_at"`
    35  	Status         string            `json:"status"`
    36  	Progress       int64             `json:"progress"`
    37  	Data           map[string]string `json:"data"`
    38  }
    39  
    40  func (j *Job) IsValid() *AppError {
    41  	if len(j.Id) != 26 {
    42  		return NewAppError("Job.IsValid", "model.job.is_valid.id.app_error", nil, "id="+j.Id, http.StatusBadRequest)
    43  	}
    44  
    45  	if j.CreateAt == 0 {
    46  		return NewAppError("Job.IsValid", "model.job.is_valid.create_at.app_error", nil, "id="+j.Id, http.StatusBadRequest)
    47  	}
    48  
    49  	switch j.Type {
    50  	case JOB_TYPE_DATA_RETENTION:
    51  	case JOB_TYPE_ELASTICSEARCH_POST_INDEXING:
    52  	case JOB_TYPE_ELASTICSEARCH_POST_AGGREGATION:
    53  	case JOB_TYPE_LDAP_SYNC:
    54  	case JOB_TYPE_MESSAGE_EXPORT:
    55  	default:
    56  		return NewAppError("Job.IsValid", "model.job.is_valid.type.app_error", nil, "id="+j.Id, http.StatusBadRequest)
    57  	}
    58  
    59  	switch j.Status {
    60  	case JOB_STATUS_PENDING:
    61  	case JOB_STATUS_IN_PROGRESS:
    62  	case JOB_STATUS_SUCCESS:
    63  	case JOB_STATUS_ERROR:
    64  	case JOB_STATUS_CANCEL_REQUESTED:
    65  	case JOB_STATUS_CANCELED:
    66  	default:
    67  		return NewAppError("Job.IsValid", "model.job.is_valid.status.app_error", nil, "id="+j.Id, http.StatusBadRequest)
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func (js *Job) ToJson() string {
    74  	b, _ := json.Marshal(js)
    75  	return string(b)
    76  }
    77  
    78  func JobFromJson(data io.Reader) *Job {
    79  	var job Job
    80  	if err := json.NewDecoder(data).Decode(&job); err == nil {
    81  		return &job
    82  	} else {
    83  		return nil
    84  	}
    85  }
    86  
    87  func JobsToJson(jobs []*Job) string {
    88  	b, _ := json.Marshal(jobs)
    89  	return string(b)
    90  }
    91  
    92  func JobsFromJson(data io.Reader) []*Job {
    93  	var jobs []*Job
    94  	if err := json.NewDecoder(data).Decode(&jobs); err == nil {
    95  		return jobs
    96  	} else {
    97  		return nil
    98  	}
    99  }
   100  
   101  func (js *Job) DataToJson() string {
   102  	b, _ := json.Marshal(js.Data)
   103  	return string(b)
   104  }
   105  
   106  type Worker interface {
   107  	Run()
   108  	Stop()
   109  	JobChannel() chan<- Job
   110  }
   111  
   112  type Scheduler interface {
   113  	Name() string
   114  	JobType() string
   115  	Enabled(cfg *Config) bool
   116  	NextScheduleTime(cfg *Config, now time.Time, pendingJobs bool, lastSuccessfulJob *Job) *time.Time
   117  	ScheduleJob(cfg *Config, pendingJobs bool, lastSuccessfulJob *Job) (*Job, *AppError)
   118  }