Skip to content

Commit

Permalink
fix http get call and error handling in job queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Southclaws committed Nov 22, 2024
1 parent 4c6f1a9 commit f9f46b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
13 changes: 12 additions & 1 deletion app/services/asset/analyse_job/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package analyse_job
import (
"context"
"net/http"
"time"

"github.com/Southclaws/fault"
"github.com/Southclaws/fault/fctx"
Expand All @@ -16,11 +17,21 @@ import (
)

func (c *analyseConsumer) downloadAsset(ctx context.Context, src string, fillrule opt.Optional[asset.ContentFillCommand]) error {
resp, err := http.Get(src)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, src, nil)
if err != nil {
return fault.Wrap(err, fctx.With(ctx))
}

client := &http.Client{
Timeout: 30 * time.Second,
}

resp, err := client.Do(req)
if err != nil {
return fault.Wrap(err, fctx.With(ctx))
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
ctx = fctx.WithMeta(ctx, "status", resp.Status)
return fault.Wrap(fault.New("failed to get asset"), fctx.With(ctx))
Expand Down
8 changes: 4 additions & 4 deletions app/services/asset/analyse_job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func runAnalyseConsumer(
lc.Append(fx.StartHook(func(_ context.Context) error {
analyseChan, err := analyseQueue.Subscribe(ctx)
if err != nil {
panic(err)
return err
}

go func() {
for msg := range analyseChan {
nctx := session.GetSessionFromMessage(ctx, msg)

if err := consumer.analyseAsset(nctx, msg.Payload.AssetID, msg.Payload.ContentFillRule); err != nil {
l.Error("failed to index node", zap.Error(err))
l.Error("failed to analyse asset", zap.Error(err))
}

msg.Ack()
Expand All @@ -40,15 +40,15 @@ func runAnalyseConsumer(

downloadChan, err := downloadQueue.Subscribe(ctx)
if err != nil {
panic(err)
return err
}

go func() {
for msg := range downloadChan {
nctx := session.GetSessionFromMessage(ctx, msg)

if err := consumer.downloadAsset(nctx, msg.Payload.URL, msg.Payload.ContentFillRule); err != nil {
l.Error("failed to index node", zap.Error(err))
l.Error("failed to download asset", zap.Error(err))
}

msg.Ack()
Expand Down

0 comments on commit f9f46b8

Please sign in to comment.