simplify send message bg helper
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
ycc
2026-03-01 21:15:17 +01:00
parent 7d06f0ff3e
commit 0fdf5dd9c7
5 changed files with 171 additions and 130 deletions

View File

@@ -21,7 +21,9 @@ const (
// SendJob describes a message to send, together with its delivery tracking state.
//
// The File field holds the path of a pre-built packed server message (binary).
// The File field holds the path to an outbox file written by CreateUserMessageAndSendJob.
// It must follow the naming convention outbox/{dbFile}_{dbId} so that
// ProcessSentMessages can recover the message DB location from the filename alone.
// Servers is tried in order; after MaxRetriesPerServer failures on one server
// the next one is attempted.
//
@@ -29,12 +31,10 @@ const (
// are managed by the queue functions and must not be set by the caller.
type SendJob struct {
// --- caller-supplied fields ---
Queue string `json:"queue,omitempty"` // uid of destination peer, used for naming the sent queue sqlite db
File string `json:"file,omitempty"` // filename which content shall be sent to the server as message payload
MessageDbFile string `json:"message_db_file,omitempty"` // peer message DB UUID (no path, no suffix) — from peer.LastMessage.Dbfile
MessageDbId int64 `json:"message_db_id,omitempty"` // SQLite row ID in MessageDbFile — from peer.LastMessage.Dbid
Servers []Server `json:"servers,omitempty"`
Timeout int `json:"timeout,omitempty"` // seconds; 0 = no timeout
Queue string `json:"queue,omitempty"` // uid of destination peer, used for naming the queue sqlite db
File string `json:"file,omitempty"` // outbox file path; basename must be {dbFile}_{dbId}
Servers []Server `json:"servers,omitempty"`
Timeout int `json:"timeout,omitempty"` // seconds; 0 = no timeout
// --- DB-managed tracking fields (not serialised by the caller) ---
ID int64
@@ -70,8 +70,6 @@ func openOrCreateSendQueue(dbPath string) (*sql.DB, error) {
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS queue (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
file TEXT NOT NULL,
message_db_file TEXT NOT NULL DEFAULT '',
message_db_id INTEGER NOT NULL DEFAULT 0,
servers TEXT NOT NULL,
timeout INTEGER NOT NULL DEFAULT 0,
inserted_at INTEGER NOT NULL,
@@ -84,10 +82,6 @@ func openOrCreateSendQueue(dbPath string) (*sql.DB, error) {
db.Close()
return nil, err
}
// Migration: add columns to existing DBs that pre-date this schema version.
// SQLite returns an error if the column already exists; we silently ignore it.
db.Exec(`ALTER TABLE queue ADD COLUMN message_db_file TEXT NOT NULL DEFAULT ''`)
db.Exec(`ALTER TABLE queue ADD COLUMN message_db_id INTEGER NOT NULL DEFAULT 0`)
return db, nil
}
@@ -109,10 +103,9 @@ func PushSendJob(storagePath string, job *SendJob) error {
return err
}
_, err = db.Exec(
`INSERT INTO queue(file, message_db_file, message_db_id, servers, timeout, inserted_at, status, retries)
VALUES(?,?,?,?,?,?,?,?)`,
job.File, job.MessageDbFile, job.MessageDbId,
string(serversJSON), job.Timeout, time.Now().Unix(), SendStatusPending, string(retriesJSON),
`INSERT INTO queue(file, servers, timeout, inserted_at, status, retries)
VALUES(?,?,?,?,?,?)`,
job.File, string(serversJSON), job.Timeout, time.Now().Unix(), SendStatusPending, string(retriesJSON),
)
return err
}
@@ -129,8 +122,6 @@ func PeekSendJob(storagePath, queue string) (*SendJob, int64, error) {
var (
id int64
file string
messageDbFile string
messageDbId int64
serversJSON string
timeout int
insertedAt int64
@@ -140,10 +131,10 @@ func PeekSendJob(storagePath, queue string) (*SendJob, int64, error) {
successfulServer sql.NullInt64
)
err = db.QueryRow(
`SELECT id, file, message_db_file, message_db_id, servers, timeout, inserted_at, status, sent_at, retries, successful_server
`SELECT id, file, servers, timeout, inserted_at, status, sent_at, retries, successful_server
FROM queue WHERE status = ? ORDER BY id ASC LIMIT 1`,
SendStatusPending,
).Scan(&id, &file, &messageDbFile, &messageDbId, &serversJSON, &timeout, &insertedAt, &status, &sentAt, &retriesJSON, &successfulServer)
).Scan(&id, &file, &serversJSON, &timeout, &insertedAt, &status, &sentAt, &retriesJSON, &successfulServer)
if err == sql.ErrNoRows {
return nil, 0, nil
}
@@ -161,16 +152,14 @@ func PeekSendJob(storagePath, queue string) (*SendJob, int64, error) {
}
job := &SendJob{
ID: id,
Queue: queue,
File: file,
MessageDbFile: messageDbFile,
MessageDbId: messageDbId,
Servers: servers,
Timeout: timeout,
InsertedAt: time.Unix(insertedAt, 0),
Status: status,
Retries: retries,
ID: id,
Queue: queue,
File: file,
Servers: servers,
Timeout: timeout,
InsertedAt: time.Unix(insertedAt, 0),
Status: status,
Retries: retries,
}
if sentAt.Valid {
t := time.Unix(sentAt.Int64, 0)
@@ -222,8 +211,6 @@ func GetSendJob(storagePath, queue string, id int64) (*SendJob, error) {
var (
file string
messageDbFile string
messageDbId int64
serversJSON string
timeout int
insertedAt int64
@@ -233,10 +220,10 @@ func GetSendJob(storagePath, queue string, id int64) (*SendJob, error) {
successfulServer sql.NullInt64
)
err = db.QueryRow(
`SELECT file, message_db_file, message_db_id, servers, timeout, inserted_at, status, sent_at, retries, successful_server
`SELECT file, servers, timeout, inserted_at, status, sent_at, retries, successful_server
FROM queue WHERE id = ?`,
id,
).Scan(&file, &messageDbFile, &messageDbId, &serversJSON, &timeout, &insertedAt, &status, &sentAt, &retriesJSON, &successfulServer)
).Scan(&file, &serversJSON, &timeout, &insertedAt, &status, &sentAt, &retriesJSON, &successfulServer)
if err == sql.ErrNoRows {
return nil, nil
}
@@ -254,16 +241,14 @@ func GetSendJob(storagePath, queue string, id int64) (*SendJob, error) {
}
job := &SendJob{
ID: id,
Queue: queue,
File: file,
MessageDbFile: messageDbFile,
MessageDbId: messageDbId,
Servers: servers,
Timeout: timeout,
InsertedAt: time.Unix(insertedAt, 0),
Status: status,
Retries: retries,
ID: id,
Queue: queue,
File: file,
Servers: servers,
Timeout: timeout,
InsertedAt: time.Unix(insertedAt, 0),
Status: status,
Retries: retries,
}
if sentAt.Valid {
t := time.Unix(sentAt.Int64, 0)
@@ -287,7 +272,7 @@ func GetSentJobs(storagePath, queue string) ([]*SendJob, error) {
defer db.Close()
rows, err := db.Query(
`SELECT id, file, message_db_file, message_db_id, servers, timeout, inserted_at, sent_at, retries, successful_server
`SELECT id, file, servers, timeout, inserted_at, sent_at, retries, successful_server
FROM queue WHERE status = ? ORDER BY id ASC`,
SendStatusSent,
)
@@ -301,8 +286,6 @@ func GetSentJobs(storagePath, queue string) ([]*SendJob, error) {
var (
id int64
file string
messageDbFile string
messageDbId int64
serversJSON string
timeout int
insertedAt int64
@@ -310,7 +293,7 @@ func GetSentJobs(storagePath, queue string) ([]*SendJob, error) {
retriesJSON string
successfulServer sql.NullInt64
)
if err := rows.Scan(&id, &file, &messageDbFile, &messageDbId, &serversJSON, &timeout, &insertedAt, &sentAt, &retriesJSON, &successfulServer); err != nil {
if err := rows.Scan(&id, &file, &serversJSON, &timeout, &insertedAt, &sentAt, &retriesJSON, &successfulServer); err != nil {
return nil, err
}
@@ -324,16 +307,14 @@ func GetSentJobs(storagePath, queue string) ([]*SendJob, error) {
}
job := &SendJob{
ID: id,
Queue: queue,
File: file,
MessageDbFile: messageDbFile,
MessageDbId: messageDbId,
Servers: servers,
Timeout: timeout,
InsertedAt: time.Unix(insertedAt, 0),
Status: SendStatusSent,
Retries: retries,
ID: id,
Queue: queue,
File: file,
Servers: servers,
Timeout: timeout,
InsertedAt: time.Unix(insertedAt, 0),
Status: SendStatusSent,
Retries: retries,
}
if sentAt.Valid {
t := time.Unix(sentAt.Int64, 0)