Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cmd/scheduler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import "github.com/spf13/cobra"

var (
schedulerSchedulerNamespace string
schedulerNamespace string
)

var SchedulerCmd = &cobra.Command{
Use: "scheduler",
Short: "Scheduler management commands",
}

func init() {
RootCmd.AddCommand(SchedulerCmd)
SchedulerCmd.PersistentFlags().BoolVarP(&kubernetesMode, "kubernetes", "k", false, "List all Dapr pods in a Kubernetes cluster")
SchedulerCmd.PersistentFlags().StringVarP(&schedulerNamespace, "namespace", "n", "", "Kubernetes namespace to list Dapr apps from. If not specified, uses all namespaces")
SchedulerCmd.PersistentFlags().StringVar(&schedulerSchedulerNamespace, "scheduler-namespace", "dapr-system", "Kubernetes namespace where the scheduler is deployed")
}
85 changes: 85 additions & 0 deletions cmd/schedulerdelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"errors"
"fmt"
"os"

"github.com/dapr/cli/pkg/print"
"github.com/dapr/cli/pkg/scheduler"
"github.com/dapr/kit/signals"
"github.com/spf13/cobra"
)

var (
schedulerDeleteAll bool
)

var SchedulerDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete jobs or actor reminders which are scheduled in Scheduler.",
Long: `Delete jobs or actor reminders which are scheduled in Scheduler.
Namespace (-n) is required.
Job names are formatted by their type, app ID, then identifier.
Actor reminders require the actor type, actor ID, then reminder name, separated by ||.
Accepts multiple job names or actor reminders to delete.

dapr scheduler delete -n foo job/my-app-id/my-job-name
dapr scheduler delete -n foo "actorreminder/my-actor-type||my-actor-id||my-reminder-name"
`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := signals.Context()

if !cmd.Flag("namespace").Changed {
return errors.New(`required flag(s) "--namespace" not set`)
}

if schedulerDeleteAll {
if err := scheduler.DeleteAll(ctx, scheduler.DeleteOptions{
SchedulerNamespace: schedulerSchedulerNamespace,
DaprNamespace: schedulerNamespace,
KubernetesMode: kubernetesMode,
}); err != nil {
return fmt.Errorf("Failed to delete jobs: %s", err)
}
return nil
}

if len(args) < 1 {
return errors.New(`Qualifier and job name are required.
Example: dapr scheduler delete -n foo job/my-app-id/my-job-name
Example: dapr scheduler delete -n foo "actorreminder/my-actor-type||my-actor-id||my-reminder-name"`)
}

for _, name := range args {
if err := scheduler.Delete(ctx, name, scheduler.DeleteOptions{
SchedulerNamespace: schedulerSchedulerNamespace,
DaprNamespace: schedulerNamespace,
KubernetesMode: kubernetesMode,
}); err != nil {
return fmt.Errorf("Failed to delete job: %s", err)
}
print.InfoStatusEvent(os.Stdout, "Deleted job '%s' in namespace '%s'.", name, schedulerNamespace)
}

return nil
},
}

func init() {
SchedulerDeleteCmd.Flags().BoolVar(&schedulerDeleteAll, "delete-all-yes-i-know-what-i-am-doing", false, "Deletes all jobs and actor reminders in the given namespace.")
SchedulerCmd.AddCommand(SchedulerDeleteCmd)
}
60 changes: 60 additions & 0 deletions cmd/schedulerexport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"os"

"github.com/spf13/cobra"

"github.com/dapr/cli/pkg/print"
"github.com/dapr/cli/pkg/scheduler"
"github.com/dapr/kit/signals"
)

var (
schedulerExportFile string
)

var SchedulerExportCmd = &cobra.Command{
Use: "export",
Short: "Export all jobs and actor reminders to a binary file, including the tracked count.",
Long: `Export jobs and actor reminders which are scheduled in Scheduler.
Can later be imported using 'dapr scheduler import'.
dapr scheduler export -o output.bin
`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := signals.Context()

err := scheduler.Export(ctx, scheduler.ExportImportOptions{
SchedulerNamespace: schedulerSchedulerNamespace,
KubernetesMode: kubernetesMode,
TargetFile: schedulerExportFile,
})
if err != nil {
return err
}

print.InfoStatusEvent(os.Stdout, "Export to '%s' complete.", schedulerExportFile)

return nil
},
}

func init() {
SchedulerExportCmd.Flags().MarkHidden("namespace")
SchedulerExportCmd.Flags().StringVarP(&schedulerExportFile, "output-file", "o", "", "Output binary file to export jobs and actor reminders to.")
SchedulerExportCmd.MarkFlagRequired("output-file")
SchedulerCmd.AddCommand(SchedulerExportCmd)
}
58 changes: 58 additions & 0 deletions cmd/schedulerimport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"os"

