session.go 930 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package workers
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "os"
  6. "strings"
  7. "time"
  8. "golang-fave/engine/utils"
  9. "github.com/vladimirok5959/golang-server-sessions/session"
  10. "github.com/vladimirok5959/golang-worker/worker"
  11. )
  12. func SessionCleaner(www_dir string) *worker.Worker {
  13. return worker.New(func(ctx context.Context, w *worker.Worker, o *[]worker.Iface) {
  14. if www_dir, ok := (*o)[0].(string); ok {
  15. session_clean(ctx, www_dir)
  16. }
  17. select {
  18. case <-ctx.Done():
  19. case <-time.After(1 * time.Hour):
  20. return
  21. }
  22. }, &[]worker.Iface{
  23. www_dir,
  24. })
  25. }
  26. func session_clean(ctx context.Context, www_dir string) {
  27. files, err := ioutil.ReadDir(www_dir)
  28. if err == nil {
  29. for _, file := range files {
  30. select {
  31. case <-ctx.Done():
  32. return
  33. default:
  34. tmpdir := strings.Join([]string{www_dir, file.Name(), "tmp"}, string(os.PathSeparator))
  35. if utils.IsDirExists(tmpdir) {
  36. session.Clean(tmpdir)
  37. }
  38. }
  39. }
  40. }
  41. }