123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package workers
- import (
- "context"
- "fmt"
- "html"
- "io/ioutil"
- "os"
- "strings"
- "time"
- "golang-fave/engine/config"
- "golang-fave/engine/mysqlpool"
- "golang-fave/engine/sqlw"
- "golang-fave/engine/utils"
- "github.com/vladimirok5959/golang-worker/worker"
- )
- func SmtpSender(www_dir string, mp *mysqlpool.MySqlPool) *worker.Worker {
- return worker.New(func(ctx context.Context, w *worker.Worker, o *[]worker.Iface) {
- if www_dir, ok := (*o)[0].(string); ok {
- if mp, ok := (*o)[1].(*mysqlpool.MySqlPool); ok {
- smtp_loop(ctx, www_dir, mp)
- }
- }
- select {
- case <-ctx.Done():
- case <-time.After(5 * time.Second):
- return
- }
- }, &[]worker.Iface{
- www_dir,
- mp,
- })
- }
- func smtp_loop(ctx context.Context, www_dir string, mp *mysqlpool.MySqlPool) {
- dirs, err := ioutil.ReadDir(www_dir)
- if err == nil {
- for _, dir := range dirs {
- select {
- case <-ctx.Done():
- return
- default:
- if mp != nil {
- target_dir := strings.Join([]string{www_dir, dir.Name()}, string(os.PathSeparator))
- if utils.IsDirExists(target_dir) {
- smtp_process(ctx, target_dir, dir.Name(), mp)
- }
- }
- }
- }
- }
- }
- func smtp_process(ctx context.Context, dir, host string, mp *mysqlpool.MySqlPool) {
- db := mp.Get(host)
- if db != nil {
- conf := config.ConfigNew()
- if err := conf.ConfigRead(strings.Join([]string{dir, "config", "config.json"}, string(os.PathSeparator))); err == nil {
- if !((*conf).SMTP.Host == "" || (*conf).SMTP.Login == "" && (*conf).SMTP.Password == "") {
- if err := db.Ping(ctx); err == nil {
- smtp_prepare(ctx, db, conf)
- }
- }
- } else {
- fmt.Printf("Smtp error (config): %v\n", err)
- }
- }
- }
- func smtp_prepare(ctx context.Context, db *sqlw.DB, conf *config.Config) {
- rows, err := db.Query(
- ctx,
- `SELECT
- id,
- email,
- subject,
- message
- FROM
- fave_notify_mail
- WHERE
- status = 2
- ORDER BY
- id ASC
- ;`,
- )
- if err == nil {
- defer rows.Close()
- values := make([]string, 4)
- scan := make([]interface{}, len(values))
- for i := range values {
- scan[i] = &values[i]
- }
- for rows.Next() {
- err = rows.Scan(scan...)
- if err == nil {
- if _, err := db.Exec(
- ctx,
- `UPDATE fave_notify_mail SET status = 3 WHERE id = ?;`,
- utils.StrToInt(string(values[0])),
- ); err == nil {
- go func(db *sqlw.DB, conf *config.Config, id int, subject, msg string, receivers []string) {
- if err := smtp_send(
- ctx,
- (*conf).SMTP.Host,
- utils.IntToStr((*conf).SMTP.Port),
- (*conf).SMTP.Login,
- (*conf).SMTP.Password,
- subject,
- msg,
- receivers,
- ); err == nil {
- if _, err := db.Exec(
- ctx,
- `UPDATE fave_notify_mail SET status = 1 WHERE id = ?;`,
- id,
- ); err != nil {
- fmt.Printf("Smtp send error (sql, success): %v\n", err)
- }
- } else {
- if _, err := db.Exec(
- ctx,
- `UPDATE fave_notify_mail SET error = ?, status = 0 WHERE id = ?;`,
- err.Error(),
- id,
- ); err != nil {
- fmt.Printf("Smtp send error (sql, error): %v\n", err)
- }
- }
- }(
- db,
- conf,
- utils.StrToInt(string(values[0])),
- html.EscapeString(string(values[2])),
- string(values[3]),
- []string{html.EscapeString(string(values[1]))},
- )
- } else {
- fmt.Printf("Smtp send error (sql, update): %v\n", err)
- }
- }
- }
- }
- }
- func smtp_send(ctx context.Context, host, port, user, pass, subject, msg string, receivers []string) error {
- return utils.SMTPSend(host, port, user, pass, subject, msg, receivers)
- }
|