File size: 2,475 Bytes
1cea837
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
import { getHuggingFaceSpaceStatus } from "./getHuggingFaceSpaceStatus"
import { sleep } from "./sleep"


export async function makeSureSpaceIsRunning({
  space,
  maxWaitTimeInSec = 15 * 60, // some spaces are ultra slow to cold boot (eg. data dl at runtime)
  statusUpdateFrequencyInSec = 5,
  // userName,
  // spaceName,
}: {
  space?: string // a joined "user_name/space_name"

  maxWaitTimeInSec?: number

  statusUpdateFrequencyInSec?: number

  // userName: string
  // spaceName: string
}): Promise<void> {
  if (!space) { return }
  
  // process.stdout.write(`trying to restart space "${space}"`)
  try {
    const { runtime: { stage } } = await getHuggingFaceSpaceStatus({ space })
    if (stage === "RUNNING") {
      // process.stdout.write(`: well, it is already ${stage}!\n`)
      return
    }
  } catch (err) {
  }

  const res = await fetch(`https://huggingface.co/api/spaces/${space}/restart`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.ADMIN_HUGGING_FACE_API_TOKEN || ""}`
    }
  })

  if (res.status !== 200) {
    process.stdout.write(`failure!\nwe couldn't trigger the restart of space "${space}"\n`)
  
    throw new Error(`failed to trigger the restart of space "${space}" (status is not 200)`)
  }

  let elapsedTime = 0

  process.stdout.write(`trying to restart space "${space}"`)

  while (true) {
    process.stdout.write(".")
    const { runtime: { stage } } = await getHuggingFaceSpaceStatus({ space })

    if (stage === "RUNNING") {
      process.stdout.write(`success!\nspace "${space}" is ${stage} (took ${elapsedTime} sec)\n`)
      return
    } else if (stage === "BUILDING" || stage === "RUNNING_BUILDING") {
      // let's wait more
      await sleep(statusUpdateFrequencyInSec * 1000)

      elapsedTime += statusUpdateFrequencyInSec

      if (elapsedTime >= maxWaitTimeInSec) {
        process.stdout.write(`failure!\nspace "${space}" is still ${stage} (after ${elapsedTime} sec)\n`)
        if (stage === "BUILDING") {
          throw new Error(`failed to start space ${space} (reason: space is ${stage}, but we reached the ${maxWaitTimeInSec} sec timeout)`)
        } else {
          // if we are "RUNNING_BUILDING" we assume it is.. okay? I guess?
          return
        }
      }
    } else {
      process.stdout.write(`failure!\nspace "${space}" is ${stage} (after ${elapsedTime} sec)\n`)
      throw new Error(`failed to build space ${space} (reason: space is ${stage})`)
    }
  }
}