@@ -2,8 +2,13 @@ package contextHandler
22
33import (
44 "context"
5+ "fmt"
6+ "os/user"
7+ "runtime"
58 "strings"
69
10+ "github.com/alitto/pond/v2"
11+ "github.com/goforj/godump"
712 "github.com/mt1976/frantic-core/logHandler"
813)
914
@@ -29,3 +34,82 @@ func Debug(c context.Context, name string) {
2934 logHandler .Trace .Printf ("[%v] DEBUG ENDS" , name )
3035 logHandler .Trace .Printf ("[%v] DEBUG ENDS" , name )
3136}
37+
38+ func AddUserContext (ctx context.Context , userID , userName string ) context.Context {
39+ existingUserID := GetSession_UserKey (ctx )
40+ existingUserName := GetSession_UserCode (ctx )
41+
42+ logHandler .Trace .Printf ("Existing User Context: %v=%v - %v=%v" , "userID" , existingUserID , "userName" , existingUserName )
43+ if existingUserID != "" && existingUserName != "" {
44+ logHandler .Event .Printf ("User Context already exists: %v=%v - %v=%v" , "userID" , existingUserID , "userName" , existingUserName )
45+ return ctx
46+ }
47+
48+ if userID == "" || userName == "" {
49+ currentUser := GetUserDetails ()
50+ userID = currentUser .Uid
51+ userName = currentUser .Username
52+ }
53+
54+ userCode := userID + "_" + userName
55+ userKey := userID
56+
57+ ctx = SetSession_UserKey (ctx , userKey )
58+ ctx = SetSession_UserCode (ctx , userCode )
59+
60+ logHandler .Trace .Printf ("User Context Added: %v(%v)=%v(%v)" , userCode , GetSession_UserCode (ctx ), userKey , GetSession_UserKey (ctx ))
61+
62+ return ctx
63+ }
64+
65+ func GetUserDetails () * user.User {
66+ currentUser , err := user .Current ()
67+ if err != nil {
68+ logHandler .Error .Fatalln (err .Error ())
69+ }
70+ // if running on windows, the UID is in the format of "S-1-5-21-3849575818-2607088806-4266749144", we need to convert it to a more readable format
71+ if runningOnWindows () {
72+ // Example UID on windows "S-1-5-21-3849575818-2607088806-4266749144"
73+ // Tokenize the UID
74+ tkID := strings .Split (currentUser .Uid , "-" )
75+ // Concatenate the token in 0, 1, 2, 3
76+ currentUser .Uid = fmt .Sprintf ("%s%s%s%s" , tkID [0 ], tkID [1 ], tkID [2 ], tkID [3 ])
77+ }
78+ return currentUser
79+ }
80+
81+ func runningOnWindows () bool {
82+ return strings .Contains (strings .ToLower (runtime .GOOS ), "windows" )
83+ }
84+
85+ func NewWorkerPool (ctx context.Context , poolName string , poolSize int ) context.Context {
86+ // Define a reasonable number of workers for the worker pool. This can be adjusted based on the expected load and performance requirements. For a typical application, starting with 10 workers is a good balance between concurrency and resource usage.
87+ noWorkers := poolSize
88+
89+ logHandler .Event .Println ("Creating Worker Pool..." )
90+ ctx = addWorkerPoolName (ctx , poolName )
91+ workerPool := pond .NewPool (noWorkers , pond .WithContext (ctx ), pond .WithQueueSize (noWorkers * 3 ))
92+ // Now add the pool to the context so it can be used by the jobs
93+ ctx = context .WithValue (ctx , WorkerPoolKey , workerPool )
94+ defer workerPool .StopAndWait ()
95+ logHandler .Event .Printf ("Worker Pool Created with %d workers" , noWorkers )
96+
97+ return ctx
98+ }
99+
100+ var workerPoolNameKey = new ("WorkerPoolName ")
101+
102+ func addWorkerPoolName (ctx context.Context , poolName string ) context.Context {
103+ logHandler .Trace .Printf ("Setting Worker Pool Name in Context: %v=%v" , workerPoolNameKey .name , poolName )
104+ return context .WithValue (ctx , workerPoolNameKey , poolName )
105+ }
106+
107+ func GetWorkerPoolName (ctx context.Context ) string {
108+ logHandler .Trace .Printf ("Retrieving Worker Pool Name from Context: %v" , godump .DumpStr (ctx ))
109+ value := ctx .Value (workerPoolNameKey )
110+ if value == nil {
111+ logHandler .Warning .Printf ("Worker pool name (%v) requested but not found in context, returning empty string" , workerPoolNameKey .name )
112+ return ""
113+ }
114+ return value .(string )
115+ }
0 commit comments