Skip to content

Commit

Permalink
Code cleaning and Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
prasunanand committed Dec 18, 2024
1 parent 62b2012 commit b941ea4
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 102 deletions.
16 changes: 6 additions & 10 deletions content/content_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func GetContent(relativePath string, contentType string, format string, hash int) models.ContentModel {
log.Info().Msgf("getting content for path : %s", relativePath)
log.Debug().Msgf("getting content for path : %s", relativePath)
// get path info
osPath := GetOSPath(relativePath)
info, err := os.Lstat(osPath)
Expand All @@ -26,7 +26,7 @@ func GetContent(relativePath string, contentType string, format string, hash int

var model models.ContentModel

log.Info().Msgf("%t", info.IsDir())
log.Debug().Msgf("Is directory %t", info.IsDir())
if info.IsDir() {
model = getDirectoryModel(relativePath)
} else {
Expand All @@ -42,7 +42,6 @@ func GetContent(relativePath string, contentType string, format string, hash int
}

func getNotebookModel(path string) models.ContentModel {
log.Info().Msg("Notebook model")

// fmt.Println(path)
osPath := GetOSPath(path)
Expand Down Expand Up @@ -83,7 +82,7 @@ func nbformatReads(data string, version int, capture_validation_error bool) OutN
}

func getDirectoryModel(relativePath string) models.ContentModel {
log.Info().Msgf("relative path %s", relativePath)
log.Debug().Msgf("relative path %s", relativePath)
abspath := GetOSPath(relativePath)

info, err := os.Lstat(abspath)
Expand All @@ -97,7 +96,6 @@ func getDirectoryModel(relativePath string) models.ContentModel {
Path: relativePath,
Last_modified: info.ModTime().GoString(),
}
// fmt.Println(path)

dir, err := os.Open(abspath)
if err != nil {
Expand All @@ -119,8 +117,6 @@ func getDirectoryModel(relativePath string) models.ContentModel {
}

func getFileModel(abspath, relativePath, fileName string) models.ContentModel {
// log.Info().Msgf("abs path %s", abspath)
// log.Info().Msgf("relative path %s", relativePath)

os_path := filepath.Join(abspath, fileName)

Expand Down Expand Up @@ -175,8 +171,8 @@ func getFileModelWithContent(path string) models.ContentModel {

func read_file2(path string, fileName string) string {
extension := filepath.Ext(fileName)
log.Info().Msgf("reading path extension: %s", extension)
log.Info().Msgf("reading path: %s", path)
log.Debug().Msgf("reading path extension: %s", extension)
log.Debug().Msgf("reading path: %s", path)
file, err := os.ReadFile(path)
if err != nil {
panic(err)
Expand All @@ -188,7 +184,7 @@ func read_file2(path string, fileName string) string {
}

func read_file(path string) string {
log.Info().Msgf("reading path: %s", path)
log.Debug().Msgf("reading path: %s", path)
file, err := os.ReadFile(path)
if err != nil {
panic(err)
Expand Down
22 changes: 9 additions & 13 deletions kernel/kernel_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (km *KernelManager) asyncPrestartKernel(kernelName string) ([]string, map[s

kw := km.preLaunch()
kernelCmd := kw["cmd"].([]string)
log.Info().Msgf("kenelName: %s", kernelName)
log.Debug().Msgf("kenelName: %s", kernelName)
return kernelCmd, kw
}

Expand All @@ -92,37 +92,33 @@ func isLocalIP(ip string) bool {
*********************************************************************/

func (km *KernelManager) asyncLaunchKernel(kernelCmd []string, kw map[string]interface{}) {
// log.Info().Msgf("kw : %s", kw)
// log.Info().Msgf("cmd : %s", kernelCmd)

ConnectionInfo := km.Provisioner.LaunchKernel(kernelCmd, kw, km.ConnectionFile)
log.Info().Msgf("connectionInfo: %s", ConnectionInfo)
log.Debug().Msgf("connectionInfo: %s", ConnectionInfo)
}

func (km *KernelManager) preLaunch() map[string]interface{} {

if km.ConnectionInfo.Transport == "tcp" && !isLocalIP(km.ConnectionInfo.IP) {
log.Info().Msg("Can only launch a kernel on a local interface.")
log.Debug().Msg("Can only launch a kernel on a local interface.")
}
// log.Info().Msgf("cache ports: %t", km.CachePorts)
// log.Info().Msgf("km.Provisioner.PortsCached %t", km.Provisioner.PortsCached)
log.Debug().Msgf("cache ports: %t", km.CachePorts)
log.Debug().Msgf("km.Provisioner.PortsCached %t", km.Provisioner.PortsCached)

if km.CachePorts && !km.Provisioner.PortsCached {
km.ConnectionInfo.ShellPort, _ = findAvailablePort()
km.ConnectionInfo.IopubPort, _ = findAvailablePort()
km.ConnectionInfo.StdinPort, _ = findAvailablePort()
km.ConnectionInfo.HbPort, _ = findAvailablePort()
km.ConnectionInfo.ControlPort, _ = findAvailablePort()
log.Info().Msgf("connectionInfo : %+v", km.ConnectionInfo)
log.Debug().Msgf("connectionInfo : %+v", km.ConnectionInfo)
}
log.Info().Msgf("km.ConnectionFile : %+v", km.ConnectionFile)
log.Debug().Msgf("km.ConnectionFile : %+v", km.ConnectionFile)

km.writeConnectionFile(km.ConnectionFile)
// km.writeConnectionFile("kernelConnection.json")

kernelCmd := km.formatKernelCmd()
log.Info().Msgf("kernel cmd is %s", kernelCmd)
// the following one is from super
log.Debug().Msgf("kernel cmd is %s", kernelCmd)

env := make(map[string]interface{})
env["cmd"] = kernelCmd
env["env"] = os.Environ()
Expand Down
22 changes: 8 additions & 14 deletions kernel/kernel_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,20 @@ func (ks *KernelSession) Send(
metadata map[string]interface{},
) Message {

log.Info().Msg("================sending the message===============")

var msg Message
switch v := msgOrType.(type) {
case Message:
// log.Print("received Message type")
msg = v
if buffers == nil {
// fill the buffer with msg buffers
buffers = msg.Buffers
}
case string:
// kernel info request goes
// log.Print("received String type")
msg = ks.createMsg(v, content, parent, header, metadata)
default:
log.Info().Msgf("msg_or_type must be of type Message or string, got %T", v)
log.Debug().Msgf("msg_or_type must be of type Message or string, got %T", v)
}

log.Info().Msgf("message is %+v", msg)
log.Debug().Msgf("message is %+v", msg)

if ks.CheckPid && os.Getpid() != ks.Pid {
log.Info().Msgf("WARNING: attempted to send message from fork %+v", msg)
Expand Down Expand Up @@ -161,9 +155,9 @@ func (ks *KernelSession) Send(
tracker, _ = stream.SendMessage(toSend)

if ks.Debug {
log.Info().Msgf("Message: %s\n", msg.MsgId)
log.Info().Msgf("ToSend: %s\n", toSend)
log.Info().Msgf("Buffers: %s\n", buffers)
log.Debug().Msgf("Message: %s\n", msg.MsgId)
log.Debug().Msgf("ToSend: %s\n", toSend)
log.Debug().Msgf("Buffers: %s\n", buffers)
}

msg.Tracker = tracker
Expand All @@ -189,12 +183,12 @@ func (ks *KernelSession) serialize(msg Message, ident [][]byte) [][]byte {
json_packer(msg.Content), // []byte("kernel_info_request"),
}
to_send := [][]byte{}
// log.Info().Msgf("real message is %s", realMessage)
log.Debug().Msgf("real message is %s", realMessage)
signature := ks.sign(realMessage)
to_send = append(to_send, []byte(DELIM))
to_send = append(to_send, []byte(signature))
to_send = append(to_send, realMessage...)
// log.Info().Msgf("after signing message is %s", realMessage)
log.Debug().Msgf("after signing message is %s", realMessage)
return to_send
}

Expand Down Expand Up @@ -268,7 +262,7 @@ func (ks *KernelSession) deserialize(
// message.Buffers = msgList[5:]

// Debug print
fmt.Printf("Message: %+v\n", message)
log.Debug().Msgf("Message: %+v\n", message)

// Adapt to the current version (implement as needed)
// message = adapt(message)
Expand Down
7 changes: 4 additions & 3 deletions kernel/kernel_supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kernel

import (
"fmt"
"log"
"os"
"path/filepath"
"sync"
Expand All @@ -12,6 +11,8 @@ import (
"github.com/zasper-io/zasper/models"

"github.com/google/uuid"

"github.com/rs/zerolog/log"
)

var ZasperPendingKernels map[string]KernelManager
Expand Down Expand Up @@ -99,7 +100,7 @@ func StartKernelManager(kernelPath string, kernelName string, env map[string]str
kernelId := uuid.New().String()

km, kernel_name, kernel_id := createKernelManager(kernelName, kernelId)
log.Println(km, kernel_name, kernel_id)
log.Debug().Msgf("%v | %v | %v ", km, kernel_name, kernel_id)

km.StartKernel(kernelName)

Expand Down Expand Up @@ -130,7 +131,7 @@ func createKernelManager(kernelName string, kernelId string) (KernelManager, str
km.ConnectionInfo.Transport = "tcp"
km.ConnectionInfo.IP = "127.0.0.1"
km.Session = getSession()
log.Println("session is", km.Session)
log.Info().Msgf("session is %v", km.Session)
return km, kernelName, kernelId
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ func LaunchKernel(kernelCmd []string, kw map[string]interface{}, connFile string
}
}()

log.Info().Msg("Process started successfully")
log.Debug().Msg("Process started successfully")
return cmd.Process
}
2 changes: 1 addition & 1 deletion kernel/provisioner/local_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ type LocalProvisioner struct {
func (provisioner *LocalProvisioner) LaunchKernel(kernelCmd []string, kw map[string]interface{}, connFile string) KernelConnectionInfo {
process := launcher.LaunchKernel(kernelCmd, kw, connFile)
provisioner.Pid = process.Pid
log.Info().Msgf("kernel launched with pid: %d", process.Pid)
log.Debug().Msgf("kernel launched with pid: %d", process.Pid)
return provisioner.ConnectionInfo
}
2 changes: 1 addition & 1 deletion kernel/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func findAvailablePort() (int, error) {
if portExists(port) {
continue
}
// log.Info().Msgf("check port %d", port)
log.Debug().Msgf("check port %d", port)
l, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if err == nil {
currentlyUsedPorts = append(currentlyUsedPorts, port)
Expand Down
11 changes: 6 additions & 5 deletions kernelspec/kernelspec_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package kernelspec
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/zasper-io/zasper/core"

"github.com/rs/zerolog/log"
)

type KernelspecResponse struct {
Expand Down Expand Up @@ -99,14 +100,14 @@ func fromResourceDir(resourceDir string) KernelSpecJsonData {
*/

kernelFile := filepath.Join(resourceDir, "kernel.json")
log.Println("loading file", kernelFile)
log.Info().Msgf("loading file %s", kernelFile)
byteValue, _ := os.ReadFile(kernelFile)

var kernelSpecJsonData KernelSpecJsonData

err := json.Unmarshal(byteValue, &kernelSpecJsonData)
if err != nil {
log.Println("error encountered")
log.Info().Msg("error encountered")
}
log.Print(kernelSpecJsonData)
kernelSpecJsonData.ResourceDir = resourceDir
Expand Down Expand Up @@ -192,10 +193,10 @@ func getKernelDirs() []string {
func listKernelsIn(kernelDir string) map[string]string {
dir, err := os.Open(kernelDir)
if err != nil {
log.Println("No kernels found in", kernelDir)
log.Debug().Msgf("No kernels found in %s", kernelDir)
return nil
}
log.Println("kernels found in", kernelDir)
log.Info().Msgf("kernels found in %s", kernelDir)
files, err := dir.Readdir(0)
if err != nil {
fmt.Println(err)
Expand Down
Loading

0 comments on commit b941ea4

Please sign in to comment.