github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/cronjob/api.go (about)

     1  package cronjob
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/robfig/cron/v3"
     7  
     8  	confid "github.com/machinefi/w3bstream/pkg/depends/conf/id"
     9  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx"
    10  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder"
    11  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes"
    12  	"github.com/machinefi/w3bstream/pkg/enums"
    13  	"github.com/machinefi/w3bstream/pkg/errors/status"
    14  	"github.com/machinefi/w3bstream/pkg/models"
    15  	"github.com/machinefi/w3bstream/pkg/types"
    16  )
    17  
    18  type CreateReq struct {
    19  	models.CronJobInfo
    20  }
    21  
    22  type CondArgs struct {
    23  	ProjectID  types.SFID   `name:"-"`
    24  	CronJobIDs []types.SFID `in:"query" name:"cronJobID,omitempty"`
    25  	EventTypes []string     `in:"query" name:"eventType,omitempty"`
    26  }
    27  
    28  func (r *CondArgs) Condition() builder.SqlCondition {
    29  	var (
    30  		m  = &models.CronJob{}
    31  		cs []builder.SqlCondition
    32  	)
    33  
    34  	if r.ProjectID != 0 {
    35  		cs = append(cs, m.ColProjectID().Eq(r.ProjectID))
    36  	}
    37  	if len(r.CronJobIDs) > 0 {
    38  		cs = append(cs, m.ColCronJobID().In(r.CronJobIDs))
    39  	}
    40  	if len(r.EventTypes) > 0 {
    41  		cs = append(cs, m.ColEventType().In(r.EventTypes))
    42  	}
    43  	cs = append(cs, m.ColDeletedAt().Eq(0))
    44  	return builder.And(cs...)
    45  }
    46  
    47  type ListReq struct {
    48  	CondArgs
    49  	datatypes.Pager
    50  }
    51  
    52  func (r *ListReq) Additions() builder.Additions {
    53  	m := &models.CronJob{}
    54  	return builder.Additions{
    55  		builder.OrderBy(
    56  			builder.DescOrder(m.ColUpdatedAt()),
    57  			builder.DescOrder(m.ColCreatedAt()),
    58  		),
    59  		r.Pager.Addition(),
    60  	}
    61  }
    62  
    63  type ListRsp struct {
    64  	Data  []models.CronJob `json:"data"`
    65  	Total int64            `json:"total"`
    66  }
    67  
    68  func List(ctx context.Context, r *ListReq) (*ListRsp, error) {
    69  	var (
    70  		d = types.MustMgrDBExecutorFromContext(ctx)
    71  		m = &models.CronJob{}
    72  
    73  		err  error
    74  		ret  = &ListRsp{}
    75  		cond = r.Condition()
    76  		adds = r.Additions()
    77  	)
    78  
    79  	ret.Data, err = m.List(d, cond, adds...)
    80  	if err != nil {
    81  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
    82  	}
    83  	ret.Total, err = m.Count(d, cond)
    84  	if err != nil {
    85  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
    86  	}
    87  	return ret, nil
    88  }
    89  
    90  func Create(ctx context.Context, r *CreateReq) (*models.CronJob, error) {
    91  	d := types.MustMgrDBExecutorFromContext(ctx)
    92  	idg := confid.MustSFIDGeneratorFromContext(ctx)
    93  	prj := types.MustProjectFromContext(ctx)
    94  
    95  	if _, err := cron.ParseStandard(r.CronExpressions); err != nil {
    96  		return nil, status.InvalidCronExpressions.StatusErr().WithDesc(err.Error())
    97  	}
    98  
    99  	n := *r
   100  	n.EventType = getEventType(n.EventType)
   101  	m := &models.CronJob{
   102  		RelCronJob:  models.RelCronJob{CronJobID: idg.MustGenSFID()},
   103  		RelProject:  models.RelProject{ProjectID: prj.ProjectID},
   104  		CronJobInfo: n.CronJobInfo,
   105  	}
   106  	if err := m.Create(d); err != nil {
   107  		if sqlx.DBErr(err).IsConflict() {
   108  			return nil, status.CronJobConflict
   109  		}
   110  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
   111  	}
   112  	return m, nil
   113  }
   114  
   115  func RemoveBySFID(ctx context.Context, id types.SFID) error {
   116  	d := types.MustMgrDBExecutorFromContext(ctx)
   117  	m := &models.CronJob{RelCronJob: models.RelCronJob{CronJobID: id}}
   118  
   119  	if err := m.DeleteByCronJobID(d); err != nil {
   120  		return status.DatabaseError.StatusErr().WithDesc(err.Error())
   121  	}
   122  	return nil
   123  }
   124  
   125  func GetBySFID(ctx context.Context, id types.SFID) (*models.CronJob, error) {
   126  	d := types.MustMgrDBExecutorFromContext(ctx)
   127  
   128  	m := &models.CronJob{RelCronJob: models.RelCronJob{CronJobID: id}}
   129  	if err := m.FetchByCronJobID(d); err != nil {
   130  		if sqlx.DBErr(err).IsNotFound() {
   131  			return nil, status.CronJobNotFound
   132  		}
   133  		return nil, status.DatabaseError.StatusErr().WithDesc(err.Error())
   134  	}
   135  	return m, nil
   136  }
   137  
   138  func getEventType(eventType string) string {
   139  	if eventType == "" {
   140  		return enums.EVENTTYPEDEFAULT
   141  	}
   142  	return eventType
   143  }