github.com/rajatvaryani/mattermost-server@v5.11.1+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  	case JOB_TYPE_PLUGINS:
    59  	default:
    60  		return NewAppError("Job.IsValid", "model.job.is_valid.type.app_error", nil, "id="+j.Id, http.StatusBadRequest)
    61  	}
    62  
    63  	switch j.Status {
    64  	case JOB_STATUS_PENDING:
    65  	case JOB_STATUS_IN_PROGRESS:
    66  	case JOB_STATUS_SUCCESS:
    67  	case JOB_STATUS_ERROR:
    68  	case JOB_STATUS_CANCEL_REQUESTED:
    69  	case JOB_STATUS_CANCELED:
    70  	default:
    71  		return NewAppError("Job.IsValid", "model.job.is_valid.status.app_error", nil, "id="+j.Id, http.StatusBadRequest)
    72  	}
    73  
    74  	return nil
    75  }
    76  
    77  func (js *Job) ToJson() string {
    78  	b, _ := json.Marshal(js)
    79  	return string(b)
    80  }
    81  
    82  func JobFromJson(data io.Reader) *Job {
    83  	var job Job
    84  	if err := json.NewDecoder(data).Decode(&job); err == nil {
    85  		return &job
    86  	} else {
    87  		return nil
    88  	}
    89  }
    90  
    91  func JobsToJson(jobs []*Job) string {
    92  	b, _ := json.Marshal(jobs)
    93  	return string(b)
    94  }
    95  
    96  func JobsFromJson(data io.Reader) []*Job {
    97  	var jobs []*Job
    98  	if err := json.NewDecoder(data).Decode(&jobs); err == nil {
    99  		return jobs
   100  	} else {
   101  		return nil
   102  	}
   103  }
   104  
   105  func (js *Job) DataToJson() string {
   106  	b, _ := json.Marshal(js.Data)
   107  	return string(b)
   108  }
   109  
   110  type Worker interface {
   111  	Run()
   112  	Stop()
   113  	JobChannel() chan<- Job
   114  }
   115  
   116  type Scheduler interface {
   117  	Name() string
   118  	JobType() string
   119  	Enabled(cfg *Config) bool
   120  	NextScheduleTime(cfg *Config, now time.Time, pendingJobs bool, lastSuccessfulJob *Job) *time.Time
   121  	ScheduleJob(cfg *Config, pendingJobs bool, lastSuccessfulJob *Job) (*Job, *AppError)
   122  }