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

     1  package oauth
     2  
     3  import (
     4  	"runtime"
     5  	"time"
     6  
     7  	"github.com/cozy/cozy-stack/model/job"
     8  	"github.com/cozy/cozy-stack/model/oauth"
     9  	"github.com/cozy/cozy-stack/pkg/couchdb"
    10  )
    11  
    12  func init() {
    13  	job.AddWorker(&job.WorkerConfig{
    14  		WorkerType:   "clean-clients",
    15  		Concurrency:  runtime.NumCPU(),
    16  		MaxExecCount: 2,
    17  		Reserved:     true,
    18  		Timeout:      30 * time.Second,
    19  		WorkerFunc:   WorkerClean,
    20  	})
    21  }
    22  
    23  // WorkerClean is used to clean unused OAuth clients.
    24  func WorkerClean(ctx *job.TaskContext) error {
    25  	var msg oauth.CleanMessage
    26  	if err := ctx.UnmarshalMessage(&msg); err != nil {
    27  		return err
    28  	}
    29  	client, err := oauth.FindClient(ctx.Instance, msg.ClientID)
    30  	if err != nil {
    31  		if couchdb.IsNotFoundError(err) {
    32  			return nil
    33  		}
    34  		return err
    35  	}
    36  	if client.Pending {
    37  		return couchdb.DeleteDoc(ctx.Instance, client)
    38  	}
    39  	return nil
    40  }