github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/job.go (about)

     1  // Copyright (c) 2015-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_BLEVE_POST_INDEXING            = "bleve_post_indexing"
    19  	JOB_TYPE_LDAP_SYNC                      = "ldap_sync"
    20  	JOB_TYPE_MIGRATIONS                     = "migrations"
    21  	JOB_TYPE_PLUGINS                        = "plugins"
    22  	JOB_TYPE_EXPIRY_NOTIFY                  = "expiry_notify"
    23  
    24  	JOB_STATUS_PENDING          = "pending"
    25  	JOB_STATUS_IN_PROGRESS      = "in_progress"
    26  	JOB_STATUS_SUCCESS          = "success"
    27  	JOB_STATUS_ERROR            = "error"
    28  	JOB_STATUS_CANCEL_REQUESTED = "cancel_requested"
    29  	JOB_STATUS_CANCELED         = "canceled"
    30  	JOB_STATUS_WARNING          = "warning"
    31  )
    32  
    33  type Job struct {
    34  	Id             string            `json:"id"`
    35  	Type           string            `json:"type"`
    36  	Priority       int64             `json:"priority"`
    37  	CreateAt       int64             `json:"create_at"`
    38  	StartAt        int64             `json:"start_at"`
    39  	LastActivityAt int64             `json:"last_activity_at"`
    40  	Status         string            `json:"status"`
    41  	Progress       int64             `json:"progress"`
    42  	Data           map[string]string `json:"data"`
    43  }
    44  
    45  func (j *Job) IsValid() *AppError {
    46  	if !IsValidId(j.Id) {
    47  		return NewAppError("Job.IsValid", "model.job.is_valid.id.app_error", nil, "id="+j.Id, http.StatusBadRequest)
    48  	}
    49  
    50  	if j.CreateAt == 0 {
    51  		return NewAppError("Job.IsValid", "model.job.is_valid.create_at.app_error", nil, "id="+j.Id, http.StatusBadRequest)
    52  	}
    53  
    54  	switch j.Type {
    55  	case JOB_TYPE_DATA_RETENTION:
    56  	case JOB_TYPE_ELASTICSEARCH_POST_INDEXING:
    57  	case JOB_TYPE_ELASTICSEARCH_POST_AGGREGATION:
    58  	case JOB_TYPE_BLEVE_POST_INDEXING:
    59  	case JOB_TYPE_LDAP_SYNC:
    60  	case JOB_TYPE_MESSAGE_EXPORT:
    61  	case JOB_TYPE_MIGRATIONS:
    62  	case JOB_TYPE_PLUGINS:
    63  	case JOB_TYPE_EXPIRY_NOTIFY:
    64  	default:
    65  		return NewAppError("Job.IsValid", "model.job.is_valid.type.app_error", nil, "id="+j.Id, http.StatusBadRequest)
    66  	}
    67  
    68  	switch j.Status {
    69  	case JOB_STATUS_PENDING:
    70  	case JOB_STATUS_IN_PROGRESS:
    71  	case JOB_STATUS_SUCCESS:
    72  	case JOB_STATUS_ERROR:
    73  	case JOB_STATUS_CANCEL_REQUESTED:
    74  	case JOB_STATUS_CANCELED:
    75  	default:
    76  		return NewAppError("Job.IsValid", "model.job.is_valid.status.app_error", nil, "id="+j.Id, http.StatusBadRequest)
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  func (j *Job) ToJson() string {
    83  	b, _ := json.Marshal(j)
    84  	return string(b)
    85  }
    86  
    87  func JobFromJson(data io.Reader) *Job {
    88  	var job Job
    89  	if err := json.NewDecoder(data).Decode(&job); err == nil {
    90  		return &job
    91  	} else {
    92  		return nil
    93  	}
    94  }
    95  
    96  func JobsToJson(jobs []*Job) string {
    97  	b, _ := json.Marshal(jobs)
    98  	return string(b)
    99  }
   100  
   101  func JobsFromJson(data io.Reader) []*Job {
   102  	var jobs []*Job
   103  	if err := json.NewDecoder(data).Decode(&jobs); err == nil {
   104  		return jobs
   105  	} else {
   106  		return nil
   107  	}
   108  }
   109  
   110  func (j *Job) DataToJson() string {
   111  	b, _ := json.Marshal(j.Data)
   112  	return string(b)
   113  }
   114  
   115  type Worker interface {
   116  	Run()
   117  	Stop()
   118  	JobChannel() chan<- Job
   119  }
   120  
   121  type Scheduler interface {
   122  	Name() string
   123  	JobType() string
   124  	Enabled(cfg *Config) bool
   125  	NextScheduleTime(cfg *Config, now time.Time, pendingJobs bool, lastSuccessfulJob *Job) *time.Time
   126  	ScheduleJob(cfg *Config, pendingJobs bool, lastSuccessfulJob *Job) (*Job, *AppError)
   127  }