Skip to content

Commit

Permalink
feat: recorder.writeToDevNull option to write files to /dev/null (tes…
Browse files Browse the repository at this point in the history
…ting)

Useful for dry run scenarios where we just want to test for crashes in field trials
  • Loading branch information
prlanzarin committed Dec 19, 2023
1 parent c310584 commit c3b506b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ recorder:
dirFileMode: 0700
# File mode permissions for the recording files (octal)
fileMode: 0700
# Whether to write to /dev/null instead of a file (for testing)
writeToDevNull: false

pubsub:
channels:
Expand Down
1 change: 1 addition & 0 deletions config/bbb-webrtc-recorder.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ recorder:
directory: /var/lib/bbb-webrtc-recorder
dirFileMode: 0700
fileMode: 0600
writeToDevNull: false

pubsub:
channels:
Expand Down
8 changes: 5 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (cfg *Config) SetDefaults() {

cfg.Recorder.DirFileMode = "0700"
cfg.Recorder.FileMode = "0600"
cfg.Recorder.WriteToDevNull = false
cfg.PubSub.Channels = Channels{
Subscribe: "to-" + cfg.App.Name,
Publish: "from-" + cfg.App.Name,
Expand All @@ -67,9 +68,10 @@ func (cfg *Config) SetDefaults() {
}

type Recorder struct {
Directory string `yaml:"directory,omitempty"`
DirFileMode string `yaml:"dirFileMode,omitempty"`
FileMode string `yaml:"fileMode,omitempty"`
Directory string `yaml:"directory,omitempty"`
DirFileMode string `yaml:"dirFileMode,omitempty"`
FileMode string `yaml:"fileMode,omitempty"`
WriteToDevNull bool `yaml:"writeToDevNull,omitempty"`
}

type Redis struct {
Expand Down
52 changes: 30 additions & 22 deletions internal/webrtc/recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,43 @@ func NewRecorder(ctx context.Context, cfg config.Recorder, file string) (Recorde
return nil, fmt.Errorf("directory does not exist %s", cfg.Directory)
}

file = path.Clean(dir + string(os.PathSeparator) + file)
fileDir := path.Dir(file)
// If we are not writing to /dev/null, we need to check if the file exists
// and if the directory is writable.
if !cfg.WriteToDevNull {
file = path.Clean(dir + string(os.PathSeparator) + file)
fileDir := path.Dir(file)

if stat, err := os.Stat(fileDir); err != nil {
log.WithField("session", ctx.Value("session")).
Debug(stat)
if stat, err := os.Stat(fileDir); err != nil {
log.WithField("session", ctx.Value("session")).
Debug(stat)

if !os.IsNotExist(err) {
return nil, fmt.Errorf("file directory is not accessible %s", fileDir)
}
if !os.IsNotExist(err) {
return nil, fmt.Errorf("file directory is not accessible %s", fileDir)
}

var dirFileMode os.FileMode
if parsedFileMode, err := strconv.ParseUint(cfg.DirFileMode, 0, 32); err != nil {
return nil, fmt.Errorf("invalid file mode %s", cfg.DirFileMode)
} else {
dirFileMode = os.FileMode(parsedFileMode)
}
var dirFileMode os.FileMode
if parsedFileMode, err := strconv.ParseUint(cfg.DirFileMode, 0, 32); err != nil {
return nil, fmt.Errorf("invalid file mode %s", cfg.DirFileMode)
} else {
dirFileMode = os.FileMode(parsedFileMode)
}

err = os.MkdirAll(fileDir, dirFileMode)
err = os.MkdirAll(fileDir, dirFileMode)

if err != nil && !os.IsExist(err) {
return nil, fmt.Errorf("file directory could not be created %s", fileDir)
if err != nil && !os.IsExist(err) {
return nil, fmt.Errorf("file directory could not be created %s", fileDir)
}
}
}

if stat, err := os.Stat(file); !os.IsNotExist(err) {
log.WithField("session", ctx.Value("session")).
Debug(stat)
return nil, fmt.Errorf("file already exists %s", file)
if stat, err := os.Stat(file); !os.IsNotExist(err) {
log.WithField("session", ctx.Value("session")).
Debug(stat)
return nil, fmt.Errorf("file already exists %s", file)
}
} else {
// We're writing to /dev/null - see the recorder.writeToDevNull config
// (for testing purposes)
file = os.DevNull
}

var fileMode os.FileMode
Expand Down

0 comments on commit c3b506b

Please sign in to comment.