Skip to content

Commit

Permalink
No longer use Badger. Currently in memory only. TODO: Write persist t…
Browse files Browse the repository at this point in the history
…o disk code.
  • Loading branch information
qrpike committed Sep 23, 2019
1 parent b41f06c commit 32efc50
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 485 deletions.
33 changes: 11 additions & 22 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

package main


import (
// "io"
"errors"
Expand All @@ -10,42 +8,33 @@ import (
gin "github.com/gin-gonic/gin"
)








type Client struct {
ErrandsServer *ErrandsServer
Notifications chan *Notification
Gin *gin.Context
EventSubs []string
ErrandsServer *ErrandsServer
Notifications chan *Notification
Gin *gin.Context
EventSubs []string
}

func ( s *ErrandsServer ) RemoveClient( c *Client ){
close( c.Notifications )
func (s *ErrandsServer) RemoveClient(c *Client) {
close(c.Notifications)
s.UnregisterClient <- c
}

func ( s *ErrandsServer ) NewClient( c *gin.Context ) ( *Client, error ) {
func (s *ErrandsServer) NewClient(c *gin.Context) (*Client, error) {
obj := &Client{
Notifications: make(chan *Notification, 10),
ErrandsServer: s,
Gin: c,
Gin: c,
}
events := c.DefaultQuery("events", "*")
obj.EventSubs = strings.Split(events, ",")
if len( obj.EventSubs ) < 1 {
if len(obj.EventSubs) < 1 {
return obj, errors.New("Must have at least 1 event subscription")
}
s.RegisterClient <- obj
return obj, nil
}

func ( c *Client ) Gone(){
c.ErrandsServer.RemoveClient( c )
func (c *Client) Gone() {
c.ErrandsServer.RemoveClient(c)
}


182 changes: 69 additions & 113 deletions errand-routes.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@

package main


import (
// "fmt"
"errors"
"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"
utils "github.com/polygon-io/errands-server/utils"
)




type UpdateRequest struct {
Progress float64 `json:"progress"`
Logs []string `json:"logs"`
Progress float64 `json:"progress"`
Logs []string `json:"logs"`
}
func ( s *ErrandsServer ) updateErrand( c *gin.Context ){

func (s *ErrandsServer) updateErrand(c *gin.Context) {
var updatedErrand *schemas.Errand
var updateReq UpdateRequest
if err := c.ShouldBind(&updateReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid Parameters",
"error": err.Error(),
"error": err.Error(),
})
return
}
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.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")
}
Expand All @@ -38,43 +34,39 @@ func ( s *ErrandsServer ) updateErrand( c *gin.Context ){
if updateReq.Progress < 0 || updateReq.Progress >= 101 {
return errors.New("Progress must be between 0 - 100")
}
errand.Progress = float64( updateReq.Progress )
errand.Progress = float64(updateReq.Progress)
}
return nil
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
"error": err.Error(),
})
return
}
s.AddNotification( "updated", updatedErrand )
s.AddNotification("updated", updatedErrand)
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"status": "OK",
"results": updatedErrand,
})
}






type FailedRequest struct {
Reason string `json:"reason" binding:"required"`
Reason string `json:"reason" binding:"required"`
}
func ( s *ErrandsServer ) failedErrand( c *gin.Context ){

func (s *ErrandsServer) failedErrand(c *gin.Context) {
var updatedErrand *schemas.Errand
var failedReq FailedRequest
if err := c.ShouldBind(&failedReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid Parameters",
"error": err.Error(),
"error": err.Error(),
})
return
}
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.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")
// }
Expand All @@ -96,77 +88,70 @@ func ( s *ErrandsServer ) failedErrand( c *gin.Context ){
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
"error": err.Error(),
})
return
}
s.AddNotification( "failed", updatedErrand )
s.AddNotification("failed", updatedErrand)
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"status": "OK",
"results": updatedErrand,
})
}




