github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/daemon/cluster/tasks.go (about)

     1  package cluster // import "github.com/demonoid81/moby/daemon/cluster"
     2  
     3  import (
     4  	"context"
     5  
     6  	apitypes "github.com/demonoid81/moby/api/types"
     7  	"github.com/demonoid81/moby/api/types/filters"
     8  	types "github.com/demonoid81/moby/api/types/swarm"
     9  	"github.com/demonoid81/moby/daemon/cluster/convert"
    10  	swarmapi "github.com/docker/swarmkit/api"
    11  	"google.golang.org/grpc"
    12  )
    13  
    14  // GetTasks returns a list of tasks matching the filter options.
    15  func (c *Cluster) GetTasks(options apitypes.TaskListOptions) ([]types.Task, error) {
    16  	var r *swarmapi.ListTasksResponse
    17  
    18  	if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
    19  		filterTransform := func(filter filters.Args) error {
    20  			if filter.Contains("service") {
    21  				serviceFilters := filter.Get("service")
    22  				for _, serviceFilter := range serviceFilters {
    23  					service, err := getService(ctx, state.controlClient, serviceFilter, false)
    24  					if err != nil {
    25  						return err
    26  					}
    27  					filter.Del("service", serviceFilter)
    28  					filter.Add("service", service.ID)
    29  				}
    30  			}
    31  			if filter.Contains("node") {
    32  				nodeFilters := filter.Get("node")
    33  				for _, nodeFilter := range nodeFilters {
    34  					node, err := getNode(ctx, state.controlClient, nodeFilter)
    35  					if err != nil {
    36  						return err
    37  					}
    38  					filter.Del("node", nodeFilter)
    39  					filter.Add("node", node.ID)
    40  				}
    41  			}
    42  			if !filter.Contains("runtime") {
    43  				// default to only showing container tasks
    44  				filter.Add("runtime", "container")
    45  				filter.Add("runtime", "")
    46  			}
    47  			return nil
    48  		}
    49  
    50  		filters, err := newListTasksFilters(options.Filters, filterTransform)
    51  		if err != nil {
    52  			return err
    53  		}
    54  
    55  		r, err = state.controlClient.ListTasks(
    56  			ctx,
    57  			&swarmapi.ListTasksRequest{Filters: filters},
    58  			grpc.MaxCallRecvMsgSize(defaultRecvSizeForListResponse),
    59  		)
    60  		return err
    61  	}); err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	tasks := make([]types.Task, 0, len(r.Tasks))
    66  	for _, task := range r.Tasks {
    67  		t, err := convert.TaskFromGRPC(*task)
    68  		if err != nil {
    69  			return nil, err
    70  		}
    71  		tasks = append(tasks, t)
    72  	}
    73  	return tasks, nil
    74  }
    75  
    76  // GetTask returns a task by an ID.
    77  func (c *Cluster) GetTask(input string) (types.Task, error) {
    78  	var task *swarmapi.Task
    79  	if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
    80  		t, err := getTask(ctx, state.controlClient, input)
    81  		if err != nil {
    82  			return err
    83  		}
    84  		task = t
    85  		return nil
    86  	}); err != nil {
    87  		return types.Task{}, err
    88  	}
    89  	return convert.TaskFromGRPC(*task)
    90  }