diff options
Diffstat (limited to 'internal/media')
| -rw-r--r-- | internal/media/processingemoji.go | 70 | ||||
| -rw-r--r-- | internal/media/processingmedia.go | 82 | 
2 files changed, 63 insertions, 89 deletions
| diff --git a/internal/media/processingemoji.go b/internal/media/processingemoji.go index f4265759b..b4e96a946 100644 --- a/internal/media/processingemoji.go +++ b/internal/media/processingemoji.go @@ -22,7 +22,6 @@ import (  	errorsv2 "codeberg.org/gruf/go-errors/v2"  	"codeberg.org/gruf/go-runners" -	"github.com/superseriousbusiness/gotosocial/internal/gtscontext"  	"github.com/superseriousbusiness/gotosocial/internal/gtserror"  	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"  	"github.com/superseriousbusiness/gotosocial/internal/log" @@ -77,42 +76,34 @@ func (p *ProcessingEmoji) load(ctx context.Context) (  		defer func() {  			// This is only done when ctx NOT cancelled. -			done = (err == nil || !errorsv2.IsV2(err, +			if done = (err == nil || !errorsv2.IsV2(err,  				context.Canceled,  				context.DeadlineExceeded, -			)) - -			if !done { -				return -			} - -			// Anything from here, we -			// need to ensure happens -			// (i.e. no ctx canceled). -			ctx = gtscontext.WithValues( -				context.Background(), -				ctx, // values -			) - -			// On error, clean -			// downloaded files. -			if err != nil { -				p.cleanup(ctx) -			} - -			if !done { -				return +			)); done { +				// Processing finished, +				// whether error or not! + +				// Anything from here, we +				// need to ensure happens +				// (i.e. no ctx canceled). +				ctx = context.WithoutCancel(ctx) + +				// On error, clean +				// downloaded files. +				if err != nil { +					p.cleanup(ctx) +				} + +				// Update with latest details, whatever happened. +				e := p.mgr.state.DB.UpdateEmoji(ctx, p.emoji) +				if e != nil { +					log.Errorf(ctx, "error updating emoji in db: %v", e) +				} + +				// Store values. +				p.done = true +				p.err = err  			} - -			// Update with latest details, whatever happened. -			e := p.mgr.state.DB.UpdateEmoji(ctx, p.emoji) -			if e != nil { -				log.Errorf(ctx, "error updating emoji in db: %v", e) -			} - -			// Store final values. -			p.done = true -			p.err = err  		}()  		// Attempt to store media and calculate @@ -122,7 +113,10 @@ func (p *ProcessingEmoji) load(ctx context.Context) (  		err = p.store(ctx)  		return err  	}) -	emoji = p.emoji + +	// Return a copy of emoji. +	emoji = new(gtsmodel.Emoji) +	*emoji = *p.emoji  	return  } @@ -265,11 +259,11 @@ func (p *ProcessingEmoji) store(ctx context.Context) error {  // cleanup will remove any traces of processing emoji from storage,  // and perform any other necessary cleanup steps after failure.  func (p *ProcessingEmoji) cleanup(ctx context.Context) { -	var err error +	log.Debugf(ctx, "running cleanup of emoji %s", p.emoji.ID)  	if p.emoji.ImagePath != "" {  		// Ensure emoji file at path is deleted from storage. -		err = p.mgr.state.Storage.Delete(ctx, p.emoji.ImagePath) +		err := p.mgr.state.Storage.Delete(ctx, p.emoji.ImagePath)  		if err != nil && !storage.IsNotFound(err) {  			log.Errorf(ctx, "error deleting %s: %v", p.emoji.ImagePath, err)  		} @@ -277,7 +271,7 @@ func (p *ProcessingEmoji) cleanup(ctx context.Context) {  	if p.emoji.ImageStaticPath != "" {  		// Ensure emoji static file at path is deleted from storage. -		err = p.mgr.state.Storage.Delete(ctx, p.emoji.ImageStaticPath) +		err := p.mgr.state.Storage.Delete(ctx, p.emoji.ImageStaticPath)  		if err != nil && !storage.IsNotFound(err) {  			log.Errorf(ctx, "error deleting %s: %v", p.emoji.ImageStaticPath, err)  		} diff --git a/internal/media/processingmedia.go b/internal/media/processingmedia.go index 81dde7bdc..a5c60900b 100644 --- a/internal/media/processingmedia.go +++ b/internal/media/processingmedia.go @@ -19,12 +19,10 @@ package media  import (  	"context" -	"time"  	errorsv2 "codeberg.org/gruf/go-errors/v2"  	"codeberg.org/gruf/go-runners" -	"github.com/superseriousbusiness/gotosocial/internal/gtscontext"  	"github.com/superseriousbusiness/gotosocial/internal/gtserror"  	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"  	"github.com/superseriousbusiness/gotosocial/internal/log" @@ -63,6 +61,7 @@ func (p *ProcessingMedia) Load(ctx context.Context) (*gtsmodel.MediaAttachment,  	media, done, err := p.load(ctx)  	if !done {  		// On a context-canceled error (marked as !done), requeue for loading. +		log.Warnf(ctx, "reprocessing media %s after canceled ctx", p.media.ID)  		p.mgr.state.Workers.Dereference.Queue.Push(func(ctx context.Context) {  			if _, _, err := p.load(ctx); err != nil {  				log.Errorf(ctx, "error loading media: %v", err) @@ -86,55 +85,35 @@ func (p *ProcessingMedia) load(ctx context.Context) (  		defer func() {  			// This is only done when ctx NOT cancelled. -			done = (err == nil || !errorsv2.IsV2(err, +			if done = (err == nil || !errorsv2.IsV2(err,  				context.Canceled,  				context.DeadlineExceeded, -			)) - -			if !done { -				return -			} - -			// Anything from here, we -			// need to ensure happens -			// (i.e. no ctx canceled). -			ctx = gtscontext.WithValues( -				context.Background(), -				ctx, // values -			) - -			// On error or unknown media types, perform error cleanup. -			if err != nil || p.media.Type == gtsmodel.FileTypeUnknown { -				p.cleanup(ctx) -			} - -			// Update with latest details, whatever happened. -			e := p.mgr.state.DB.UpdateAttachment(ctx, p.media) -			if e != nil { -				log.Errorf(ctx, "error updating media in db: %v", e) +			)); done { +				// Processing finished, +				// whether error or not! + +				// Anything from here, we +				// need to ensure happens +				// (i.e. no ctx canceled). +				ctx = context.WithoutCancel(ctx) + +				// On error or unknown media types, perform error cleanup. +				if err != nil || p.media.Type == gtsmodel.FileTypeUnknown { +					p.cleanup(ctx) +				} + +				// Update with latest details, whatever happened. +				e := p.mgr.state.DB.UpdateAttachment(ctx, p.media) +				if e != nil { +					log.Errorf(ctx, "error updating media in db: %v", e) +				} + +				// Store values. +				p.done = true +				p.err = err  			} - -			// Store final values. -			p.done = true -			p.err = err  		}() -		// TODO: in time update this -		// to perhaps follow a similar -		// freshness window to statuses -		// / accounts? But that's a big -		// maybe, media don't change in -		// the same way so this is largely -		// just to slow down fail retries. -		const maxfreq = 6 * time.Hour - -		// Check whether media is uncached but repeatedly failing, -		// specifically limit the frequency at which we allow this. -		if !p.media.UpdatedAt.Equal(p.media.CreatedAt) && // i.e. not new -			p.media.UpdatedAt.Add(maxfreq).Before(time.Now()) { -			return nil -		} -  		// Attempt to store media and calculate  		// full-size media attachment details.  		// @@ -142,7 +121,10 @@ func (p *ProcessingMedia) load(ctx context.Context) (  		err = p.store(ctx)  		return err  	}) -	media = p.media + +	// Return a copy of media attachment. +	media = new(gtsmodel.MediaAttachment) +	*media = *p.media  	return  } @@ -331,11 +313,9 @@ func (p *ProcessingMedia) store(ctx context.Context) error {  // cleanup will remove any traces of processing media from storage.  // and perform any other necessary cleanup steps after failure.  func (p *ProcessingMedia) cleanup(ctx context.Context) { -	var err error -  	if p.media.File.Path != "" {  		// Ensure media file at path is deleted from storage. -		err = p.mgr.state.Storage.Delete(ctx, p.media.File.Path) +		err := p.mgr.state.Storage.Delete(ctx, p.media.File.Path)  		if err != nil && !storage.IsNotFound(err) {  			log.Errorf(ctx, "error deleting %s: %v", p.media.File.Path, err)  		} @@ -343,7 +323,7 @@ func (p *ProcessingMedia) cleanup(ctx context.Context) {  	if p.media.Thumbnail.Path != "" {  		// Ensure media thumbnail at path is deleted from storage. -		err = p.mgr.state.Storage.Delete(ctx, p.media.Thumbnail.Path) +		err := p.mgr.state.Storage.Delete(ctx, p.media.Thumbnail.Path)  		if err != nil && !storage.IsNotFound(err) {  			log.Errorf(ctx, "error deleting %s: %v", p.media.Thumbnail.Path, err)  		} | 
