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

     1  // Package share is where the workers for Cozy to Cozy sharings are defined.
     2  package share
     3  
     4  import (
     5  	"runtime"
     6  	"time"
     7  
     8  	"github.com/cozy/cozy-stack/model/job"
     9  	"github.com/cozy/cozy-stack/model/sharing"
    10  )
    11  
    12  func init() {
    13  	job.AddWorker(&job.WorkerConfig{
    14  		WorkerType:   "share-group",
    15  		Concurrency:  runtime.NumCPU(),
    16  		MaxExecCount: 2,
    17  		Reserved:     true,
    18  		Timeout:      30 * time.Second,
    19  		WorkerFunc:   WorkerGroup,
    20  	})
    21  
    22  	job.AddWorker(&job.WorkerConfig{
    23  		WorkerType:   "share-track",
    24  		Concurrency:  runtime.NumCPU(),
    25  		MaxExecCount: 2,
    26  		Reserved:     true,
    27  		Timeout:      30 * time.Second,
    28  		WorkerFunc:   WorkerTrack,
    29  	})
    30  
    31  	job.AddWorker(&job.WorkerConfig{
    32  		WorkerType:  "share-replicate",
    33  		Concurrency: runtime.NumCPU(),
    34  		// XXX the worker is not idempotent: if it fails, it adds a new job to
    35  		// retry, but with MaxExecCount > 1, it can amplifies a lot the number
    36  		// of retries
    37  		MaxExecCount: 1,
    38  		Reserved:     true,
    39  		Timeout:      5 * time.Minute,
    40  		WorkerFunc:   WorkerReplicate,
    41  	})
    42  
    43  	job.AddWorker(&job.WorkerConfig{
    44  		WorkerType:  "share-upload",
    45  		Concurrency: runtime.NumCPU(),
    46  		// XXX the worker is not idempotent: if it fails, it adds a new job to
    47  		// retry, but with MaxExecCount > 1, it can amplifies a lot the number
    48  		// of retries
    49  		MaxExecCount: 1,
    50  		Reserved:     true,
    51  		Timeout:      1 * time.Hour,
    52  		WorkerFunc:   WorkerUpload,
    53  	})
    54  }
    55  
    56  // WorkerGroup is used to update the list of members of sharings for a group
    57  // when someone is added or removed to this group.
    58  func WorkerGroup(ctx *job.TaskContext) error {
    59  	var msg job.ShareGroupMessage
    60  	if err := ctx.UnmarshalMessage(&msg); err != nil {
    61  		return err
    62  	}
    63  	ctx.Instance.Logger().WithNamespace("share").
    64  		Debugf("Group %#v", msg)
    65  	return sharing.UpdateGroups(ctx.Instance, msg)
    66  }
    67  
    68  // WorkerTrack is used to update the io.cozy.shared database when a document
    69  // that matches a sharing rule is created/updated/remove
    70  func WorkerTrack(ctx *job.TaskContext) error {
    71  	var msg sharing.TrackMessage
    72  	if err := ctx.UnmarshalMessage(&msg); err != nil {
    73  		return err
    74  	}
    75  	var evt sharing.TrackEvent
    76  	if err := ctx.UnmarshalEvent(&evt); err != nil {
    77  		return err
    78  	}
    79  	ctx.Instance.Logger().WithNamespace("share").
    80  		Debugf("Track %#v - %#v", msg, evt)
    81  	return sharing.UpdateShared(ctx.Instance, msg, evt)
    82  }
    83  
    84  // WorkerReplicate is used for the replication of documents to the other
    85  // members of a sharing.
    86  func WorkerReplicate(ctx *job.TaskContext) error {
    87  	var msg sharing.ReplicateMsg
    88  	if err := ctx.UnmarshalMessage(&msg); err != nil {
    89  		return err
    90  	}
    91  	ctx.Instance.Logger().WithNamespace("share").
    92  		Debugf("Replicate %#v", msg)
    93  	s, err := sharing.FindSharing(ctx.Instance, msg.SharingID)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	if !s.Active {
    98  		return nil
    99  	}
   100  	return s.Replicate(ctx.Instance, msg.Errors)
   101  }
   102  
   103  // WorkerUpload is used to upload files for a sharing
   104  func WorkerUpload(ctx *job.TaskContext) error {
   105  	var msg sharing.UploadMsg
   106  	if err := ctx.UnmarshalMessage(&msg); err != nil {
   107  		return err
   108  	}
   109  	ctx.Instance.Logger().WithNamespace("share").
   110  		Debugf("Upload %#v", msg)
   111  	s, err := sharing.FindSharing(ctx.Instance, msg.SharingID)
   112  	if err != nil {
   113  		return err
   114  	}
   115  	if !s.Active {
   116  		return nil
   117  	}
   118  	return s.Upload(ctx.Instance, ctx.Context, msg.Errors)
   119  }