"github.com/spf13/cobra"

"github.com/dapr/cli/pkg/print"
"github.com/dapr/cli/pkg/scheduler"
"github.com/dapr/kit/signals"
)

var (
schedulerImportFile string
)

var SchedulerImportCmd = &cobra.Command{
Use: "import",
Short: "Import all jobs and actor reminders from a binary file generated by 'dapr scheduler export'.",
Long: `Import jobs and actor reminders to Scheduler from a binary file generated by 'dapr scheduler export'.
dapr scheduler import -f export.bin`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := signals.Context()

err := scheduler.Import(ctx, scheduler.ExportImportOptions{
SchedulerNamespace: schedulerSchedulerNamespace,
KubernetesMode: kubernetesMode,
TargetFile: schedulerImportFile,
})
if err != nil {
return err
}

print.InfoStatusEvent(os.Stdout, "Import from '%s' complete.", schedulerImportFile)

return nil
},
}

func init() {
SchedulerImportCmd.Flags().MarkHidden("namespace")
SchedulerImportCmd.Flags().StringVarP(&schedulerImportFile, "input-file", "f", "", "Input file to import jobs and actor reminders from (required).")
SchedulerImportCmd.MarkFlagRequired("input-file")
SchedulerCmd.AddCommand(SchedulerImportCmd)
}
111 changes: 111 additions & 0 deletions cmd/schedulerlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2025 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"errors"
"os"
"slices"

"github.com/gocarina/gocsv"
"github.com/spf13/cobra"

"github.com/dapr/cli/pkg/scheduler"
"github.com/dapr/cli/utils"
"github.com/dapr/kit/ptr"
"github.com/dapr/kit/signals"
)

const (
schedulerListOutputFormatShort = "short"
schedulerListOutputFormatWide = "wide"
schedulerListOutputFormatYAML = "yaml"
schedulerListOutputFormatJSON = "json"
)

var (
schedulerListFilterType string
schedulerListOutputFormat string
)

var SchedulerListCmd = &cobra.Command{
Use: "list",
Short: "List scheduled jobs in the Scheduler",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := signals.Context()

if !slices.Contains([]string{
scheduler.FilterJobsAll,
scheduler.FilterJobsJob,
scheduler.FilterJobsActor,
}, schedulerListFilterType) {
return errors.New("invalid value for --filter-type. Supported values are 'all', 'jobs', 'actorreminder'.")
}

if !slices.Contains([]string{
schedulerListOutputFormatShort,
schedulerListOutputFormatWide,
schedulerListOutputFormatYAML,
schedulerListOutputFormatJSON,
}, schedulerListOutputFormat) {
return errors.New("invalid value for --output. Supported values are 'table', 'wide', 'yaml', 'json'.")
}

opts := scheduler.ListJobsOptions{
SchedulerNamespace: schedulerSchedulerNamespace,
KubernetesMode: kubernetesMode,
FilterJobType: schedulerListFilterType,
}
if schedulerNamespace != "" {
opts.DaprNamespace = ptr.Of(schedulerNamespace)
}

var list any
var err error
if schedulerListOutputFormat == schedulerListOutputFormatShort {
list, err = scheduler.ListJobsAsOutput(ctx, opts)
} else {
list, err = scheduler.ListJobsAsOutputWide(ctx, opts)
}
if err != nil {
return err
}

switch schedulerListOutputFormat {
case schedulerListOutputFormatYAML:
err = utils.PrintDetail(os.Stdout, "yaml", list)
case schedulerListOutputFormatJSON:
err = utils.PrintDetail(os.Stdout, "json", list)
default:
table, err := gocsv.MarshalString(list)

Check failure on line 91 in cmd/schedulerlist.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

shadow: declaration of "err" shadows declaration at line 75 (govet)
if err != nil {
break
}

utils.PrintTable(table)
}

if err != nil {
return err
}

return nil
},
}

func init() {
SchedulerListCmd.Flags().StringVar(&schedulerListFilterType, "filter-type", scheduler.FilterJobsAll, "Filter jobs by type. Supported values are 'all', 'jobs', 'actorreminder'")
SchedulerListCmd.Flags().StringVarP(&schedulerListOutputFormat, "output", "o", schedulerListOutputFormatShort, "Output format. One of 'short', 'wide', 'yaml', 'json'")
SchedulerCmd.AddCommand(SchedulerListCmd)
}
Loading
Loading