Files
EdgeAPI/internal/rpc/services/service_sys_locker.go
2021-04-05 20:48:33 +08:00

65 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package services
import (
"context"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
// 互斥锁管理
type SysLockerService struct {
BaseService
}
// 获得锁
func (this *SysLockerService) SysLockerLock(ctx context.Context, req *pb.SysLockerLockRequest) (*pb.SysLockerLockResponse, error) {
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
if err != nil {
_, err = this.ValidateMonitor(ctx)
if err != nil {
return nil, err
}
}
key := req.Key
if userId > 0 {
key = "@user" // 这里不加入用户ID防止多个用户间冲突
}
timeout := req.TimeoutSeconds
if timeout <= 0 {
timeout = 60
} else if timeout > 86400 { // 最多不能超过1天
timeout = 86400
}
var tx = this.NullTx()
ok, err := models.SharedSysLockerDAO.Lock(tx, key, timeout)
if err != nil {
return nil, err
}
return &pb.SysLockerLockResponse{Ok: ok}, nil
}
// 释放锁
func (this *SysLockerService) SysLockerUnlock(ctx context.Context, req *pb.SysLockerUnlockRequest) (*pb.RPCSuccess, error) {
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
if err != nil {
_, err = this.ValidateMonitor(ctx)
if err != nil {
return nil, err
}
}
key := req.Key
if userId > 0 {
key = "@user"
}
var tx = this.NullTx()
err = models.SharedSysLockerDAO.Unlock(tx, key)
if err != nil {
return nil, err
}
return this.Success()
}