github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/worker/notes/notes.go (about)

     1  package notes
     2  
     3  import (
     4  	"runtime"
     5  	"time"
     6  
     7  	"github.com/cozy/cozy-stack/model/job"
     8  	"github.com/cozy/cozy-stack/model/note"
     9  )
    10  
    11  func init() {
    12  	job.AddWorker(&job.WorkerConfig{
    13  		WorkerType:   "notes-save",
    14  		Concurrency:  runtime.NumCPU(),
    15  		MaxExecCount: 2,
    16  		Reserved:     true,
    17  		Timeout:      30 * time.Second,
    18  		WorkerFunc:   WorkerPersist,
    19  	})
    20  }
    21  
    22  // WorkerPersist is used to persist a note to its file in the VFS. The changes
    23  // (title and steps) on a notes can happen with a high frequency, and
    24  // debouncing them allows to not make too many calls to Swift.
    25  func WorkerPersist(ctx *job.TaskContext) error {
    26  	var msg note.DebounceMessage
    27  	if err := ctx.UnmarshalMessage(&msg); err != nil {
    28  		return err
    29  	}
    30  	log := ctx.Instance.Logger().WithNamespace("notes")
    31  	log.Debugf("Persist %#v", msg)
    32  	err := note.Update(ctx.Instance, msg.NoteID)
    33  	if err != nil {
    34  		log.Warnf("Cannot persist note %s: %s", msg.NoteID, err)
    35  	}
    36  	return err
    37  }