diff --git a/errand-routes.go b/errand-routes.go index 5682f2d..b8a7cae 100644 --- a/errand-routes.go +++ b/errand-routes.go @@ -8,6 +8,8 @@ import ( "net/http" gin "github.com/gin-gonic/gin" badger "github.com/dgraph-io/badger" + utils "github.com/polygon-io/errands-server/utils" + schemas "github.com/polygon-io/errands-server/schemas" ) @@ -18,7 +20,7 @@ type UpdateRequest struct { Logs []string `json:"logs"` } func ( s *ErrandsServer ) updateErrand( c *gin.Context ){ - var updatedErrand *Errand + var updatedErrand *schemas.Errand var updateReq UpdateRequest if err := c.ShouldBind(&updateReq); err != nil { c.JSON(http.StatusBadRequest, gin.H{ @@ -27,7 +29,7 @@ func ( s *ErrandsServer ) updateErrand( c *gin.Context ){ }) return } - updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error { + updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error { if errand.Status != "active" { return errors.New("Errand must be in active state to update progress") } @@ -63,7 +65,7 @@ type FailedRequest struct { Reason string `json:"reason" binding:"required"` } func ( s *ErrandsServer ) failedErrand( c *gin.Context ){ - var updatedErrand *Errand + var updatedErrand *schemas.Errand var failedReq FailedRequest if err := c.ShouldBind(&failedReq); err != nil { c.JSON(http.StatusBadRequest, gin.H{ @@ -72,15 +74,15 @@ func ( s *ErrandsServer ) failedErrand( c *gin.Context ){ }) return } - updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error { + updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error { // if errand.Status != "active" { // return errors.New("Errand must be in active state to fail") // } // Update this errand attributes: - if err := errand.addToLogs("ERROR", failedReq.Reason); err != nil { + if err := errand.AddToLogs("ERROR", failedReq.Reason); err != nil { return err } - errand.Failed = getTimestamp() + errand.Failed = utils.GetTimestamp() errand.Status = "failed" errand.Progress = 0 if errand.Options.Retries > 0 { @@ -112,7 +114,7 @@ type CompletedRequest struct { Results *gin.H `json:"results"` } func ( s *ErrandsServer ) completeErrand( c *gin.Context ){ - var updatedErrand *Errand + var updatedErrand *schemas.Errand var compReq CompletedRequest if err := c.ShouldBind(&compReq); err != nil { c.JSON(http.StatusBadRequest, gin.H{ @@ -122,15 +124,15 @@ func ( s *ErrandsServer ) completeErrand( c *gin.Context ){ return } shouldDelete := false - updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error { + updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error { // if errand.Status != "active" { // return errors.New("Errand must be in active state to complete") // } // Update this errand attributes: - if err := errand.addToLogs("INFO", "Completed!"); err != nil { + if err := errand.AddToLogs("INFO", "Completed!"); err != nil { return err } - errand.Completed = getTimestamp() + errand.Completed = utils.GetTimestamp() errand.Status = "completed" errand.Progress = 100 errand.Results = compReq.Results @@ -163,13 +165,13 @@ func ( s *ErrandsServer ) completeErrand( c *gin.Context ){ func ( s *ErrandsServer ) retryErrand( c *gin.Context ){ - var updatedErrand *Errand - updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error { + var updatedErrand *schemas.Errand + updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error { if errand.Status == "inactive" { return errors.New("Cannot retry errand which is in inactive state") } // Update this errand attributes: - if err := errand.addToLogs("INFO", "Retrying!"); err != nil { + if err := errand.AddToLogs("INFO", "Retrying!"); err != nil { return err } errand.Status = "inactive" @@ -194,7 +196,7 @@ func ( s *ErrandsServer ) retryErrand( c *gin.Context ){ func ( s *ErrandsServer ) logToErrand( c *gin.Context ){ - var logReq Log + var logReq schemas.Log if err := c.ShouldBind(&logReq); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "message": "Invalid Parameters", @@ -202,12 +204,12 @@ func ( s *ErrandsServer ) logToErrand( c *gin.Context ){ }) return } - updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *Errand ) error { + updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error { if errand.Status != "active" { return errors.New("Errand must be in active state to log to") } // Update this errand attributes: - return errand.addToLogs(logReq.Severity, logReq.Message) + return errand.AddToLogs(logReq.Severity, logReq.Message) }) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ @@ -260,14 +262,14 @@ func ( s *ErrandsServer ) deleteErrandByID( id string ) error { If no error is returned, the errand will be saved in the DB with the new attributes. */ -func ( s *ErrandsServer ) UpdateErrandByID( id string, fn func( *Errand ) error ) ( *Errand, error ) { - var updatedErrand *Errand +func ( s *ErrandsServer ) UpdateErrandByID( id string, fn func( *schemas.Errand ) error ) ( *schemas.Errand, error ) { + var updatedErrand *schemas.Errand err := s.DB.Update(func(txn *badger.Txn) error { item, err := txn.Get([]byte( id )); if err != nil { return err } err = item.Value(func(v []byte) error { - errand := &Errand{} + errand := &schemas.Errand{} err := errand.UnmarshalJSON( v ); if err != nil { return err } diff --git a/errands-routes.go b/errands-routes.go index 541bd1b..1fbb54e 100644 --- a/errands-routes.go +++ b/errands-routes.go @@ -13,6 +13,8 @@ import ( "encoding/json" gin "github.com/gin-gonic/gin" badger "github.com/dgraph-io/badger" + utils "github.com/polygon-io/errands-server/utils" + schemas "github.com/polygon-io/errands-server/schemas" ) @@ -44,7 +46,7 @@ func ( s *ErrandsServer ) errandNotifications( c *gin.Context ){ case t, ok := <-client.Notifications: if ok { // If we are subscribed to this event type: - if contains(client.EventSubs, t.Event) || client.EventSubs[0] == "*" { + if utils.Contains(client.EventSubs, t.Event) || client.EventSubs[0] == "*" { jsonData, _ := json.Marshal( t ) client.Gin.SSEvent("message", string( jsonData )) w.Flush() @@ -69,7 +71,7 @@ func ( s *ErrandsServer ) errandNotifications( c *gin.Context ){ func ( s *ErrandsServer ) createErrand( c *gin.Context ){ log.Println("creating errand") - var item Errand + var item schemas.Errand if err := c.ShouldBindJSON(&item); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "message": "Errand validation failed!", @@ -77,7 +79,7 @@ func ( s *ErrandsServer ) createErrand( c *gin.Context ){ }) return } - item.setDefaults() + item.SetDefaults() err := s.DB.Update(func( txn *badger.Txn ) error { return s.saveErrand( txn, &item ) }) @@ -97,8 +99,8 @@ func ( s *ErrandsServer ) createErrand( c *gin.Context ){ -func ( s *ErrandsServer ) saveErrand( txn *badger.Txn, errand *Errand ) error { - if !contains(ErrandStatuses, errand.Status) { +func ( s *ErrandsServer ) saveErrand( txn *badger.Txn, errand *schemas.Errand ) error { + if !utils.Contains(schemas.ErrandStatuses, errand.Status) { return errors.New("Invalid errand status state") } bytes, err := errand.MarshalJSON(); if err != nil { @@ -111,7 +113,7 @@ func ( s *ErrandsServer ) saveErrand( txn *badger.Txn, errand *Errand ) error { func ( s *ErrandsServer ) getAllErrands( c *gin.Context ){ - errands, err := s.GetErrandsBy(func( errand *Errand ) bool { + errands, err := s.GetErrandsBy(func( errand *schemas.Errand ) bool { return true }) if err != nil { @@ -132,7 +134,7 @@ func ( s *ErrandsServer ) getAllErrands( c *gin.Context ){ func ( s *ErrandsServer ) getFilteredErrands( c *gin.Context ){ key := c.Param("key") value := c.Param("val") - errands, err := s.GetErrandsBy(func( errand *Errand ) bool { + errands, err := s.GetErrandsBy(func( errand *schemas.Errand ) bool { switch key { case "status": return ( errand.Status == value ) @@ -173,7 +175,7 @@ func ( s *ErrandsServer ) updateFilteredErrands( c *gin.Context ){ }) return } - errands, err := s.GetErrandsBy(func( errand *Errand ) bool { + errands, err := s.GetErrandsBy(func( errand *schemas.Errand ) bool { switch key { case "status": return ( errand.Status == value ) @@ -191,7 +193,7 @@ func ( s *ErrandsServer ) updateFilteredErrands( c *gin.Context ){ } }else { if updateReq.Status != "" { - _, err = s.UpdateErrandByID( errand.ID, func( e *Errand ) error { + _, err = s.UpdateErrandByID( errand.ID, func( e *schemas.Errand ) error { e.Status = updateReq.Status return nil }) @@ -221,8 +223,8 @@ func ( s *ErrandsServer ) updateFilteredErrands( c *gin.Context ){ func ( s *ErrandsServer ) processErrand( c *gin.Context ){ - var procErrand *Errand - errands := make([]*Errand, 0) + var procErrand *schemas.Errand + errands := make([]*schemas.Errand, 0) hasFound := false typeFilter := c.Param("type") err := s.DB.Update(func(txn *badger.Txn) error { @@ -234,7 +236,7 @@ func ( s *ErrandsServer ) processErrand( c *gin.Context ){ item := it.Item() err := item.Value(func( v []byte ) error { - errand := &Errand{} + errand := &schemas.Errand{} err := errand.UnmarshalJSON( v ); if err != nil { return err } @@ -264,11 +266,11 @@ func ( s *ErrandsServer ) processErrand( c *gin.Context ){ }) procErrand = errands[0] // We are processing this errand: - procErrand.Started = getTimestamp() + procErrand.Started = utils.GetTimestamp() procErrand.Attempts += 1 procErrand.Status = "active" procErrand.Progress = 0.0 - if err := procErrand.addToLogs("INFO", "Started!"); err != nil { + if err := procErrand.AddToLogs("INFO", "Started!"); err != nil { return err } err := s.saveErrand( txn, procErrand ); if err != nil { @@ -306,8 +308,8 @@ func ( s *ErrandsServer ) processErrand( c *gin.Context ){ -func ( s *ErrandsServer ) GetErrandsBy( fn func ( *Errand ) bool ) ( []*Errand, error ) { - errands := make([]*Errand, 0) +func ( s *ErrandsServer ) GetErrandsBy( fn func ( *schemas.Errand ) bool ) ( []*schemas.Errand, error ) { + errands := make([]*schemas.Errand, 0) err := s.DB.View(func( txn *badger.Txn ) error { opts := badger.DefaultIteratorOptions opts.PrefetchSize = 50 @@ -316,7 +318,7 @@ func ( s *ErrandsServer ) GetErrandsBy( fn func ( *Errand ) bool ) ( []*Errand, for it.Rewind(); it.Valid(); it.Next() { item := it.Item() err := item.Value(func( v []byte ) error { - errand := &Errand{} + errand := &schemas.Errand{} err := errand.UnmarshalJSON( v ); if err != nil { return err } diff --git a/errands-server.go b/errands-server.go index 2c2aad2..6bcea80 100644 --- a/errands-server.go +++ b/errands-server.go @@ -13,6 +13,7 @@ import ( badger "github.com/dgraph-io/badger" binding "github.com/gin-gonic/gin/binding" validator "gopkg.in/go-playground/validator.v8" + schemas "github.com/polygon-io/errands-server/schemas" ) @@ -23,8 +24,8 @@ import ( //easyjson:json type Notification struct { - Event string `json:"event"` - Errand Errand `json:"errand,omitempty"` + Event string `json:"event"` + Errand schemas.Errand `json:"errand,omitempty"` } @@ -62,7 +63,7 @@ func NewErrandsServer( cfg *Config ) *ErrandsServer { return obj } -func ( s *ErrandsServer ) AddNotification( event string, errand *Errand ){ +func ( s *ErrandsServer ) AddNotification( event string, errand *schemas.Errand ){ obj := &Notification{ Event: event, Errand: *errand, @@ -119,7 +120,7 @@ func ( s *ErrandsServer ) killDB(){ func UserStructLevelValidation(v *validator.Validate, structLevel *validator.StructLevel) { - errand := structLevel.CurrentStruct.Interface().(Errand) + errand := structLevel.CurrentStruct.Interface().(schemas.Errand) if errand.Options.TTL < 5 && errand.Options.TTL != 0 { structLevel.ReportError( reflect.ValueOf(errand.Options.TTL), "ttl", "ttl", "must be positive, and more than 5", @@ -153,7 +154,7 @@ func ( s *ErrandsServer) createAPI(){ // s.API.Use(gzip.Gzip(gzip.DefaultCompression)) if v, ok := binding.Validator.Engine().(*validator.Validate); ok { - v.RegisterStructValidation(UserStructLevelValidation, Errand{}) + v.RegisterStructValidation(UserStructLevelValidation, schemas.Errand{}) } // Singular errand Routes: diff --git a/main_easyjson.go b/main_easyjson.go index 51a5f6a..fa21a77 100644 --- a/main_easyjson.go +++ b/main_easyjson.go @@ -4,7 +4,6 @@ package main import ( json "encoding/json" - gin "github.com/gin-gonic/gin" easyjson "github.com/mailru/easyjson" jlexer "github.com/mailru/easyjson/jlexer" jwriter "github.com/mailru/easyjson/jwriter" @@ -101,556 +100,3 @@ func (v *Notification) UnmarshalJSON(data []byte) error { func (v *Notification) UnmarshalEasyJSON(l *jlexer.Lexer) { easyjson89aae3efDecodeGithubComPolygonIoErrandsServer(l, v) } -func easyjson89aae3efDecodeGithubComPolygonIoErrandsServer1(in *jlexer.Lexer, out *Log) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeString() - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "severity": - out.Severity = string(in.String()) - case "message": - out.Message = string(in.String()) - case "timestamp": - out.Timestamp = int64(in.Int64()) - default: - in.SkipRecursive() - } - in.WantComma() - } - in.Delim('}') - if isTopLevel { - in.Consumed() - } -} -func easyjson89aae3efEncodeGithubComPolygonIoErrandsServer1(out *jwriter.Writer, in Log) { - out.RawByte('{') - first := true - _ = first - { - const prefix string = ",\"severity\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Severity)) - } - { - const prefix string = ",\"message\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Message)) - } - { - const prefix string = ",\"timestamp\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Int64(int64(in.Timestamp)) - } - out.RawByte('}') -} - -// MarshalJSON supports json.Marshaler interface -func (v Log) MarshalJSON() ([]byte, error) { - w := jwriter.Writer{} - easyjson89aae3efEncodeGithubComPolygonIoErrandsServer1(&w, v) - return w.Buffer.BuildBytes(), w.Error -} - -// MarshalEasyJSON supports easyjson.Marshaler interface -func (v Log) MarshalEasyJSON(w *jwriter.Writer) { - easyjson89aae3efEncodeGithubComPolygonIoErrandsServer1(w, v) -} - -// UnmarshalJSON supports json.Unmarshaler interface -func (v *Log) UnmarshalJSON(data []byte) error { - r := jlexer.Lexer{Data: data} - easyjson89aae3efDecodeGithubComPolygonIoErrandsServer1(&r, v) - return r.Error() -} - -// UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *Log) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson89aae3efDecodeGithubComPolygonIoErrandsServer1(l, v) -} -func easyjson89aae3efDecodeGithubComPolygonIoErrandsServer2(in *jlexer.Lexer, out *Errand) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeString() - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "id": - out.ID = string(in.String()) - case "name": - out.Name = string(in.String()) - case "type": - out.Type = string(in.String()) - case "options": - easyjson89aae3efDecode(in, &out.Options) - case "data": - if in.IsNull() { - in.Skip() - out.Data = nil - } else { - if out.Data == nil { - out.Data = new(gin.H) - } - if in.IsNull() { - in.Skip() - } else { - in.Delim('{') - if !in.IsDelim('}') { - *out.Data = make(gin.H) - } else { - *out.Data = nil - } - for !in.IsDelim('}') { - key := string(in.String()) - in.WantColon() - var v1 interface{} - if m, ok := v1.(easyjson.Unmarshaler); ok { - m.UnmarshalEasyJSON(in) - } else if m, ok := v1.(json.Unmarshaler); ok { - _ = m.UnmarshalJSON(in.Raw()) - } else { - v1 = in.Interface() - } - (*out.Data)[key] = v1 - in.WantComma() - } - in.Delim('}') - } - } - case "created": - out.Created = int64(in.Int64()) - case "status": - out.Status = string(in.String()) - case "results": - if in.IsNull() { - in.Skip() - out.Results = nil - } else { - if out.Results == nil { - out.Results = new(gin.H) - } - if in.IsNull() { - in.Skip() - } else { - in.Delim('{') - if !in.IsDelim('}') { - *out.Results = make(gin.H) - } else { - *out.Results = nil - } - for !in.IsDelim('}') { - key := string(in.String()) - in.WantColon() - var v2 interface{} - if m, ok := v2.(easyjson.Unmarshaler); ok { - m.UnmarshalEasyJSON(in) - } else if m, ok := v2.(json.Unmarshaler); ok { - _ = m.UnmarshalJSON(in.Raw()) - } else { - v2 = in.Interface() - } - (*out.Results)[key] = v2 - in.WantComma() - } - in.Delim('}') - } - } - case "progress": - out.Progress = float64(in.Float64()) - case "attempts": - out.Attempts = int(in.Int()) - case "started": - out.Started = int64(in.Int64()) - case "failed": - out.Failed = int64(in.Int64()) - case "compelted": - out.Completed = int64(in.Int64()) - case "logs": - if in.IsNull() { - in.Skip() - out.Logs = nil - } else { - in.Delim('[') - if out.Logs == nil { - if !in.IsDelim(']') { - out.Logs = make([]Log, 0, 1) - } else { - out.Logs = []Log{} - } - } else { - out.Logs = (out.Logs)[:0] - } - for !in.IsDelim(']') { - var v3 Log - (v3).UnmarshalEasyJSON(in) - out.Logs = append(out.Logs, v3) - in.WantComma() - } - in.Delim(']') - } - default: - in.SkipRecursive() - } - in.WantComma() - } - in.Delim('}') - if isTopLevel { - in.Consumed() - } -} -func easyjson89aae3efEncodeGithubComPolygonIoErrandsServer2(out *jwriter.Writer, in Errand) { - out.RawByte('{') - first := true - _ = first - { - const prefix string = ",\"id\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.ID)) - } - { - const prefix string = ",\"name\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Name)) - } - { - const prefix string = ",\"type\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Type)) - } - { - const prefix string = ",\"options\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - easyjson89aae3efEncode(out, in.Options) - } - if in.Data != nil { - const prefix string = ",\"data\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - if *in.Data == nil && (out.Flags&jwriter.NilMapAsEmpty) == 0 { - out.RawString(`null`) - } else { - out.RawByte('{') - v4First := true - for v4Name, v4Value := range *in.Data { - if v4First { - v4First = false - } else { - out.RawByte(',') - } - out.String(string(v4Name)) - out.RawByte(':') - if m, ok := v4Value.(easyjson.Marshaler); ok { - m.MarshalEasyJSON(out) - } else if m, ok := v4Value.(json.Marshaler); ok { - out.Raw(m.MarshalJSON()) - } else { - out.Raw(json.Marshal(v4Value)) - } - } - out.RawByte('}') - } - } - { - const prefix string = ",\"created\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Int64(int64(in.Created)) - } - if in.Status != "" { - const prefix string = ",\"status\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.String(string(in.Status)) - } - if in.Results != nil { - const prefix string = ",\"results\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - if *in.Results == nil && (out.Flags&jwriter.NilMapAsEmpty) == 0 { - out.RawString(`null`) - } else { - out.RawByte('{') - v5First := true - for v5Name, v5Value := range *in.Results { - if v5First { - v5First = false - } else { - out.RawByte(',') - } - out.String(string(v5Name)) - out.RawByte(':') - if m, ok := v5Value.(easyjson.Marshaler); ok { - m.MarshalEasyJSON(out) - } else if m, ok := v5Value.(json.Marshaler); ok { - out.Raw(m.MarshalJSON()) - } else { - out.Raw(json.Marshal(v5Value)) - } - } - out.RawByte('}') - } - } - { - const prefix string = ",\"progress\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Float64(float64(in.Progress)) - } - { - const prefix string = ",\"attempts\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Int(int(in.Attempts)) - } - if in.Started != 0 { - const prefix string = ",\"started\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Int64(int64(in.Started)) - } - if in.Failed != 0 { - const prefix string = ",\"failed\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Int64(int64(in.Failed)) - } - if in.Completed != 0 { - const prefix string = ",\"compelted\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Int64(int64(in.Completed)) - } - if len(in.Logs) != 0 { - const prefix string = ",\"logs\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - { - out.RawByte('[') - for v6, v7 := range in.Logs { - if v6 > 0 { - out.RawByte(',') - } - (v7).MarshalEasyJSON(out) - } - out.RawByte(']') - } - } - out.RawByte('}') -} - -// MarshalJSON supports json.Marshaler interface -func (v Errand) MarshalJSON() ([]byte, error) { - w := jwriter.Writer{} - easyjson89aae3efEncodeGithubComPolygonIoErrandsServer2(&w, v) - return w.Buffer.BuildBytes(), w.Error -} - -// MarshalEasyJSON supports easyjson.Marshaler interface -func (v Errand) MarshalEasyJSON(w *jwriter.Writer) { - easyjson89aae3efEncodeGithubComPolygonIoErrandsServer2(w, v) -} - -// UnmarshalJSON supports json.Unmarshaler interface -func (v *Errand) UnmarshalJSON(data []byte) error { - r := jlexer.Lexer{Data: data} - easyjson89aae3efDecodeGithubComPolygonIoErrandsServer2(&r, v) - return r.Error() -} - -// UnmarshalEasyJSON supports easyjson.Unmarshaler interface -func (v *Errand) UnmarshalEasyJSON(l *jlexer.Lexer) { - easyjson89aae3efDecodeGithubComPolygonIoErrandsServer2(l, v) -} -func easyjson89aae3efDecode(in *jlexer.Lexer, out *struct { - TTL int `json:"ttl,omitempty"` - Retries int `json:"retries,omitempty"` - Priority int `json:"priority,omitempty"` - DeleteOnCompleted bool `json:"deleteOnCompleted,omitempty"` -}) { - isTopLevel := in.IsStart() - if in.IsNull() { - if isTopLevel { - in.Consumed() - } - in.Skip() - return - } - in.Delim('{') - for !in.IsDelim('}') { - key := in.UnsafeString() - in.WantColon() - if in.IsNull() { - in.Skip() - in.WantComma() - continue - } - switch key { - case "ttl": - out.TTL = int(in.Int()) - case "retries": - out.Retries = int(in.Int()) - case "priority": - out.Priority = int(in.Int()) - case "deleteOnCompleted": - out.DeleteOnCompleted = bool(in.Bool()) - default: - in.SkipRecursive() - } - in.WantComma() - } - in.Delim('}') - if isTopLevel { - in.Consumed() - } -} -func easyjson89aae3efEncode(out *jwriter.Writer, in struct { - TTL int `json:"ttl,omitempty"` - Retries int `json:"retries,omitempty"` - Priority int `json:"priority,omitempty"` - DeleteOnCompleted bool `json:"deleteOnCompleted,omitempty"` -}) { - out.RawByte('{') - first := true - _ = first - if in.TTL != 0 { - const prefix string = ",\"ttl\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Int(int(in.TTL)) - } - if in.Retries != 0 { - const prefix string = ",\"retries\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Int(int(in.Retries)) - } - if in.Priority != 0 { - const prefix string = ",\"priority\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Int(int(in.Priority)) - } - if in.DeleteOnCompleted { - const prefix string = ",\"deleteOnCompleted\":" - if first { - first = false - out.RawString(prefix[1:]) - } else { - out.RawString(prefix) - } - out.Bool(bool(in.DeleteOnCompleted)) - } - out.RawByte('}') -} diff --git a/schemas/glide.yaml b/schemas/glide.yaml new file mode 100644 index 0000000..d6268b5 --- /dev/null +++ b/schemas/glide.yaml @@ -0,0 +1,14 @@ +package: github.com/polygon-io/errands-server/schemas +import: +- package: github.com/gin-gonic/gin + version: ^1.3.0 +- package: github.com/google/uuid + version: ^1.1.1 +- package: github.com/mailru/easyjson + subpackages: + - jlexer + - jwriter +- package: github.com/polygon-io/errands-server + version: ^1.0.0 + subpackages: + - utils diff --git a/errand.go b/schemas/schemas.go similarity index 82% rename from errand.go rename to schemas/schemas.go index c7dd9f8..5b541d8 100644 --- a/errand.go +++ b/schemas/schemas.go @@ -1,16 +1,17 @@ -package main - +package schemas import ( "errors" - uuid "github.com/google/uuid" gin "github.com/gin-gonic/gin" + uuid "github.com/google/uuid" + utils "github.com/polygon-io/errands-server/utils" ) + var ErrandStatuses []string = []string{"inactive", "active", "failed", "completed"} //easyjson:json type Errand struct { @@ -50,44 +51,38 @@ type Log struct { + func NewErrand() *Errand { obj := &Errand{} - obj.setDefaults() + obj.SetDefaults() return obj } -func ( e *Errand ) setDefaults(){ +func ( e *Errand ) SetDefaults(){ uid := uuid.New() uidText, err := uid.MarshalText(); if err != nil { panic( err ) } e.ID = string( uidText ) e.Status = "inactive" - e.Created = getTimestamp() + e.Created = utils.GetTimestamp() e.Logs = make( []Log, 0 ) } -func ( e *Errand ) addToLogs( severity, message string ) error { - if !contains( LogSeverities, severity ) { +func ( e *Errand ) AddToLogs( severity, message string ) error { + if !utils.Contains( LogSeverities, severity ) { return errors.New("Invalid log severity") } obj := Log{ Severity: severity, Message: message, - Timestamp: getTimestamp(), + Timestamp: utils.GetTimestamp(), } e.Logs = append( e.Logs, obj ) return nil } -func contains(s []string, e string) bool { - for _, a := range s { - if a == e { - return true - } - } - return false -} \ No newline at end of file + diff --git a/schemas/schemas_easyjson.go b/schemas/schemas_easyjson.go new file mode 100644 index 0000000..52f617f --- /dev/null +++ b/schemas/schemas_easyjson.go @@ -0,0 +1,573 @@ +// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT. + +package schemas + +import ( + json "encoding/json" + gin "github.com/gin-gonic/gin" + easyjson "github.com/mailru/easyjson" + jlexer "github.com/mailru/easyjson/jlexer" + jwriter "github.com/mailru/easyjson/jwriter" +) + +// suppress unused package warning +var ( + _ *json.RawMessage + _ *jlexer.Lexer + _ *jwriter.Writer + _ easyjson.Marshaler +) + +func easyjson2189435aDecodeGithubComPolygonIoErrandsServerSchemas(in *jlexer.Lexer, out *Log) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeString() + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "severity": + out.Severity = string(in.String()) + case "message": + out.Message = string(in.String()) + case "timestamp": + out.Timestamp = int64(in.Int64()) + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson2189435aEncodeGithubComPolygonIoErrandsServerSchemas(out *jwriter.Writer, in Log) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"severity\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Severity)) + } + { + const prefix string = ",\"message\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Message)) + } + { + const prefix string = ",\"timestamp\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(in.Timestamp)) + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v Log) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson2189435aEncodeGithubComPolygonIoErrandsServerSchemas(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v Log) MarshalEasyJSON(w *jwriter.Writer) { + easyjson2189435aEncodeGithubComPolygonIoErrandsServerSchemas(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *Log) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson2189435aDecodeGithubComPolygonIoErrandsServerSchemas(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *Log) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson2189435aDecodeGithubComPolygonIoErrandsServerSchemas(l, v) +} +func easyjson2189435aDecodeGithubComPolygonIoErrandsServerSchemas1(in *jlexer.Lexer, out *Errand) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeString() + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "id": + out.ID = string(in.String()) + case "name": + out.Name = string(in.String()) + case "type": + out.Type = string(in.String()) + case "options": + easyjson2189435aDecode(in, &out.Options) + case "data": + if in.IsNull() { + in.Skip() + out.Data = nil + } else { + if out.Data == nil { + out.Data = new(gin.H) + } + if in.IsNull() { + in.Skip() + } else { + in.Delim('{') + if !in.IsDelim('}') { + *out.Data = make(gin.H) + } else { + *out.Data = nil + } + for !in.IsDelim('}') { + key := string(in.String()) + in.WantColon() + var v1 interface{} + if m, ok := v1.(easyjson.Unmarshaler); ok { + m.UnmarshalEasyJSON(in) + } else if m, ok := v1.(json.Unmarshaler); ok { + _ = m.UnmarshalJSON(in.Raw()) + } else { + v1 = in.Interface() + } + (*out.Data)[key] = v1 + in.WantComma() + } + in.Delim('}') + } + } + case "created": + out.Created = int64(in.Int64()) + case "status": + out.Status = string(in.String()) + case "results": + if in.IsNull() { + in.Skip() + out.Results = nil + } else { + if out.Results == nil { + out.Results = new(gin.H) + } + if in.IsNull() { + in.Skip() + } else { + in.Delim('{') + if !in.IsDelim('}') { + *out.Results = make(gin.H) + } else { + *out.Results = nil + } + for !in.IsDelim('}') { + key := string(in.String()) + in.WantColon() + var v2 interface{} + if m, ok := v2.(easyjson.Unmarshaler); ok { + m.UnmarshalEasyJSON(in) + } else if m, ok := v2.(json.Unmarshaler); ok { + _ = m.UnmarshalJSON(in.Raw()) + } else { + v2 = in.Interface() + } + (*out.Results)[key] = v2 + in.WantComma() + } + in.Delim('}') + } + } + case "progress": + out.Progress = float64(in.Float64()) + case "attempts": + out.Attempts = int(in.Int()) + case "started": + out.Started = int64(in.Int64()) + case "failed": + out.Failed = int64(in.Int64()) + case "compelted": + out.Completed = int64(in.Int64()) + case "logs": + if in.IsNull() { + in.Skip() + out.Logs = nil + } else { + in.Delim('[') + if out.Logs == nil { + if !in.IsDelim(']') { + out.Logs = make([]Log, 0, 1) + } else { + out.Logs = []Log{} + } + } else { + out.Logs = (out.Logs)[:0] + } + for !in.IsDelim(']') { + var v3 Log + (v3).UnmarshalEasyJSON(in) + out.Logs = append(out.Logs, v3) + in.WantComma() + } + in.Delim(']') + } + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson2189435aEncodeGithubComPolygonIoErrandsServerSchemas1(out *jwriter.Writer, in Errand) { + out.RawByte('{') + first := true + _ = first + { + const prefix string = ",\"id\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.ID)) + } + { + const prefix string = ",\"name\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Name)) + } + { + const prefix string = ",\"type\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Type)) + } + { + const prefix string = ",\"options\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + easyjson2189435aEncode(out, in.Options) + } + if in.Data != nil { + const prefix string = ",\"data\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + if *in.Data == nil && (out.Flags&jwriter.NilMapAsEmpty) == 0 { + out.RawString(`null`) + } else { + out.RawByte('{') + v4First := true + for v4Name, v4Value := range *in.Data { + if v4First { + v4First = false + } else { + out.RawByte(',') + } + out.String(string(v4Name)) + out.RawByte(':') + if m, ok := v4Value.(easyjson.Marshaler); ok { + m.MarshalEasyJSON(out) + } else if m, ok := v4Value.(json.Marshaler); ok { + out.Raw(m.MarshalJSON()) + } else { + out.Raw(json.Marshal(v4Value)) + } + } + out.RawByte('}') + } + } + { + const prefix string = ",\"created\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(in.Created)) + } + if in.Status != "" { + const prefix string = ",\"status\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Status)) + } + if in.Results != nil { + const prefix string = ",\"results\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + if *in.Results == nil && (out.Flags&jwriter.NilMapAsEmpty) == 0 { + out.RawString(`null`) + } else { + out.RawByte('{') + v5First := true + for v5Name, v5Value := range *in.Results { + if v5First { + v5First = false + } else { + out.RawByte(',') + } + out.String(string(v5Name)) + out.RawByte(':') + if m, ok := v5Value.(easyjson.Marshaler); ok { + m.MarshalEasyJSON(out) + } else if m, ok := v5Value.(json.Marshaler); ok { + out.Raw(m.MarshalJSON()) + } else { + out.Raw(json.Marshal(v5Value)) + } + } + out.RawByte('}') + } + } + { + const prefix string = ",\"progress\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Float64(float64(in.Progress)) + } + { + const prefix string = ",\"attempts\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int(int(in.Attempts)) + } + if in.Started != 0 { + const prefix string = ",\"started\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(in.Started)) + } + if in.Failed != 0 { + const prefix string = ",\"failed\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(in.Failed)) + } + if in.Completed != 0 { + const prefix string = ",\"compelted\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int64(int64(in.Completed)) + } + if len(in.Logs) != 0 { + const prefix string = ",\"logs\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + { + out.RawByte('[') + for v6, v7 := range in.Logs { + if v6 > 0 { + out.RawByte(',') + } + (v7).MarshalEasyJSON(out) + } + out.RawByte(']') + } + } + out.RawByte('}') +} + +// MarshalJSON supports json.Marshaler interface +func (v Errand) MarshalJSON() ([]byte, error) { + w := jwriter.Writer{} + easyjson2189435aEncodeGithubComPolygonIoErrandsServerSchemas1(&w, v) + return w.Buffer.BuildBytes(), w.Error +} + +// MarshalEasyJSON supports easyjson.Marshaler interface +func (v Errand) MarshalEasyJSON(w *jwriter.Writer) { + easyjson2189435aEncodeGithubComPolygonIoErrandsServerSchemas1(w, v) +} + +// UnmarshalJSON supports json.Unmarshaler interface +func (v *Errand) UnmarshalJSON(data []byte) error { + r := jlexer.Lexer{Data: data} + easyjson2189435aDecodeGithubComPolygonIoErrandsServerSchemas1(&r, v) + return r.Error() +} + +// UnmarshalEasyJSON supports easyjson.Unmarshaler interface +func (v *Errand) UnmarshalEasyJSON(l *jlexer.Lexer) { + easyjson2189435aDecodeGithubComPolygonIoErrandsServerSchemas1(l, v) +} +func easyjson2189435aDecode(in *jlexer.Lexer, out *struct { + TTL int `json:"ttl,omitempty"` + Retries int `json:"retries,omitempty"` + Priority int `json:"priority,omitempty"` + DeleteOnCompleted bool `json:"deleteOnCompleted,omitempty"` +}) { + isTopLevel := in.IsStart() + if in.IsNull() { + if isTopLevel { + in.Consumed() + } + in.Skip() + return + } + in.Delim('{') + for !in.IsDelim('}') { + key := in.UnsafeString() + in.WantColon() + if in.IsNull() { + in.Skip() + in.WantComma() + continue + } + switch key { + case "ttl": + out.TTL = int(in.Int()) + case "retries": + out.Retries = int(in.Int()) + case "priority": + out.Priority = int(in.Int()) + case "deleteOnCompleted": + out.DeleteOnCompleted = bool(in.Bool()) + default: + in.SkipRecursive() + } + in.WantComma() + } + in.Delim('}') + if isTopLevel { + in.Consumed() + } +} +func easyjson2189435aEncode(out *jwriter.Writer, in struct { + TTL int `json:"ttl,omitempty"` + Retries int `json:"retries,omitempty"` + Priority int `json:"priority,omitempty"` + DeleteOnCompleted bool `json:"deleteOnCompleted,omitempty"` +}) { + out.RawByte('{') + first := true + _ = first + if in.TTL != 0 { + const prefix string = ",\"ttl\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int(int(in.TTL)) + } + if in.Retries != 0 { + const prefix string = ",\"retries\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int(int(in.Retries)) + } + if in.Priority != 0 { + const prefix string = ",\"priority\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int(int(in.Priority)) + } + if in.DeleteOnCompleted { + const prefix string = ",\"deleteOnCompleted\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Bool(bool(in.DeleteOnCompleted)) + } + out.RawByte('}') +} diff --git a/utils.go b/utils.go deleted file mode 100644 index b7fd299..0000000 --- a/utils.go +++ /dev/null @@ -1,16 +0,0 @@ - -package main - -import ( - "time" -) - - -func getTimestamp() int64 { - return ( time.Now().UnixNano() / 1000000 ) -} - - - - - diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..f802154 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,22 @@ + +package utils + + +import ( + "time" +) + + +func GetTimestamp() int64 { + return ( time.Now().UnixNano() / 1000000 ) +} + + +func Contains(s []string, e string) bool { + for _, a := range s { + if a == e { + return true + } + } + return false +}