-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ddcc13a
commit 71feca2
Showing
15 changed files
with
315 additions
and
283 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
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,45 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/Improwised/golang-api/config" | ||
"github.com/Improwised/golang-api/pkg/watermill" | ||
"github.com/ThreeDotsLabs/watermill/message" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// GetAPICommandDef runs app | ||
// ? need rename in file name or command name | ||
func GetDeadQueueCommandDef(cfg config.AppConfig, logger *zap.Logger) cobra.Command { | ||
|
||
workerCommand := cobra.Command{ | ||
Use: "dead-letter-queue", | ||
Short: "To start dead-letter queue", | ||
Long: `This queue is used to store failed job from all worker`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
// Init worker | ||
subscriber, err := watermill.InitSubscriber(cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// run worker with topic(queue name) and process function | ||
if err := subscriber.Run(cfg.MQ.DeadQueue, HandleFailJob); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
return workerCommand | ||
} | ||
|
||
func HandleFailJob(msg *message.Message) error { | ||
fmt.Println("failed job", string(msg.Payload)) | ||
// process here | ||
return nil | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,32 @@ | ||
package workers | ||
|
||
import ( | ||
"log" | ||
|
||
helpers "github.com/Improwised/golang-api/helpers/smtp" | ||
) | ||
|
||
type WelcomeMail struct { | ||
FirstName string `json:"first_name" ` | ||
LastName string `json:"last_name"` | ||
Email string `json:"email"` | ||
Roles string `json:"roles"` | ||
} | ||
|
||
func (w WelcomeMail) Handle() error { | ||
log.Println("sending mail") | ||
|
||
smtp := helpers.NewSMTPHelper("sandbox.smtp.mailtrap.io", "2525", "7628fa366c0257c", "2afb7200812272") | ||
smtp.SetSubject("welocme") | ||
smtp.SetPlainBody([]byte("welcome to our org")) | ||
|
||
smtp.SetSender("chintansakhiya00001@gmail.com") | ||
smtp.SetReceivers([]string{"chintansakhiya00001@gmail.com"}) | ||
|
||
if err := smtp.SendMail(); err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("mail send to %v", w.Email) | ||
return nil | ||
} |
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,44 @@ | ||
package workers | ||
|
||
import ( | ||
"bytes" | ||
"encoding/gob" | ||
|
||
"github.com/ThreeDotsLabs/watermill/message" | ||
) | ||
|
||
func init() { | ||
for _, v := range ListStruct() { | ||
gob.Register(v) | ||
} | ||
} | ||
|
||
// Register all worker struct here befour run worker for proper unmarshalling | ||
func ListStruct() []interface{} { | ||
return []interface{}{ | ||
WelcomeMail{}, | ||
// ... | ||
} | ||
} | ||
|
||
// Handler interface for all worker struct | ||
type Handler interface { | ||
Handle() error | ||
} | ||
|
||
// process all worker struct and call Handle function according to struct | ||
func Process(msg *message.Message) error { | ||
buf := bytes.NewBuffer(msg.Payload) | ||
dec := gob.NewDecoder(buf) | ||
|
||
var result Handler | ||
err := dec.Decode(&result) | ||
if err != nil { | ||
return err | ||
} | ||
if err := result.Handle(); err != nil { | ||
return err | ||
} | ||
msg.Ack() | ||
return nil | ||
} |
Oops, something went wrong.