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