when a job dies, delete images associated with it

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

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
}