on job no longer in results, delete from matrix;; no backwards compatible

master
bel 2022-01-17 21:29:56 -07:00
parent 6a2b2f38d0
commit 92b6019052
5 changed files with 74 additions and 53 deletions

View File

@ -16,7 +16,8 @@ type Job struct {
Weight int
Miles int
Meta string
secrets func() interface{} `json:"-"`
Pays string
secrets func(j *Job) `json:"-"`
}
type JobLocation struct {
@ -29,12 +30,7 @@ func (j *Job) Secrets() {
if j.secrets == nil {
return
}
v := j.secrets()
j.secrets = nil
if v == nil {
return
}
j.Meta = fmt.Sprintf("%s %+v", j.Meta, v)
j.secrets(j)
}
func (j Job) String() string {
@ -53,6 +49,19 @@ func (j JobLocation) String() string {
return fmt.Sprintf("%s, %s @ %s", j.City, j.State, j.Date.Format("Monday Jan 02"))
}
func (j Job) FormatMultilineTextDead() string {
return fmt.Sprintf(
"no longer available: %s,%s => %s,%s for $%v @%s",
j.Pickup.City,
j.Pickup.State,
j.Dropoff.City,
j.Dropoff.State,
j.Pays,
j.URI,
)
}
func (j Job) FormatMultilineText() string {
foo := func(client string) string {
return fmt.Sprintf(

View File

@ -143,13 +143,14 @@ func (ntgJob *ntgVisionJob) Job() Job {
Miles: ntgJob.Miles,
Weight: ntgJob.Weight,
Meta: fmt.Sprintf("equipment:%s", ntgJob.Equipment),
secrets: func() interface{} {
secrets: func(j *Job) {
jobInfo, err := ntgJob.JobInfo()
if err != nil {
logtr.Errorf("failed to get jobinfo: %v", err)
return nil
return
}
return jobInfo.String()
j.Meta = jobInfo.String()
j.Pays = fmt.Sprint(jobInfo.PayUpTo)
},
}
}

View File

@ -1,36 +1,2 @@
[
{
"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": []
}
]

View File

@ -1,7 +1,7 @@
{
"Log": {
"Path": "/tmp/truckstop.log",
"Level": "verbose",
"Level": "debug",
"SOSMatrix": {
"ReceiveEnabled": true,
"Mock": false,
@ -69,7 +69,7 @@
"Room": "!OYZqtInrBCn1cyz90D:m.bltrucks.top"
}
},
"Once": false,
"Once": true,
"Brokers": {
"NTG": {
"JobInfo": true,

59
main.go
View File

@ -9,6 +9,7 @@ import (
"local/truckstop/config"
"local/truckstop/logtr"
"local/truckstop/message"
"log"
"net/http"
"net/url"
"regexp"
@ -235,7 +236,7 @@ func _main() error {
logtr.Errorf("failed _main: %v", err)
}
if config.Get().Once {
time.Sleep(time.Second)
time.Sleep(3 * time.Second)
return err
}
if err != nil {
@ -267,6 +268,11 @@ func once() error {
if err != nil {
return err
}
logtr.Debugf("once: update dead jobs: %+v", alljobs)
err = updateDeadJobs(alljobs)
if err != nil {
return err
}
logtr.Debugf("once: all jobs: %+v", alljobs)
newjobs, err := dropStaleJobs(alljobs)
if err != nil {
@ -314,6 +320,45 @@ func getJobs() ([]broker.Job, error) {
return jobs, nil
}
type recordedJob struct {
Job broker.Job
SentNS int64
MatrixID string
}
func updateDeadJobs(jobs []broker.Job) error {
db := config.Get().DB()
list, err := db.List([]string{}, "sent_job_", "sent_job_}}")
if err != nil {
return err
}
log.Printf("db.List() => %+v", list)
for _, listEntry := range list {
wouldBe := strings.TrimPrefix(listEntry, "sent_job_")
found := false
for i := range jobs {
found = found || jobs[i].ID == wouldBe
}
logtr.Debugf("found job %s to be still alive==%v", wouldBe, found)
if !found {
logtr.Debugf("updating dead job %+v", listEntry)
b, err := db.Get(listEntry)
if err != nil {
return err
}
var recorded recordedJob
if err := json.Unmarshal(b, &recorded); err != nil {
return err
}
if err := message.NewMatrix().Update(recorded.MatrixID, recorded.Job.FormatMultilineTextDead()); err != nil {
return err
}
db.Set(listEntry, nil)
}
}
return nil
}
func dropStaleJobs(jobs []broker.Job) ([]broker.Job, error) {
db := config.Get().DB()
for i := len(jobs) - 1; i >= 0; i-- {
@ -345,17 +390,17 @@ func sendJob(job broker.Job) (bool, error) {
}
func() {
db := config.Get().DB()
b, err := json.Marshal(map[string]interface{}{
"Job": job,
"SentTS": time.Now().Unix(),
"MatrixID": id,
b, err := json.Marshal(recordedJob{
Job: job,
SentNS: time.Now().UnixNano(),
MatrixID: id,
})
if err != nil {
logtr.Debugf("cannot marshal job+meta: %v", err)
logtr.Errorf("failed to marshal recorded job: %v", err)
return
}
if err := db.Set("sent_job_"+job.ID, b); err != nil {
logtr.Debugf("cannot db.set job+meta: %v", err)
logtr.Errorf("failed to set recorded job: %v", err)
return
}
}()