@@ -10,7 +10,6 @@ import (
1010 "github.com/webhookx-io/webhookx/db/entities"
1111 "github.com/webhookx-io/webhookx/eventbus"
1212 "github.com/webhookx-io/webhookx/mcache"
13- "github.com/webhookx-io/webhookx/model"
1413 "github.com/webhookx-io/webhookx/pkg/metrics"
1514 "github.com/webhookx-io/webhookx/pkg/plugin"
1615 plugintypes "github.com/webhookx-io/webhookx/pkg/plugin/types"
@@ -127,7 +126,7 @@ func (w *Worker) run() {
127126 defer span .End ()
128127 ctx = tracingCtx
129128 }
130- task .Data = & model. MessageData {}
129+ task .Data = & MessageData {}
131130 err = task .UnmarshalData (task .Data )
132131 if err != nil {
133132 w .log .Errorf ("[worker] failed to unmarshal task: %v" , err )
@@ -195,13 +194,19 @@ func (w *Worker) processRequeue() {
195194
196195 tasks := make ([]* taskqueue.TaskMessage , 0 , len (attempts ))
197196 for _ , attempt := range attempts {
197+ event , err := w .DB .Events .Get (ctx , attempt .EventId )
198+ if err != nil {
199+ w .log .Errorf ("[worker] failed to get event: %v" , err )
200+ break
201+ }
198202 task := & taskqueue.TaskMessage {
199203 ID : attempt .ID ,
200204 ScheduledAt : attempt .ScheduledAt .Time ,
201- Data : & model. MessageData {
205+ Data : & MessageData {
202206 EventID : attempt .EventId ,
203207 EndpointId : attempt .EndpointId ,
204208 Attempt : attempt .AttemptNumber ,
209+ Event : string (event .Data ),
205210 },
206211 }
207212 tasks = append (tasks , task )
@@ -232,7 +237,7 @@ func (w *Worker) handleTask(ctx context.Context, task *taskqueue.TaskMessage) er
232237 defer span .End ()
233238 ctx = tracingCtx
234239 }
235- data := task .Data .(* model. MessageData )
240+ data := task .Data .(* MessageData )
236241
237242 // verify endpoint
238243 cacheKey := constants .EndpointCacheKey .Build (data .EndpointId )
@@ -247,15 +252,18 @@ func (w *Worker) handleTask(ctx context.Context, task *taskqueue.TaskMessage) er
247252 return w .DB .Attempts .UpdateErrorCode (ctx , task .ID , entities .AttemptStatusCanceled , entities .AttemptErrorCodeEndpointDisabled )
248253 }
249254
250- // verify event
251- cacheKey = constants .EventCacheKey .Build (data .EventID )
252- opts := & mcache.LoadOptions {DisableLRU : true }
253- event , err := mcache .Load (ctx , cacheKey , opts , w .DB .Events .Get , data .EventID )
254- if err != nil {
255- return err
256- }
257- if event == nil {
258- return w .DB .Attempts .UpdateErrorCode (ctx , task .ID , entities .AttemptStatusCanceled , entities .AttemptErrorCodeUnknown )
255+ if data .Event == "" { // backward compatibility
256+ // verify event
257+ cacheKey = constants .EventCacheKey .Build (data .EventID )
258+ opts := & mcache.LoadOptions {DisableLRU : true }
259+ event , err := mcache .Load (ctx , cacheKey , opts , w .DB .Events .Get , data .EventID )
260+ if err != nil {
261+ return err
262+ }
263+ if event == nil {
264+ return w .DB .Attempts .UpdateErrorCode (ctx , task .ID , entities .AttemptStatusCanceled , entities .AttemptErrorCodeUnknown )
265+ }
266+ data .Event = string (event .Data )
259267 }
260268
261269 plugins , err := listEndpointPlugins (ctx , w .DB , endpoint .ID )
@@ -273,7 +281,7 @@ func (w *Worker) handleTask(ctx context.Context, task *taskqueue.TaskMessage) er
273281 URL : endpoint .Request .URL ,
274282 Method : endpoint .Request .Method ,
275283 Headers : endpoint .Request .Headers ,
276- Payload : event . Data ,
284+ Payload : [] byte ( data . Event ) ,
277285 }
278286 if pluginReq .Headers == nil {
279287 pluginReq .Headers = make (map [string ]string )
@@ -326,22 +334,24 @@ func (w *Worker) handleTask(ctx context.Context, task *taskqueue.TaskMessage) er
326334 return err
327335 }
328336
329- attemptDetail := & entities.AttemptDetail {
330- ID : task .ID ,
331- RequestHeaders : utils .HeaderMap (request .Request .Header ),
332- RequestBody : utils .Pointer (string (request .Payload )),
333- }
334- if len (response .Header ) > 0 {
335- attemptDetail .ResponseHeaders = utils .Pointer (entities .Headers (utils .HeaderMap (response .Header )))
336- }
337- if response .ResponseBody != nil {
338- attemptDetail .ResponseBody = utils .Pointer (string (response .ResponseBody ))
339- }
340- attemptDetail .WorkspaceId = endpoint .WorkspaceId
341- err = w .DB .AttemptDetails .Insert (ctx , attemptDetail )
342- if err != nil {
343- return err
344- }
337+ go func () {
338+ attemptDetail := & entities.AttemptDetail {
339+ ID : task .ID ,
340+ RequestHeaders : utils .HeaderMap (request .Request .Header ),
341+ RequestBody : utils .Pointer (string (request .Payload )),
342+ }
343+ if len (response .Header ) > 0 {
344+ attemptDetail .ResponseHeaders = utils .Pointer (entities .Headers (utils .HeaderMap (response .Header )))
345+ }
346+ if response .ResponseBody != nil {
347+ attemptDetail .ResponseBody = utils .Pointer (string (response .ResponseBody ))
348+ }
349+ attemptDetail .WorkspaceId = endpoint .WorkspaceId
350+ err = w .DB .AttemptDetails .Insert (ctx , attemptDetail )
351+ if err != nil {
352+ w .log .Errorf ("[worker] failed to insert attempt detail: %v" , err )
353+ }
354+ }()
345355
346356 if result .Status == entities .AttemptStatusSuccess {
347357 return nil
@@ -355,7 +365,7 @@ func (w *Worker) handleTask(ctx context.Context, task *taskqueue.TaskMessage) er
355365 delay := endpoint .Retry .Config .Attempts [data .Attempt ]
356366 nextAttempt := & entities.Attempt {
357367 ID : utils .KSUID (),
358- EventId : event . ID ,
368+ EventId : data . EventID ,
359369 EndpointId : endpoint .ID ,
360370 Status : entities .AttemptStatusInit ,
361371 AttemptNumber : data .Attempt + 1 ,
@@ -372,7 +382,7 @@ func (w *Worker) handleTask(ctx context.Context, task *taskqueue.TaskMessage) er
372382 task = & taskqueue.TaskMessage {
373383 ID : nextAttempt .ID ,
374384 ScheduledAt : nextAttempt .ScheduledAt .Time ,
375- Data : & model. MessageData {
385+ Data : & MessageData {
376386 EventID : data .EventID ,
377387 EndpointId : data .EndpointId ,
378388 Attempt : nextAttempt .AttemptNumber ,
0 commit comments