From 3fc48c8094e0a74a6bea2c22bc149975e4ed956e Mon Sep 17 00:00:00 2001 From: "i.navrotskyj" Date: Thu, 27 Jun 2024 10:42:35 +0300 Subject: [PATCH] WTEL-4679 --- agent_manager/agent_manager.go | 24 ++++++++++++++++++------ agent_manager/interface.go | 1 + app/agent.go | 5 +++++ app/app.go | 1 + model/agent.go | 9 +++++---- store/sqlstore/agent_store.go | 6 ++++-- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/agent_manager/agent_manager.go b/agent_manager/agent_manager.go index 3c7ccea3..73fd1334 100644 --- a/agent_manager/agent_manager.go +++ b/agent_manager/agent_manager.go @@ -21,13 +21,16 @@ var ( MaxAgentOnlineWithOutSocSec = 60 ) +type HookAutoOfflineAgent func(agent AgentObject) + type agentManager struct { - store store.Store - mq mq.MQ - watcher *utils.Watcher - nodeId string - startOnce sync.Once - agentsCache utils.ObjectCache + store store.Store + mq mq.MQ + watcher *utils.Watcher + nodeId string + startOnce sync.Once + agentsCache utils.ObjectCache + hookAutoOfflineAgent HookAutoOfflineAgent sync.Mutex } @@ -41,6 +44,10 @@ func NewAgentManager(nodeId string, s store.Store, mq_ mq.MQ) AgentManager { return &am } +func (am *agentManager) SetHookAutoOfflineAgent(hook HookAutoOfflineAgent) { + am.hookAutoOfflineAgent = hook +} + func (am *agentManager) Start() { wlog.Debug("starting agent service") am.watcher = utils.MakeWatcher("AgentManager", watcherPollingInterval, am.changeDeadlineState) @@ -193,9 +200,14 @@ func (am *agentManager) changeDeadlineState() { *s = *s + "/sip" } } + if a.TeamUpdatedAt() != v.TeamUpdatedAt { + a.SetTeamUpdatedAt(v.TeamUpdatedAt) + } err = am.SetOffline(a, s) if err != nil { wlog.Error(err.Error()) + } else if am.hookAutoOfflineAgent != nil { + am.hookAutoOfflineAgent(a) } } } diff --git a/agent_manager/interface.go b/agent_manager/interface.go index b8d33ffb..e8ea6255 100644 --- a/agent_manager/interface.go +++ b/agent_manager/interface.go @@ -15,6 +15,7 @@ type AgentManager interface { //internal SetAgentOnBreak(agentId int) *model.AppError MissedAttempt(agentId int, attemptId int64, cause string) *model.AppError + SetHookAutoOfflineAgent(hook HookAutoOfflineAgent) } type AgentObject interface { diff --git a/app/agent.go b/app/agent.go index c88831d0..e9463047 100644 --- a/app/agent.go +++ b/app/agent.go @@ -184,6 +184,11 @@ func (app *App) RunTeamTrigger(ctx context.Context, domainId int64, userId int64 return jobId, nil } +func (app *App) hookAutoOfflineAgent(agent agent_manager.AgentObject) { + app.Queue().Manager().AgentTeamHook(model.HookAgentStatus, agent, agent.TeamUpdatedAt()) + return +} + func getString(p *string) string { if p == nil { return "" diff --git a/app/app.go b/app/app.go index b860dc0b..9f61a566 100644 --- a/app/app.go +++ b/app/app.go @@ -97,6 +97,7 @@ func New(options ...string) (outApp *App, outErr error) { app.engine.Start() app.agentManager = agent_manager.NewAgentManager(app.GetInstanceId(), app.Store, app.MQ) + app.agentManager.SetHookAutoOfflineAgent(app.hookAutoOfflineAgent) app.agentManager.Start() app.flowManager = client.NewFlowManager(app.Cluster().ServiceDiscovery()) diff --git a/model/agent.go b/model/agent.go index 6991797a..48247ba9 100644 --- a/model/agent.go +++ b/model/agent.go @@ -61,10 +61,11 @@ type Agent struct { } type AgentHashKey struct { - Id int `json:"id" db:"id" ` - UpdatedAt int64 `json:"updated_at" db:"updated_at"` - Sip bool `json:"sip" db:"sip"` - Ws bool `json:"ws" db:"ws"` + Id int `json:"id" db:"id" ` + UpdatedAt int64 `json:"updated_at" db:"updated_at"` + Sip bool `json:"sip" db:"sip"` + Ws bool `json:"ws" db:"ws"` + TeamUpdatedAt int64 `json:"team_updated_at" db:"team_updated_at"` } type AgentChannel struct { diff --git a/store/sqlstore/agent_store.go b/store/sqlstore/agent_store.go index 3d8e7468..2bb2b41f 100644 --- a/store/sqlstore/agent_store.go +++ b/store/sqlstore/agent_store.go @@ -351,8 +351,10 @@ func (s *SqlAgentStore) OnlineWithOutActive(sec int) ([]model.AgentHashKey, *mod where p.user_id = a.user_id and p.status = 'web' and p.updated_at >= now() at time zone 'UTC' - (:Sec || ' sec')::interval - ) ws + ) ws, + t.updated_at as team_updated_at from call_center.cc_agent a + left join call_center.cc_team t on t.id = a.team_id where a.status in ('online', 'break_out') and not exists(SELECT 1 FROM directory.wbt_session s @@ -371,7 +373,7 @@ where a.status in ('online', 'break_out') or (p.status = 'web' and p.updated_at >= now() at time zone 'UTC' - (:Sec || ' sec')::interval) ) ) -for update skip locked`, map[string]interface{}{ +for update of a skip locked`, map[string]interface{}{ "Sec": sec, })