Skip to content

Commit

Permalink
feat: add cleanup jobs parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurcgc committed Sep 20, 2023
1 parent adfcea3 commit 6c32e5f
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions tsuru/client/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ import (
)

type JobCreate struct {
schedule string
teamOwner string
plan string
pool string
description string
manual bool
tags cmd.StringSliceFlag
schedule string
teamOwner string
plan string
pool string
description string
manual bool
tags cmd.StringSliceFlag
activeDeadline int64

fs *gnuflag.FlagSet
}
Expand Down Expand Up @@ -73,6 +74,8 @@ description associated
The [[--manual]] parameter sets your job as a manual job.
A manual job is only run when explicitly triggered by the user i.e: tsuru job trigger <job-name>
The [[--active-deadline]] parameter determines the limit of time, in seconds, that the job is allowed to run (default = 1h). After the determined time, the Job will be terminated and marked as failed.
The [[--tag]] parameter sets a tag to your job. You can set multiple [[--tag]] parameters`,
MinArgs: 2,
}
Expand Down Expand Up @@ -101,6 +104,7 @@ func (c *JobCreate) Flags() *gnuflag.FlagSet {
c.fs.Var(&c.tags, "g", tagMessage)
manualMessage := "Manual job"
c.fs.BoolVar(&c.manual, "manual", false, manualMessage)
c.fs.Int64Var(&c.activeDeadline, "active-deadline", 0, "active deadline seconds")
}
return c.fs
}
Expand All @@ -121,18 +125,25 @@ func parseJobCommands(commands []string) ([]string, error) {
return shellCommands, nil
}

func validateFlags(c *JobCreate) error {
if !c.manual && c.schedule == "" {
return errors.New("schedule or manual option must be set")
}
if c.manual && c.schedule != "" {
return errors.New("cannot set both manual job and schedule options")
}
return nil
}

func (c *JobCreate) Run(ctx *cmd.Context, cli *cmd.Client) error {
apiClient, err := client.ClientFromEnvironment(&tsuru.Configuration{
HTTPClient: cli.HTTPClient,
})
if err != nil {
return err
}
if !c.manual && c.schedule == "" {
return errors.New("schedule or manual option must be set")
}
if c.manual && c.schedule != "" {
return errors.New("cannot set both manual job and schedule options")
if err := validateFlags(c); err != nil {
return err
}
jobName := ctx.Args[0]
image := ctx.Args[1]
Expand All @@ -154,6 +165,7 @@ func (c *JobCreate) Run(ctx *cmd.Context, cli *cmd.Client) error {
Image: image,
Command: parsedCommands,
},
ActiveDeadlineSeconds: c.activeDeadline,

Check failure on line 168 in tsuru/client/jobs.go

View workflow job for this annotation

GitHub Actions / lint

unknown field 'ActiveDeadlineSeconds' in struct literal of type tsuru.InputJob

Check failure on line 168 in tsuru/client/jobs.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

unknown field 'ActiveDeadlineSeconds' in struct literal of type tsuru.InputJob

Check failure on line 168 in tsuru/client/jobs.go

View workflow job for this annotation

GitHub Actions / test (macOS-latest)

unknown field 'ActiveDeadlineSeconds' in struct literal of type tsuru.InputJob

Check failure on line 168 in tsuru/client/jobs.go

View workflow job for this annotation

GitHub Actions / lint

unknown field 'ActiveDeadlineSeconds' in struct literal of type tsuru.InputJob

Check failure on line 168 in tsuru/client/jobs.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

unknown field 'ActiveDeadlineSeconds' in struct literal of type tsuru.InputJob
}
if _, err := apiClient.JobApi.CreateJob(context.Background(), j); err != nil {
return err
Expand Down Expand Up @@ -455,21 +467,22 @@ func (c *JobTrigger) Run(ctx *cmd.Context, cli *cmd.Client) error {
}

type JobUpdate struct {
schedule string
teamOwner string
plan string
pool string
description string
image string
tags cmd.StringSliceFlag
schedule string
teamOwner string
plan string
pool string
description string
image string
tags cmd.StringSliceFlag
activeDeadline int64

fs *gnuflag.FlagSet
}

func (c *JobUpdate) Info() *cmd.Info {
return &cmd.Info{
Name: "job-update",
Usage: "job update <job-name> [--image/-i <image>] [--plan/-p plan name] [--schedule/-s schedule name] [--team/-t team owner] [--pool/-o pool name] [--description/-d description] [--tag/-g tag]... -- [commands]",
Usage: "job update <job-name> [--image/-i <image>] [--plan/-p plan name] [--schedule/-s schedule name] [--team/-t team owner] [--pool/-o pool name] [--description/-d description] [--tag/-g tag] [[--active-deadline deadline]]... -- [commands] ",
Desc: "Updates a job",
MinArgs: 1,
}
Expand Down Expand Up @@ -499,6 +512,7 @@ func (c *JobUpdate) Flags() *gnuflag.FlagSet {
imageMessage := "New image for the job to run"
c.fs.StringVar(&c.image, "image", "", imageMessage)
c.fs.StringVar(&c.image, "i", "", imageMessage)
c.fs.Int64Var(&c.activeDeadline, "active-deadline", 0, "active deadline seconds")
}
return c.fs
}
Expand Down Expand Up @@ -530,6 +544,7 @@ func (c *JobUpdate) Run(ctx *cmd.Context, cli *cmd.Client) error {
Image: c.image,
Command: jobUpdateCommands,
},
ActiveDeadlineSeconds: c.activeDeadline,

Check failure on line 547 in tsuru/client/jobs.go

View workflow job for this annotation

GitHub Actions / lint

unknown field 'ActiveDeadlineSeconds' in struct literal of type tsuru.InputJob

Check failure on line 547 in tsuru/client/jobs.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

unknown field 'ActiveDeadlineSeconds' in struct literal of type tsuru.InputJob

Check failure on line 547 in tsuru/client/jobs.go

View workflow job for this annotation

GitHub Actions / test (macOS-latest)

unknown field 'ActiveDeadlineSeconds' in struct literal of type tsuru.InputJob

Check failure on line 547 in tsuru/client/jobs.go

View workflow job for this annotation

GitHub Actions / lint

unknown field 'ActiveDeadlineSeconds' in struct literal of type tsuru.InputJob

Check failure on line 547 in tsuru/client/jobs.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

unknown field 'ActiveDeadlineSeconds' in struct literal of type tsuru.InputJob
}

_, err = apiClient.JobApi.UpdateJob(context.Background(), jobName, j)
Expand Down

0 comments on commit 6c32e5f

Please sign in to comment.