aseli commited on
Commit
965abdd
1 Parent(s): d813b2f

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +16 -1
index.js CHANGED
@@ -207,13 +207,28 @@ async function processScreenshot(req, res, {
207
  }
208
 
209
  const PORT = process.env.PORT || 7860
 
 
 
 
 
 
 
 
210
  function keepAlive() {
211
  var url = `https://${process.env.SPACE_HOST}`
212
  if (/(\/\/|\.)undefined\./.test(url)) return
213
  setInterval(() => {
214
- fetch(url, { headers: { Authorization: "Bearer " + process.env.HF_TOKEN}}).catch(console.error)
 
 
 
 
 
 
215
  }, 5 * 1000 * 60)
216
  }
 
217
  app.listen(PORT, async () => {
218
  console.log('App running on port', PORT)
219
  console.log(fetch)
 
207
  }
208
 
209
  const PORT = process.env.PORT || 7860
210
+ function restartSpace() {
211
+ const { SPACE_ID, HF_TOKEN } = process.env
212
+ return fetch(`https://huggingface.co/api/spaces/${SPACE_ID}/restart`, {
213
+ method: 'POST',
214
+ headers: { Authorization: `Bearer ${HF_TOKEN}` }
215
+ })
216
+ }
217
+
218
  function keepAlive() {
219
  var url = `https://${process.env.SPACE_HOST}`
220
  if (/(\/\/|\.)undefined\./.test(url)) return
221
  setInterval(() => {
222
+ fetch(url, { headers: { Authorization: "Bearer " + process.env.HF_TOKEN}})
223
+ .then(response => {
224
+ if (!response.ok) {
225
+ restartSpace().catch(console.error)
226
+ }
227
+ })
228
+ .catch(console.error)
229
  }, 5 * 1000 * 60)
230
  }
231
+
232
  app.listen(PORT, async () => {
233
  console.log('App running on port', PORT)
234
  console.log(fetch)