Skip to content

Commit

Permalink
Improve live reload
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Aug 19, 2023
1 parent 218db7c commit 63cf560
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 8 additions & 1 deletion cmd-rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func newCmd_rpc() *cli.Command {
defer cancel()

err = onFileChanged(ctx, dirs, func(event fsnotify.Event) {
if !isJSONFile(event.Name) && !isYAMLFile(event.Name) {
klog.Infof("File %q is not a JSON or YAML file; do nothing", event.Name)
return
}
klog.Infof("File event: %s", spew.Sdump(event))

if event.Op != fsnotify.Remove && multi.HasEpochWithSameHashAsFile(event.Name) {
Expand Down Expand Up @@ -173,6 +177,7 @@ func newCmd_rpc() *cli.Command {
klog.Errorf("error replacing epoch %d: %s", epoch.Epoch(), err.Error())
return
}
klog.Infof("Epoch %d replaced", epoch.Epoch())
}
case fsnotify.Create:
{
Expand All @@ -193,15 +198,17 @@ func newCmd_rpc() *cli.Command {
klog.Errorf("error adding epoch %d: %s", epoch.Epoch(), err.Error())
return
}
klog.Infof("Epoch %d added", epoch.Epoch())
}
case fsnotify.Remove:
{
klog.Infof("File %q was removed", event.Name)
// find the epoch that corresponds to this file, and remove it (if any)
err := multi.RemoveEpochByConfigFilepath(event.Name)
epNumber, err := multi.RemoveEpochByConfigFilepath(event.Name)
if err != nil {
klog.Errorf("error removing epoch for config file %q: %s", event.Name, err.Error())
}
klog.Infof("Epoch %d removed", epNumber)
}
case fsnotify.Rename:
klog.Infof("File %q was renamed; do nothing", event.Name)
Expand Down
6 changes: 3 additions & 3 deletions multiepoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,16 @@ func (m *MultiEpoch) RemoveEpoch(epoch uint64) error {
return nil
}

func (m *MultiEpoch) RemoveEpochByConfigFilepath(configFilepath string) error {
func (m *MultiEpoch) RemoveEpochByConfigFilepath(configFilepath string) (uint64, error) {
m.mu.Lock()
defer m.mu.Unlock()
for epoch, ep := range m.epochs {
if ep.config.ConfigFilepath() == configFilepath {
delete(m.epochs, epoch)
return nil
return epoch, nil
}
}
return fmt.Errorf("epoch not found for config file %q", configFilepath)
return 0, fmt.Errorf("epoch not found for config file %q", configFilepath)
}

func (m *MultiEpoch) ReplaceEpoch(epoch uint64, ep *Epoch) error {
Expand Down

0 comments on commit 63cf560

Please sign in to comment.