Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3c36948269 | ||
|
|
bc2efe928a | ||
|
|
e1b4460ebd | ||
|
|
9bb9929ff6 | ||
|
|
a6c1b8505a | ||
|
|
c755aa88fb | ||
|
|
b451ed93bf | ||
|
|
76b7211d6c | ||
|
|
31a608d7f8 | ||
|
|
0c3419a1fb |
@@ -1,6 +1,7 @@
|
||||
package broker
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"local/truckstop/config"
|
||||
"local/truckstop/logtr"
|
||||
@@ -26,6 +27,18 @@ type JobLocation struct {
|
||||
State string
|
||||
}
|
||||
|
||||
func (j Job) UID() string {
|
||||
return fmt.Sprintf(
|
||||
"%v-%s-%s-%s-%s-%v",
|
||||
j.ID,
|
||||
j.Pickup.State,
|
||||
base64.StdEncoding.EncodeToString([]byte(j.Pickup.City)),
|
||||
j.Dropoff.State,
|
||||
base64.StdEncoding.EncodeToString([]byte(j.Dropoff.City)),
|
||||
j.Pickup.Date.Unix(),
|
||||
)
|
||||
}
|
||||
|
||||
func (j *Job) Secrets() {
|
||||
if j.secrets == nil {
|
||||
return
|
||||
|
||||
@@ -93,7 +93,7 @@ func (ntgJob *ntgVisionJob) JobInfo() (ntgVisionJobInfo, error) {
|
||||
key := fmt.Sprintf("ntg_job_info_%v", ntgJob.ID)
|
||||
if b, err := db.Get(key); err != nil {
|
||||
} else if err := json.Unmarshal(b, &ntgJob.jobinfo); err == nil {
|
||||
return ntgJob.jobinfo, fmt.Errorf("failed to parse ntg job info from db: %w: %s", err, b)
|
||||
return ntgJob.jobinfo, nil
|
||||
}
|
||||
ntg := NewNTGVision()
|
||||
ji, err := ntg.SearchJob(ntgJob.ID)
|
||||
@@ -228,12 +228,14 @@ func setNTGToken(token string) {
|
||||
|
||||
func (ntg NTGVision) search(states []config.State) (io.ReadCloser, error) {
|
||||
if getNTGToken() == "" {
|
||||
logtr.Debugf("NTG token is empty, refreshing ntg auth")
|
||||
if err := ntg.refreshAuth(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
rc, err := ntg._search(states)
|
||||
if err == ErrNoAuth {
|
||||
logtr.Debugf("err no auth on search, refreshing ntg auth")
|
||||
if err := ntg.refreshAuth(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -295,7 +297,9 @@ func (ntg NTGVision) _search(states []config.State) (io.ReadCloser, error) {
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
b, _ := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode > 400 && resp.StatusCode < 500 && resp.StatusCode != 404 && resp.StatusCode != 410 {
|
||||
logtr.Debugf("ntg auth bad status: url=%s, status=%v, body=%s", request.URL.String(), resp.StatusCode, b)
|
||||
if resp.StatusCode > 400 && resp.StatusCode < 404 {
|
||||
logtr.Debugf("ntg auth bad status: err no auth")
|
||||
return nil, ErrNoAuth
|
||||
}
|
||||
return nil, fmt.Errorf("bad status searching ntg: %d: %s", resp.StatusCode, b)
|
||||
@@ -305,7 +309,7 @@ func (ntg NTGVision) _search(states []config.State) (io.ReadCloser, error) {
|
||||
|
||||
func (ntg NTGVision) newRequest(states []config.State) (*http.Request, error) {
|
||||
body, err := json.Marshal(map[string]interface{}{
|
||||
"OriginFromDate": time.Now().UTC().Format("2006-01-02T15:04:05.000Z"),
|
||||
"OriginFromDate": time.Now().Add(time.Hour * -24).UTC().Format("2006-01-02T15:04:05.000Z"),
|
||||
"OriginToDate": time.Now().UTC().Add(time.Hour * 24 * 30).Format("2006-01-02T15:04:05.000Z"),
|
||||
"DestinationFromDate": nil,
|
||||
"DestinationToDate": nil,
|
||||
|
||||
@@ -82,7 +82,7 @@ func SetLevel(l Level) {
|
||||
}
|
||||
|
||||
func logf(l Level, format string, args []interface{}) {
|
||||
format = fmt.Sprintf("%v: %v: %s\n", time.Now().Format("15:04:05"), l.String(), strings.TrimSpace(format))
|
||||
format = fmt.Sprintf("%v: %v: %s\n", time.Now().Format("01-02T15:04:05"), l.String(), strings.TrimSpace(format))
|
||||
cLevel := level
|
||||
cAnsoser := ansoser
|
||||
if l >= cLevel {
|
||||
|
||||
10
main.go
10
main.go
@@ -291,12 +291,13 @@ func once() error {
|
||||
jobs[i].Secrets()
|
||||
}
|
||||
logtr.Infof("once: sending jobs: %+v", jobs)
|
||||
db := config.Get().DB()
|
||||
for i := range jobs {
|
||||
if ok, err := sendJob(jobs[i]); err != nil {
|
||||
return err
|
||||
} else if ok {
|
||||
logtr.Debugf("sent job", jobs[i])
|
||||
if err := config.Get().DB().Set(jobs[i].ID, []byte(`sent`)); err != nil {
|
||||
if err := db.Set(jobs[i].UID(), []byte(`sent`)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -349,9 +350,14 @@ func updateDeadJobs(jobs []broker.Job) error {
|
||||
if err := json.Unmarshal(b, &recorded); err != nil {
|
||||
return err
|
||||
}
|
||||
/* // TODO this beeps on fluffychat
|
||||
if err := message.NewMatrix().Update(recorded.MatrixID, recorded.Job.FormatMultilineTextDead()); err != nil {
|
||||
return err
|
||||
}
|
||||
*/
|
||||
if err := message.NewMatrix().Remove(recorded.MatrixID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.Set(listEntry, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -368,7 +374,7 @@ func updateDeadJobs(jobs []broker.Job) error {
|
||||
func dropStaleJobs(jobs []broker.Job) ([]broker.Job, error) {
|
||||
db := config.Get().DB()
|
||||
for i := len(jobs) - 1; i >= 0; i-- {
|
||||
if _, err := db.Get(jobs[i].ID); err == storage.ErrNotFound {
|
||||
if _, err := db.Get(jobs[i].UID()); err == storage.ErrNotFound {
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user