type CompletedRequest struct {
Results *gin.H `json:"results"`
Results *gin.H `json:"results"`
}
func ( s *ErrandsServer ) completeErrand( c *gin.Context ){

func (s *ErrandsServer) completeErrand(c *gin.Context) {
var updatedErrand *schemas.Errand
var compReq CompletedRequest
if err := c.ShouldBind(&compReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid Parameters",
"error": err.Error(),
"error": err.Error(),
})
return
}
shouldDelete := false
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")
// }
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func(errand *schemas.Errand) error {
// Update this errand attributes:
if err := errand.AddToLogs("INFO", "Completed!"); err != nil {
return err
}
errand.Completed = utils.GetTimestamp()
errand.Status = "completed"
errand.Progress = 100
errand.Results = compReq.Results
// errand.Results = compReq.Results
// If we should delete this errand upon completion:
if errand.Options.DeleteOnCompleted == true {
shouldDelete = true
}
return nil
})
if err == nil && shouldDelete == true && updatedErrand.ID != "" {
err = s.deleteErrandByID( updatedErrand.ID )
err = s.deleteErrandByID(updatedErrand.ID)
}
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
})
return

if shouldDelete == true && updatedErrand.ID != "" {
if err := s.deleteErrandByID(updatedErrand.ID); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
})
return
}
}
s.AddNotification( "completed", updatedErrand )
s.AddNotification("completed", updatedErrand)
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"status": "OK",
"results": updatedErrand,
})
}






func ( s *ErrandsServer ) retryErrand( c *gin.Context ){
func (s *ErrandsServer) retryErrand(c *gin.Context) {
var updatedErrand *schemas.Errand
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.Errand ) error {
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")
}
Expand All @@ -181,30 +166,27 @@ func ( s *ErrandsServer ) retryErrand( c *gin.Context ){
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
"error": err.Error(),
})
return
}
s.AddNotification( "retry", updatedErrand )
s.AddNotification("retry", updatedErrand)
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"status": "OK",
"results": updatedErrand,
})
}




func ( s *ErrandsServer ) logToErrand( c *gin.Context ){
func (s *ErrandsServer) logToErrand(c *gin.Context) {
var logReq schemas.Log
if err := c.ShouldBind(&logReq); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"message": "Invalid Parameters",
"error": err.Error(),
"error": err.Error(),
})
return
}
updatedErrand, err := s.UpdateErrandByID(c.Param("id"), func( errand *schemas.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")
}
Expand All @@ -214,26 +196,23 @@ func ( s *ErrandsServer ) logToErrand( c *gin.Context ){
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
"error": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": "OK",
"status": "OK",
"results": updatedErrand,
})
}





func ( s *ErrandsServer ) deleteErrand( c *gin.Context ){
err := s.deleteErrandByID( c.Param("id") )
func (s *ErrandsServer) deleteErrand(c *gin.Context) {
s.Store.Delete(c.Param("id"))
err := s.deleteErrandByID(c.Param("id"))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Internal Server Error!",
"error": err.Error(),
"error": err.Error(),
})
return
}
Expand All @@ -242,48 +221,25 @@ func ( s *ErrandsServer ) deleteErrand( c *gin.Context ){
})
}








func ( s *ErrandsServer ) deleteErrandByID( id string ) error {
return s.DB.Update(func(txn *badger.Txn) error {
return txn.Delete([]byte( id ))
})
func (s *ErrandsServer) deleteErrandByID(id string) error {
s.Store.Delete(id)
return nil
}



/*
Pass in a function which will be called allowing you to update the errand.
If no error is returned, the errand will be saved in the DB with the new
UpdateErrandByID Lets you pass in a function which will be called allowing you to update the errand.
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( *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 := &schemas.Errand{}
err := errand.UnmarshalJSON( v ); if err != nil {
return err
}
err = fn( errand ); if err != nil {
return err
}
updatedErrand = errand
err = s.saveErrand( txn, errand ); if err != nil {
return err
}
return nil
})
return err
})
return updatedErrand, err
*/
func (s *ErrandsServer) UpdateErrandByID(id string, fn func(*schemas.Errand) error) (*schemas.Errand, error) {
errandObj, found := s.Store.Get(id)
if !found {
return nil, errors.New("Errand with this ID not found")
}
errand := errandObj.(schemas.Errand)
if err := fn(&errand); err != nil {
return nil, errors.New("Error in given update function (fn)")
}
s.Store.SetDefault(id, errand)
return &errand, nil
}

Loading

0 comments on commit 32efc50

Please sign in to comment.