func (d *downloader) downloadVideoReq(ctx context.Context, video *models.VideoDLRequest, m *sync.Mutex) error {
	if strings.HasPrefix(video.VideoID, "so") {
		err := video.SetDownloadFailed()
		if err != nil {
			log.Errorf("Could not set download failed for video %s. Err: %s", video.VideoID, err)
		}
		log.Info("Video VideoID has the bad prefix so, skipping...")
		return nil
	}
	err := video.AcquireLockForVideo()
	if err != nil {
		log.Errorf("Could not acquire redis lock for video VideoID %s during download of content parent URL %s. Err: %s", video.VideoID, video.URL, video.ParentURL, err)
		return nil
	}
	website, err := models.GetWebsiteFromURL(video.URL)
	if err != nil {
		log.Errorf("Failed to extract website domain from %s", video.URL)
		return nil
	}
	videoReq := videoproto.ForeignVideoCheck{
		ForeignVideoID: video.VideoID,
		ForeignWebsite: website,  
	}
	videoExists, err := d.videoClient.ForeignVideoExists(context.TODO(), &videoReq)
	if err != nil {
		err := fmt.Errorf("could not check whether video exists for video VideoID %s. Err: %s", video.VideoID, err)
		log.Error(err)
		return nil
	}
	if videoExists.Exists {
		log.Errorf("Video VideoID %s already exists", video.VideoID)
		err = video.SetDownloadSucceeded()
		if err != nil {
			log.Errorf("Could not set download succeeded for video %s. Err: %s", video.VideoID, err)
		}
		return nil
	}
	m.Lock()
	go func() {
		time.Sleep(time.Second * 10)
		m.Unlock()
	}()
currVideoLoop:
	for currentRetryNum := 1; currentRetryNum <= d.numberOfRetries+1; currentRetryNum++ {
		select {
		case <-ctx.Done():
			log.Infof("Context done, returning from download request loop for parent url", video.ParentURL)
			return nil
		default:
		}
		switch {
		case currentRetryNum == d.numberOfRetries+1:
			log.Errorf("Failed to download %s in %d attempts", video.URL, d.numberOfRetries)
			err := video.SetDownloadFailed()
			if err != nil {
				err = video.RecordEvent(models.Error)
				if err != nil {
					log.Errorf("Could not record error event. Err: %s", err)
				}
				log.Errorf("Could not set download failed for video %s. Err: %s", video.VideoID, err)
			}
			break currVideoLoop
		case currentRetryNum > 1:
			log.Infof("Attempting to download %s, attempt %d of %d", video.URL, currentRetryNum, d.numberOfRetries)
		}
		metafile, metadata, err := d.downloadVideo(video)
		if err == nil {
			log.Infof("Download succeeded for video %s", video.VideoID)
			err = d.uploadToVideoService(context.Background(), metadata, video, metafile)
			if err != nil {
				log.Infof("failed to upload to video service. Err: %s. Continuing...", err)
				continue
			}
			err = video.SetDownloadSucceeded()
			if err != nil {
				log.Errorf("Could not set download succeeded for video %s. Err: %s", video.VideoID, err)
			}
			break
		}
		log.Errorf("Failed to download video %s. Err: %s", video.VideoID, err)
	}
	return nil
}
Deals with a particular video download request
package downloader
import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"sync"
	"time"
	"github.com/horahoradev/horahora/scheduler/internal/models"
	videoproto "github.com/horahoradev/horahora/video_service/protocol"
	log "github.com/sirupsen/logrus"
)
type downloader struct {
	downloadQueue   chan *models.VideoDLRequest
	outputLoc       string
	videoClient     videoproto.VideoServiceClient
	numberOfRetries int
	socksConnStr    string
	maxFS           uint64
}
func New(dlQueue chan *models.VideoDLRequest, outputLoc string, client videoproto.VideoServiceClient, numberOfRetries int,
	socksConnStr string, maxFS uint64) downloader {
	return downloader{
		downloadQueue:   dlQueue,
		outputLoc:       outputLoc,
		videoClient:     client,
		numberOfRetries: numberOfRetries,
		socksConnStr:    socksConnStr,
		maxFS:           maxFS,
	}
}
func (d *downloader) SubscribeAndDownload(ctx context.Context, m *sync.Mutex) error {
	for {
		select {
		case <-ctx.Done():
			log.Info("Context done, downloader returning")
			return nil
		default:
		}
		select {
		case <-ctx.Done():
			log.Info("Context done, downloader returning")
			return nil
		case r := <-d.downloadQueue:
			err := d.downloadVideoReq(ctx, r, m)
			if err != nil {
				log.Errorf("Encountered error in downloader. Err: %s. Continuing...", err)
			}
		}
	}
}