-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder.go
143 lines (114 loc) · 3.2 KB
/
order.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package crawly
import (
"context"
"log/slog"
"time"
"github.com/rubpy/crawly/clog"
)
//////////////////////////////////////////////////
type Order struct {
Command TrackingCommand `json:"command"`
Attempt int `json:"attempt"`
LastProcessing time.Time `json:"last_processing"`
Handle Handle `json:"handle"`
Data any `json:"data"`
}
type OrderHandler func(ctx context.Context, order *Order, result *TrackingResult) error
//////////////////////////////////////////////////
func (cr *Crawler) processOrder(parentCtx context.Context, order *Order, result *TrackingResult) (err error) {
if err = parentCtx.Err(); err != nil {
return
}
result.Order.Value = *order
result.Entity.Value.Handle = order.Handle
var ctx context.Context
var cancel context.CancelFunc
settings := cr.loadSettings()
timeout := settings.TrackingOrderTimeout
if timeout > 0 {
ctx, cancel = context.WithTimeoutCause(parentCtx, timeout, ExceededTrackingOrderTimeout)
} else {
ctx, cancel = context.WithCancel(parentCtx)
}
defer cancel()
if !result.Order.Value.LastProcessing.IsZero() {
elapsed := time.Now().Sub(result.Order.Value.LastProcessing)
if elapsed < settings.MinimumTrackingOrderDelay {
return
}
}
maxAttempts := settings.MaximumTrackingOrderAttempts
if maxAttempts < 1 {
maxAttempts = -1
}
switch result.Order.Value.Command {
case TrackingCommandStart:
{
handlers := cr.loadHandlers()
if handlers.Order != nil {
result.Order.Err = handlers.Order(ctx, &result.Order.Value, result)
} else {
result.Order.Err = NilHandler
}
if result.Order.Err != nil {
result.Order.Value.Attempt++
} else {
result.Order.Value.Attempt = 0
}
if result.Order.Err != nil {
result.Entity.Action = TrackingActionNone
if result.Order.Err == InvalidHandle || (maxAttempts > 0 && result.Order.Value.Attempt >= maxAttempts) {
result.Order.Action = TrackingActionRemove
result.Entity.Action = TrackingActionNone
} else if result.Order.Action == TrackingActionNone {
result.Order.Action = TrackingActionUpdate
}
} else {
if result.Order.Action == TrackingActionNone {
result.Order.Action = TrackingActionRemove
result.Entity.Action = TrackingActionUpdate
}
}
}
case TrackingCommandStop:
result.Order.Action = TrackingActionRemove
result.Entity.Action = TrackingActionRemove
default:
result.Order.Err = InvalidTrackingCommand
result.Order.Action = TrackingActionRemove
}
result.Order.Value.LastProcessing = time.Now()
{
lp := clog.Params{
Message: "process:order",
Level: slog.LevelInfo,
Err: result.Order.Err,
}
{
g := clog.ParamGroup{
"handle": result.Order.Value.Handle,
}
if result.Order.Value.Attempt > 0 {
g.Set("attempt", clog.ParamGroup{
"current": result.Order.Value.Attempt,
"limit": maxAttempts,
})
if result.Order.Action == TrackingActionRemove {
g.Set("action", result.Order.Action)
}
}
lp.Set("order", g)
}
{
g := clog.ParamGroup{}
if result.Entity.Action == TrackingActionRemove {
g.Set("action", result.Entity.Action)
}
if g.Count() > 0 {
lp.Set("entity", g)
}
}
cr.Log(ctx, lp)
}
return
}