-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: saltbo <[email protected]>
- Loading branch information
Showing
6 changed files
with
121 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package vfs | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/robfig/cron" | ||
"github.com/saltbo/zpan/internal/app/entity" | ||
"github.com/saltbo/zpan/internal/app/repo" | ||
) | ||
|
||
func (v *Vfs) matterCreatedEventHandler(matter *entity.Matter) error { | ||
c := cron.New() | ||
c.Start() | ||
return c.AddFunc("@every 10s", func() { | ||
ctx := context.Background() | ||
if err := v.uploader.UploadDone(ctx, matter); err != nil { | ||
return | ||
} | ||
|
||
c.Stop() | ||
}) | ||
} | ||
|
||
func (v *Vfs) matterDeletedEventHandler(matter *entity.Matter) error { | ||
return nil | ||
} | ||
|
||
func (v *Vfs) cleanExpiredMatters(ctx context.Context) { | ||
matters, _, err := v.matterRepo.FindAll(ctx, &repo.MatterListOption{Draft: true}) | ||
if err != nil { | ||
log.Printf("error getting the files of not uploaded: %s", err) | ||
return | ||
} | ||
|
||
for _, matter := range matters { | ||
if time.Since(matter.CreatedAt) > time.Hour*24 { | ||
continue | ||
} | ||
|
||
if err := v.matterRepo.Delete(ctx, matter.Id); err != nil { | ||
log.Printf("error deleting the file %s: %s", matter.FullPath(), err) | ||
return | ||
} | ||
|
||
log.Printf("deleted the file: %s", matter.FullPath()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,54 @@ | ||
package vfs | ||
|
||
import ( | ||
"github.com/saltbo/zpan/internal/app/repo" | ||
"github.com/saltbo/zpan/internal/app/entity" | ||
"github.com/sourcegraph/conc/pool" | ||
) | ||
|
||
type Worker struct { | ||
matterRepo repo.Matter | ||
const ( | ||
EventActionCreated EventAction = "Created" | ||
EventActionDeleted EventAction = "Deleted" | ||
) | ||
|
||
type ( | ||
EventAction string | ||
EventHandler func(matter *entity.Matter) error | ||
) | ||
|
||
type Event struct { | ||
Action EventAction | ||
Matter *entity.Matter | ||
} | ||
|
||
func NewWorker() *Worker { | ||
return &Worker{} | ||
type EventWorker struct { | ||
eventChan chan Event | ||
eventReg map[EventAction]EventHandler | ||
} | ||
|
||
func (w *Worker) Start() { | ||
func NewWorker() *EventWorker { | ||
return &EventWorker{ | ||
eventChan: make(chan Event), | ||
eventReg: make(map[EventAction]EventHandler), | ||
} | ||
} | ||
|
||
func (w *EventWorker) Run() { | ||
p := pool.New().WithMaxGoroutines(10) | ||
for elem := range w.eventChan { | ||
eventHandle := w.eventReg[elem.Action] | ||
p.Go(func() { | ||
if err := eventHandle(elem.Matter); err != nil { | ||
return | ||
} | ||
}) | ||
} | ||
p.Wait() | ||
} | ||
|
||
func (w *EventWorker) registerEventHandler(action EventAction, h EventHandler) { | ||
w.eventReg[action] = h | ||
} | ||
|
||
func (w *Worker) cleanExpireMatters() { | ||
// matters, total, err := w.matterRepo.FindAll(context.Background(), &repo.MatterListOption{}) | ||
// if err != nil { | ||
// return | ||
// } | ||
// | ||
// w.matterRepo.Delete() | ||
func (w *EventWorker) sendEvent(action EventAction, m *entity.Matter) { | ||
w.eventChan <- Event{Action: action, Matter: m} | ||
} |