github.com/goravel/framework@v1.13.9/queue/utils.go (about)

     1  package queue
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/goravel/framework/contracts/event"
     8  	"github.com/goravel/framework/contracts/queue"
     9  )
    10  
    11  func jobs2Tasks(jobs []queue.Job) (map[string]any, error) {
    12  	tasks := make(map[string]any)
    13  
    14  	for _, job := range jobs {
    15  		if job.Signature() == "" {
    16  			return nil, errors.New("the Signature of job can't be empty")
    17  		}
    18  
    19  		if tasks[job.Signature()] != nil {
    20  			return nil, fmt.Errorf("job signature duplicate: %s, the names of Job and Listener cannot be duplicated", job.Signature())
    21  		}
    22  
    23  		tasks[job.Signature()] = job.Handle
    24  	}
    25  
    26  	return tasks, nil
    27  }
    28  
    29  func eventsToTasks(events map[event.Event][]event.Listener) (map[string]any, error) {
    30  	tasks := make(map[string]any)
    31  
    32  	for _, listeners := range events {
    33  		for _, listener := range listeners {
    34  			if listener.Signature() == "" {
    35  				return nil, errors.New("the Signature of listener can't be empty")
    36  			}
    37  
    38  			if tasks[listener.Signature()] != nil {
    39  				continue
    40  			}
    41  
    42  			tasks[listener.Signature()] = listener.Handle
    43  		}
    44  	}
    45  
    46  	return tasks, nil
    47  }