File size: 2,232 Bytes
e4e0e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccd48b8
 
 
e4e0e54
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
import { Video } from "../types.mts"
import { deleteVideo } from "./deleteVideo.mts"
import { getVideoStatus } from "./getVideoStatus.mts"
import { saveCompletedVideo } from "./saveCompletedVideo.mts"
import { updatePendingVideo } from "./updatePendingVideo.mts"

export const saveAndCheckIfNeedToStop = async (video: Video): Promise<boolean> => {
  
  const status = await getVideoStatus(video)
  const isToDelete =  status === "delete"
  const isToAbort = status === "abort"
  const isToPause = status === "pause"

  // well, normally no other process is supported to mark a video as "completed"
  // while we are busy processing it
  // but maybe in the future, we can afford to waste procesing power to do the "who goes faster"..?
  // const isCompleted = status === "completed"
  
  const mustStop = isToAbort || isToPause || isToDelete

  // deletion is the most priority check, as we just need to ignore all the rest
  if (isToDelete) {
    await deleteVideo(video.ownerId, video.id)
    return mustStop
  }

  // then we give priority to the pending video: maybe it is done?
  if (video.completed) {
    console.log(`video ${video.id} is completed!`)
    video.progressPercent = 100
    video.completedAt = new Date().toISOString()
    video.status = "completed"
    await updatePendingVideo(video)
    await saveCompletedVideo(video)
    return mustStop
  }


  if (isToPause) {
    console.log(`we've been requested to pause the video`)
    video.status = "pause"
    await updatePendingVideo(video)
    return mustStop
  }
  
  if (isToAbort) {
    console.log(`we've been requested to cancel the video`)
    
    // we are not going to update the percentage, because we want to keep the
    // info that we aborted mid-course
    video.completed = true

    // watch what we do here: we mark the video as completed
    // that's because "abort" is a temporary status
    video.status = "completed"

    video.completedAt = new Date().toISOString()
    await updatePendingVideo(video)
    await saveCompletedVideo(video)

    return mustStop
  }

  await updatePendingVideo(video)
  
  // if we return "true", it means we will yield, which can be an interesting thing
  // for us, to increase parallelism
  return true
}