// 监视支付单完成 func supervisePaymentOrderFinish(ss []Service) { conn := core.GetRedisConn() defer conn.Close() var id int for { arr, err := redis.Values(conn.Do("BLPOP", variable.KvPaymentOrderFinishQueue, 0)) if err == nil { //通知会员修改,格式如: 1-[create|update] s := string(arr[1].([]byte)) id, err = strconv.Atoi(s) if err == nil { order := dps.PaymentService.GetPaymentOrder(id) for _, v := range ss { if !v.PaymentOrderObs(order) { break } } } } else { appCtx.Log().Println("[ Daemon][ PaymentOrderQueue][ Error] - ", err.Error()) break } } }
// 监视新会员 func superviseMemberUpdate(ss []Service) { conn := core.GetRedisConn() defer conn.Close() var id int for { arr, err := redis.Values(conn.Do("BLPOP", variable.KvMemberUpdateQueue, 0)) if err == nil { //通知会员修改,格式如: 1-[create|update] s := string(arr[1].([]byte)) mArr := strings.Split(s, "-") id, err = strconv.Atoi(mArr[0]) if err == nil { m := dps.MemberService.GetMember(id) for _, v := range ss { if !v.MemberObs(m, mArr[1] == "create") { break } } } } else { appCtx.Log().Println("[ Daemon][ MemberQueue][ Error] - ", err.Error()) break } } }
// 监视新订单 func superviseOrder(ss []Service) { conn := core.GetRedisConn() defer conn.Close() var id int sv := dps.ShoppingService for { arr, err := redis.Values(conn.Do("BLPOP", variable.KvOrderBusinessQueue, 0)) //取出队列的一个元素 if err == nil { id, err = strconv.Atoi(string(arr[1].([]byte))) if err == nil { //通知订单更新 if order := sv.GetSubOrder(id); order != nil { for _, v := range ss { if !v.OrderObs(order) { break } } } } } else { appCtx.Log().Println("[ Daemon][ OrderQueue][ Error] - ", err.Error()) break } } }
// 今日是否结算 func todayIsSettled(unix int64) bool { conn := core.GetRedisConn() defer conn.Close() unix2, err := redis.Int(conn.Do("GET", settleUnixKey)) if err != nil { return false } return unix == int64(unix2) }
func (this *MemberRep) pushToAccountUpdateQueue(memberId int, updateTime int64) { rc := core.GetRedisConn() defer rc.Close() // 保存最后更新时间 mutKey := fmt.Sprintf("%s%d", variable.KvAccountUpdateTime, memberId) rc.Do("SETEX", mutKey, 3600*400, updateTime) // push to tcp notify queue rc.Do("RPUSH", variable.KvAccountUpdateTcpNotifyQueue, memberId) }
// 处理订单,需根据订单不同的状态,作不同的业务 // 返回布尔值,如果返回false,则不继续执行 func (this *defaultService) OrderObs(o *order.SubOrder) bool { defer Recover() conn := core.GetRedisConn() defer conn.Close() if this.app.Debug() { this.app.Log().Println("---订单", o.OrderNo, "状态:", o.State) } if this.sOrder { if o.State == enum.ORDER_WAIT_CONFIRM { //确认订单 dps.ShoppingService.ConfirmOrder(o.Id) } this.updateOrderExpires(conn, o) } return true }
func MemberSummaryNotifyJob(s *nc.SocketServer) { conn := core.GetRedisConn() defer conn.Close() for { values, err := redis.Values(conn.Do("BLPOP", variable.KvMemberUpdateTcpNotifyQueue, 0)) if err == nil { id, err := strconv.Atoi(string(values[1].([]byte))) if err == nil { connList := s.GetConnections(id) if len(connList) > 0 { go pushMemberSummary(s, connList, id) } } } } }
// 加入到发送对列 func (this *MssRep) JoinMailTaskToQueen(v *mss.MailTask) error { var err error if v.Id > 0 { _, _, err = this._conn.GetOrm().Save(v.Id, v) } else { _, _, err = this._conn.GetOrm().Save(nil, v) if err == nil { err = this._conn.ExecScalar("SELECT max(id) FROM pt_mail_queue", &v.Id) } } if err == nil { rc := core.GetRedisConn() defer rc.Close() rc.Do("RPUSH", variable.KvNewMailTask, v.Id) // push to queue } return err }
// 邮件队列 func startMailQueue(ss []Service) { conn := core.GetRedisConn() defer conn.Close() //var id int for { arr, err := redis.Values(conn.Do("BLPOP", variable.KvNewMailTask, 0)) if err == nil { _, err = strconv.Atoi(string(arr[1].([]byte))) if err == nil { //todo: 此处获取所有需发送的邮件,应去掉从数据库批量查询操作 sendForWaitingQueue(ss) } } else { appCtx.Log().Println("[ Daemon][ MailQueue][ Error] - ", err.Error()) break } } }
// 保存会员 func (this *MemberRep) SaveMember(v *member.Member) (int, error) { if v.Id > 0 { rc := core.GetRedisConn() defer rc.Close() // 保存最后更新时间 todo: mutKey := fmt.Sprintf("%s%d", variable.KvMemberUpdateTime, v.Id) rc.Do("SETEX", mutKey, 3600*400, v.UpdateTime) rc.Do("RPUSH", variable.KvMemberUpdateTcpNotifyQueue, v.Id) // push to tcp notify queue // 保存会员信息 _, _, err := this.Connector.GetOrm().Save(v.Id, v) if err == nil { rc.Do("RPUSH", variable.KvMemberUpdateQueue, fmt.Sprintf("%d-update", v.Id)) // push to queue } return v.Id, err } return this.createMember(v) }
func detectOrderExpires(a gof.App) { conn := core.GetRedisConn() defer conn.Close() //获取标记为等待过期的订单 orderId := 0 list, _ := redis.Strings(conn.Do("KEYS", variable.KvOrderExpiresTime+"*")) ss := dps.ShoppingService for _, v := range list { if unix, err := redis.Int64(conn.Do("GET", v)); err == nil { //获取过期时间 if unix < time.Now().Unix() { //订单号 orderId, err = strconv.Atoi(v[len(variable.KvOrderExpiresTime):]) err = ss.CancelOrder(orderId, "订单超时,自动取消") //清除 conn.Do("DEL", v) //清除待取消记录 //log.Println("---",orderId,"---",unix, "--", time.Now().Unix(), v, err) } } } }
func (this *MemberRep) createMember(v *member.Member) (int, error) { var id int64 _, id, err := this.Connector.GetOrm().Save(nil, v) if err != nil { return -1, err } v.Id = int(id) this.initMember(v) rc := core.GetRedisConn() defer rc.Close() rc.Do("RPUSH", variable.KvMemberUpdateQueue, fmt.Sprintf("%d-create", v.Id)) // push to queue // 更新会员数 todo: 考虑去掉 var total = 0 this.Connector.ExecScalar("SELECT COUNT(0) FROM mm_member", &total) gof.CurrentApp.Storage().Set(variable.KvTotalMembers, total) return v.Id, err }
// 保存子订单 func (o *orderRepImpl) SaveSubOrder(v *order.SubOrder) (int, error) { var err error var statusIsChanged bool //业务状态是否改变 d := o.Connector if v.Id <= 0 { statusIsChanged = true } else { var oriStatus int d.ExecScalar("SELECT state FROM sale_sub_order WHERE id=?", &oriStatus, v.Id) statusIsChanged = oriStatus != v.State // 业务状态是否改变 } v.Id, err = orm.Save(o.GetOrm(), v, v.Id) //如果业务状态已经发生改变,则提交到队列 if statusIsChanged && v.Id > 0 { rc := core.GetRedisConn() rc.Do("RPUSH", variable.KvOrderBusinessQueue, v.Id) rc.Close() //log.Println("-----order ",v.Id,v.Status,statusIsChanged,err) } return v.Id, err }
// 通知支付单完成 func (this *paymentRep) NotifyPaymentFinish(paymentOrderId int) error { rc := core.GetRedisConn() defer rc.Close() _, err := rc.Do("RPUSH", variable.KvPaymentOrderFinishQueue, paymentOrderId) return err }
// 保存最新结算日期 func saveLatestSettleUnix(unix int64) { conn := core.GetRedisConn() defer conn.Close() conn.Do("SET", settleUnixKey, unix) }