github.com/ngicks/gokugen@v0.0.5/task_storage/repository.go (about) 1 package taskstorage 2 3 import ( 4 "errors" 5 "fmt" 6 "time" 7 ) 8 9 var ( 10 // ErrNoEnt is used in Repository implementation 11 // to express situation where TaskInfo for operation does not exist. 12 ErrNoEnt = errors.New("no ent") 13 // ErrInvalidEnt is used in Repository implementation. 14 // This is indication of an invalid TaskInfo insertion or a stored info is invalid. 15 ErrInvalidEnt = errors.New("invalid ent") 16 // ErrNotUpdatableState is used in Repository implementation. 17 // It is returned when TaskInfo for the update is not updatable. 18 ErrNotUpdatableState = errors.New("not updatable") 19 ) 20 21 type StateUpdater interface { 22 // UpdateState updates task State to new if current state is old. 23 // swapped is true if update is successful, false otherwise. 24 UpdateState(taskId string, old, new TaskState) (swapped bool, err error) 25 } 26 27 type Updater interface { 28 // Update updates the info with diff. 29 // Implementation may limit updatable state. 30 // If state is not updatable, return wrapped or direct ErrNotUpdatableState. 31 Update(taskId string, diff UpdateDiff) (err error) 32 } 33 34 type Repository interface { 35 // Insert inserts given model to the repository. 36 // Id and LastModified field are ignored and will be newly created. 37 Insert(TaskInfo) (taskId string, err error) 38 // GetUpdatedSince fetches all elements modified after or equal to t. 39 // Result must be ordered by LastModified in ascending order. 40 // Implementation may limit the number of fetched elements. 41 // Implementation may or may not unmarshal Param to any. Fetched Param may be []byte or naive 42 GetUpdatedSince(t time.Time) ([]TaskInfo, error) 43 // GetById fetches an element associated with the id. 44 // If the id does not exist in the repository, 45 // GetById reports it by returning wrapped or unwrapped ErrNoEnt error. 46 // Implementation may or may not unmarshal Param to any. 47 GetById(id string) (TaskInfo, error) 48 // MarkAsDone marks the task as done. 49 // Other than Initialized or Working state is not updatable to done. 50 MarkAsDone(id string) (ok bool, err error) 51 // MarkAsCancelled marks the task as cancelled. 52 // Other than Initialized or Working state is not updatable to cancelled. 53 MarkAsCancelled(id string) (ok bool, err error) 54 // MarkAsFailed marks the task as failed which means workers failed to do this task. 55 // Other than Initialized or Working state is not updatable to failed. 56 MarkAsFailed(id string) (ok bool, err error) 57 } 58 59 type TaskState int 60 61 const ( 62 Initialized TaskState = iota 63 Working 64 Done 65 Cancelled 66 Failed 67 ) 68 69 func NewStateFromString(s string) TaskState { 70 switch s { 71 case "Initialized": 72 return Initialized 73 case "Working": 74 return Working 75 case "Done": 76 return Done 77 case "Cancelled": 78 return Cancelled 79 case "Failed": 80 return Failed 81 } 82 return -1 83 } 84 85 func (ts TaskState) String() string { 86 switch ts { 87 case Initialized: 88 return "Initialized" 89 case Working: 90 return "Working" 91 case Done: 92 return "Done" 93 case Cancelled: 94 return "Cancelled" 95 case Failed: 96 return "Failed" 97 default: 98 return fmt.Sprintf("unknown=%d", ts) 99 } 100 } 101 102 type TaskInfo struct { 103 Id string 104 WorkId string 105 Param any 106 ScheduledTime time.Time 107 State TaskState 108 LastModified time.Time 109 } 110 111 type UpdateKey struct { 112 WorkId bool 113 Param bool 114 ScheduledTime bool 115 State bool 116 } 117 118 type UpdateDiff struct { 119 UpdateKey UpdateKey 120 Diff TaskInfo 121 }