Create generic backend process, fix background interdependencies
This refactor was born out of the inter-dependency cycles developing between the "background" module and just about every other module which was caused by the background module becoming a dependency of every module that needed to background work and the fact that the background module was also supposedly responsible for the logic for processing those tasks. Instead the "background" module is now very, very shallow and relies entirely on the Postgres NOTIFY logic for triggering jobs. There's a new table, `job` which holds just a type and single row ID. All told, this means that jobs can be added to the queue as part of the API-level or platform-level transaction, ensuring atomicity, and processing coordination is handled by the platform module, which can depend on anything.
This commit is contained in:
parent
3a28151b09
commit
2538638c9d
47 changed files with 1553 additions and 1054 deletions
18
api/api.go
18
api/api.go
|
|
@ -4,7 +4,6 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
|
|
@ -32,7 +31,7 @@ func apiAudioPost(w http.ResponseWriter, r *http.Request, u platform.User) {
|
|||
}
|
||||
|
||||
var payload NoteAudioPayload
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to read the payload", http.StatusBadRequest)
|
||||
return
|
||||
|
|
@ -49,6 +48,7 @@ func apiAudioPost(w http.ResponseWriter, r *http.Request, u platform.User) {
|
|||
Deleted: omitnull.FromPtr(payload.Deleted),
|
||||
DeletorID: omitnull.FromPtr(payload.DeletorID),
|
||||
Duration: omit.From(payload.Duration),
|
||||
OrganizationID: omit.From(u.Organization.ID()),
|
||||
Transcription: omitnull.FromPtr(payload.Transcription),
|
||||
TranscriptionUserEdited: omit.From(payload.TranscriptionUserEdited),
|
||||
Version: omit.From(payload.Version),
|
||||
|
|
@ -61,20 +61,24 @@ func apiAudioPost(w http.ResponseWriter, r *http.Request, u platform.User) {
|
|||
w.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func apiAudioContentPost(w http.ResponseWriter, r *http.Request, u platform.User) {
|
||||
func apiAudioContentPost(w http.ResponseWriter, r *http.Request, user platform.User) {
|
||||
u_str := chi.URLParam(r, "uuid")
|
||||
audioUUID, err := uuid.Parse(u_str)
|
||||
u, err := uuid.Parse(u_str)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to parse image UUID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
err = file.FileContentWrite(r.Body, file.CollectionAudioRaw, audioUUID)
|
||||
err = file.FileContentWrite(r.Body, file.CollectionAudioRaw, u)
|
||||
if err != nil {
|
||||
log.Printf("Failed to write content file: %v", err)
|
||||
http.Error(w, "failed to write content file", http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
background.AudioTranscode(audioUUID)
|
||||
ctx := r.Context()
|
||||
a, err := models.NoteAudios.Query(
|
||||
models.SelectWhere.NoteAudios.UUID.EQ(u),
|
||||
models.SelectWhere.NoteAudios.OrganizationID.EQ(user.Organization.ID()),
|
||||
).One(ctx, db.PGInstance.BobDB)
|
||||
background.NewAudioTranscode(ctx, db.PGInstance.BobDB, a.ID)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
|
@ -139,7 +140,12 @@ func twilioTextPost(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
go text.HandleTextMessage(src, dst, body)
|
||||
go func() {
|
||||
err := text.HandleTextMessage(context.Background(), src, dst, body)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to handle Twilio incoming text")
|
||||
}
|
||||
}()
|
||||
w.Header().Set("Content-Type", "text/xml")
|
||||
fmt.Fprintf(w, "%s", twiml)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
|
@ -94,6 +95,11 @@ func voipmsTextPost(w http.ResponseWriter, r *http.Request) {
|
|||
log.Info().Int("ID", b.Data.ID).Str("event_type", b.Data.EventType).Str("record_type", b.Data.RecordType).Str("from", b.Data.Payload.From.PhoneNumber).Str("to", to).Str("content", b.Data.Payload.Text).Msg("Text status")
|
||||
|
||||
// Convert phone numbers from Voip.ms into E164 format for consistency
|
||||
go text.HandleTextMessage(b.Data.Payload.From.PhoneNumber, to, b.Data.Payload.Text)
|
||||
go func() {
|
||||
err := text.HandleTextMessage(context.Background(), b.Data.Payload.From.PhoneNumber, to, b.Data.Payload.Text)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to handle VoIP.ms incoming text")
|
||||
}
|
||||
}()
|
||||
fmt.Fprintf(w, "ok")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue