-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notification): add postgres queue
- Loading branch information
Showing
41 changed files
with
1,306 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/MakeNowJust/heredoc" | ||
"github.com/odpf/salt/cmdx" | ||
"github.com/odpf/salt/log" | ||
"github.com/odpf/salt/printer" | ||
"github.com/odpf/siren/config" | ||
"github.com/odpf/siren/core/notification" | ||
"github.com/odpf/siren/plugins/queues" | ||
"github.com/odpf/siren/plugins/queues/postgresq" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func jobCmd(cmdxConfig *cmdx.Config) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "job <command>", | ||
Aliases: []string{"jobs"}, | ||
Short: "Manage siren jobs", | ||
Long: "Jobs management commands.", | ||
Example: heredoc.Doc(` | ||
$ siren job run cleanup_queue | ||
`), | ||
} | ||
|
||
cmd.AddCommand( | ||
jobRunCommand(cmdxConfig), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func jobRunCommand(cmdxConfig *cmdx.Config) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "run", | ||
Short: "Trigger a job", | ||
Long: heredoc.Doc(` | ||
Trigger a job | ||
`), | ||
Args: cobra.ExactValidArgs(1), | ||
ValidArgs: []string{ | ||
"cleanup_queue", | ||
}, | ||
Example: heredoc.Doc(` | ||
$ siren job run cleanup_queue | ||
`), | ||
} | ||
|
||
cmd.AddCommand( | ||
jobRunCleanupQueueCommand(), | ||
) | ||
|
||
return cmd | ||
} | ||
|
||
func jobRunCleanupQueueCommand() *cobra.Command { | ||
var configFile string | ||
|
||
cmd := &cobra.Command{ | ||
Use: "cleanup_queue", | ||
Short: "Cleanup stale messages in queue", | ||
Long: heredoc.Doc(` | ||
Cleaning up all published messages in queue with last updated | ||
more than specific threshold (default 7 days) from now() and | ||
(Optional) cleaning up all pending messages in queue with last updated | ||
more than specific threshold (default 7 days) from now(). | ||
`), | ||
Example: heredoc.Doc(` | ||
$ siren job run cleanup_queue | ||
`), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg, err := config.Load(configFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var queue notification.Queuer | ||
switch cfg.Notification.Queue.Kind { | ||
case queues.KindPostgres: | ||
queue, err = postgresq.New(log.NewZap(), cfg.DB) | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
printer.Info("Cleanup queue job only works for postgres queue") | ||
return nil | ||
} | ||
|
||
spinner := printer.Spin("") | ||
defer spinner.Stop() | ||
printer.Info("Running job cleanup_queue(%s)", string(cfg.Notification.Queue.Kind)) | ||
if err := queue.Cleanup(cmd.Context(), queues.FilterCleanup{}); err != nil { | ||
return err | ||
} | ||
spinner.Stop() | ||
printer.Success(fmt.Sprintf("Job cleanup_queue(%s) finished", string(cfg.Notification.Queue.Kind))) | ||
printer.Space() | ||
printer.SuccessIcon() | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().StringVarP(&configFile, "config", "c", "config.yaml", "Config file path") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package notification | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/odpf/siren/plugins/queues" | ||
) | ||
|
||
type Config struct { | ||
Queue queues.Config `mapstructure:"queue"` | ||
MessageHandlers []HandlerConfig `mapstructure:"message_handlers"` | ||
DLQHandlers []HandlerConfig `mapstructure:"dlq_handlers"` | ||
} | ||
|
||
type HandlerConfig struct { | ||
PollDuration time.Duration `mapstructure:"poll_duration"` | ||
ReceiverTypes []string `mapstructure:"receiver_types"` | ||
BatchSize int `mapstructure:"batch_size"` | ||
} |
Oops, something went wrong.