p06.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package query
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. "adm-data/errors"
  8. "adm-data/model"
  9. v1 "adm-data/pb/v1"
  10. jsoniter "github.com/json-iterator/go"
  11. "git.getensh.com/common/gopkgsv2/database"
  12. "git.getensh.com/common/gopkgsv2/logger"
  13. "go.uber.org/zap"
  14. "google.golang.org/grpc/status"
  15. )
  16. type p06Request struct {
  17. ExportId string `json:"export_id"`
  18. }
  19. type p06Response struct {
  20. State int8 `json:"state"`
  21. Addr string `json:"addr"`
  22. }
  23. func p06(ctx context.Context, params string) (reply *v1.QueryResponse, err error) {
  24. reply = &v1.QueryResponse{}
  25. // 捕获各个task中的异常并返回给调用者
  26. defer func() {
  27. if r := recover(); r != nil {
  28. err = fmt.Errorf("%+v", r)
  29. e := &status.Status{}
  30. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  31. logger.Error("err",
  32. zap.String("system_err", err.Error()),
  33. zap.Stack("stacktrace"))
  34. }
  35. }
  36. }()
  37. var req p06Request
  38. err = jsoniter.UnmarshalFromString(params, &req)
  39. if err != nil && req.ExportId == "" {
  40. return nil, errors.ParamsError
  41. }
  42. info, err := model.NewExportStyleTaskModel().Get(database.DB().Where("export_id = ?", req.ExportId))
  43. if err != nil {
  44. return nil, errors.DataNotExistError
  45. }
  46. res := p06Response{
  47. State: info.Status,
  48. Addr: info.Path,
  49. }
  50. if info.Status == 0 && time.Now().Unix()-info.CreatedAt.Unix() >= 3600 {
  51. res.State = 2
  52. }
  53. reply.Data, _ = jsoniter.MarshalToString(res)
  54. return reply, nil
  55. }