Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cleanup jobs parameter #212

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
)

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 @@
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 @@
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 @@
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 @@
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 @@
}

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 @@
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 @@
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