fix: max id issues
This commit is contained in:
@@ -4,8 +4,10 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"exporter/config"
|
||||
"exporter/database/telegram_resource/public/model"
|
||||
@@ -22,7 +24,14 @@ func PublishCmd() *cobra.Command {
|
||||
Use: "publish",
|
||||
Short: "publish posts",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return publishCmd(context.Background())
|
||||
for {
|
||||
if err := publishCmd(context.Background()); err != nil {
|
||||
log.Println("ERROR: ", err)
|
||||
return err
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
@@ -39,9 +48,19 @@ func publishCmd(ctx context.Context) error {
|
||||
|
||||
tbl := table.ChannelMessages
|
||||
tblC := table.Channels
|
||||
stmt := tbl.SELECT(tbl.AllColumns, tblC.Title).
|
||||
WHERE(tbl.Published.IS_FALSE()).
|
||||
var maxIDResult struct {
|
||||
MaxID int64
|
||||
}
|
||||
stmt := tbl.SELECT(MAX(tbl.ID).AS("maxID")).FROM(tbl)
|
||||
|
||||
if err := stmt.QueryContext(context.Background(), db, &maxIDResult); err != nil {
|
||||
return errors.Wrap(err, "failed to get max id")
|
||||
}
|
||||
|
||||
stmt = tbl.SELECT(tbl.AllColumns, tblC.Title).
|
||||
WHERE(tbl.Published.IS_FALSE().AND(tbl.ID.NOT_EQ(Int(maxIDResult.MaxID)))).
|
||||
LIMIT(1).
|
||||
ORDER_BY(tbl.UUID.ASC()).
|
||||
FROM(tbl.LEFT_JOIN(tblC, tbl.ChannelID.EQ(tblC.UUID)))
|
||||
if err := stmt.QueryContext(context.Background(), db, &msg); err != nil {
|
||||
return errors.Wrap(err, "failed to get message")
|
||||
@@ -57,6 +76,7 @@ func publishCmd(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Println("published: ", msg.ChannelMessages.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -90,7 +110,7 @@ func publish(_ context.Context, msg publishMsg) error {
|
||||
} else if media.WebPage != nil {
|
||||
data.Content += fmt.Sprintf("\n\n[%s](%s)", media.WebPage.Title, media.WebPage.URL)
|
||||
} else {
|
||||
return errors.New("unknown media type")
|
||||
continue
|
||||
}
|
||||
|
||||
filepath := filepath.Join(config.C.Output, file)
|
||||
|
||||
Reference in New Issue
Block a user