when a job dies, delete images associated with it

master
bel 2022-01-17 21:57:33 -07:00
parent 92b6019052
commit ffa33ea299
4 changed files with 91 additions and 22 deletions

View File

@ -1,2 +1,36 @@
[
{
"id": 4650337,
"sDate": "01/12/22",
"sCity": "Advance",
"sState": "NC",
"sdh": null,
"cDate": "01/13/22",
"cCity": "Sacramento",
"cState": "CA",
"cdh": null,
"stopCnt": 2,
"miles": 578,
"weight": 5000,
"equip": "Str Truck W/ Lift Gate",
"temp": "",
"alertReasons": []
},
{
"id": 4650338,
"sDate": "01/12/22",
"sCity": "Advance",
"sState": "NC",
"sdh": null,
"cDate": "01/13/22",
"cCity": "Winston-Salem",
"cState": "NC",
"cdh": null,
"stopCnt": 2,
"miles": 378,
"weight": 2500,
"equip": "107 tall",
"temp": "",
"alertReasons": []
}
]

41
main.go
View File

@ -321,9 +321,10 @@ func getJobs() ([]broker.Job, error) {
}
type recordedJob struct {
Job broker.Job
SentNS int64
MatrixID string
Job broker.Job
SentNS int64
MatrixID string
MatrixImageIDs []string
}
func updateDeadJobs(jobs []broker.Job) error {
@ -353,7 +354,14 @@ func updateDeadJobs(jobs []broker.Job) error {
if err := message.NewMatrix().Update(recorded.MatrixID, recorded.Job.FormatMultilineTextDead()); err != nil {
return err
}
db.Set(listEntry, nil)
if err := db.Set(listEntry, nil); err != nil {
return err
}
for _, imageid := range recorded.MatrixImageIDs {
if err := message.NewMatrix().Remove(imageid); err != nil {
return err
}
}
}
}
return nil
@ -388,13 +396,14 @@ func sendJob(job broker.Job) (bool, error) {
if err != nil {
return false, err
}
func() {
recordedJob := recordedJob{
Job: job,
SentNS: time.Now().UnixNano(),
MatrixID: id,
}
defer func() {
db := config.Get().DB()
b, err := json.Marshal(recordedJob{
Job: job,
SentNS: time.Now().UnixNano(),
MatrixID: id,
})
b, err := json.Marshal(recordedJob)
if err != nil {
logtr.Errorf("failed to marshal recorded job: %v", err)
return
@ -461,24 +470,30 @@ func sendJob(job broker.Job) (bool, error) {
uri = fmt.Sprintf("%s&zoom=%d", uri, maps.Pathed.Zoom.Override)
}
logtr.Debugf("sending pathed image: %s", uri)
if err := sender.SendImage(uri); err != nil {
pathedid, err := sender.SendImageTracked(uri)
if err != nil {
return true, err
}
recordedJob.MatrixImageIDs = append(recordedJob.MatrixImageIDs, pathedid)
}
}
if maps.Pickup {
uri := fmt.Sprintf(maps.URIFormat, pickup, pickup)
logtr.Debugf("sending pickup image: %s", uri)
if err := sender.SendImage(uri); err != nil {
pickupid, err := sender.SendImageTracked(uri)
if err != nil {
return true, err
}
recordedJob.MatrixImageIDs = append(recordedJob.MatrixImageIDs, pickupid)
}
if maps.Dropoff {
uri := fmt.Sprintf(maps.URIFormat, dropoff, dropoff)
logtr.Debugf("sending dropoff image: %s", uri)
if err := sender.SendImage(uri); err != nil {
dropid, err := sender.SendImageTracked(uri)
if err != nil {
return true, err
}
recordedJob.MatrixImageIDs = append(recordedJob.MatrixImageIDs, dropid)
}
return true, nil
}

View File

@ -125,6 +125,19 @@ func (m *Matrix) Receive() ([]Message, error) {
return messages, nil
}
func (m Matrix) Remove(id string) error {
if m.mock {
logtr.Infof("matrix.Remove(%s)", id)
return nil
}
c, err := m.getclient()
if err != nil {
return err
}
_, err = c.RedactEvent(m.room, id, &gomatrix.ReqRedact{Reason: "stale"})
return err
}
func (m Matrix) Update(id, text string) error {
if m.mock {
logtr.Infof("matrix.Update(%s)", text)
@ -185,36 +198,41 @@ func (m Matrix) SendTracked(text string) (string, error) {
}
func (m Matrix) SendImage(uri string) error {
_, err := m.SendImageTracked(uri)
return err
}
func (m Matrix) SendImageTracked(uri string) (string, error) {
if m.mock {
logtr.Infof("matrix.SendImage(%s)", uri)
return nil
return "", nil
}
response, err := http.Get(uri)
if err != nil {
return err
return "", err
}
if response.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(response.Body)
response.Body.Close()
return fmt.Errorf("failed to get %s: (%d) %s", uri, response.StatusCode, b)
return "", fmt.Errorf("failed to get %s: (%d) %s", uri, response.StatusCode, b)
}
b, err := ioutil.ReadAll(response.Body)
response.Body.Close()
if err != nil {
return err
return "", err
}
c, err := m.getclient()
if err != nil {
return err
return "", err
}
mediaUpload, err := c.UploadToContentRepo(bytes.NewReader(b), "image/jpeg", int64(len(b)))
if err != nil {
return err
return "", err
}
publicURI := mediaUpload.ContentURI
resp, err := c.SendImage(m.room, "img", publicURI)
logtr.Debugf("sent image %s => %s: %+v", uri, publicURI, resp)
return err
return resp.EventID, err
}
func SetMatrixContinuation(continuation string) {

View File

@ -10,9 +10,11 @@ import (
"github.com/google/uuid"
)
func TestMatrixSend(t *testing.T) {
func TestMatrixSendDel(t *testing.T) {
sender := testMatrix(t)
if err := sender.Send("hello world from unittest"); err != nil {
if id, err := sender.SendTracked("hello world from unittest"); err != nil {
t.Fatal(err)
} else if err := sender.Remove(id); err != nil {
t.Fatal(err)
}
}