maylinejix commited on
Commit
9fa30ca
·
verified ·
1 Parent(s): 970a583

Update plugins/jobs-imggen.js

Browse files
Files changed (1) hide show
  1. plugins/jobs-imggen.js +15 -85
plugins/jobs-imggen.js CHANGED
@@ -1,4 +1,3 @@
1
- const axios = require('axios');
2
  const fs = require('fs');
3
  const path = require('path');
4
 
@@ -16,83 +15,6 @@ const saveJobs = (jobs) => {
16
  fs.writeFileSync(jobsPath, JSON.stringify(jobs, null, 2));
17
  };
18
 
19
- const checkres = async (token, iid) => {
20
- const res = await axios.get(`https://supawork.ai/supawork/headshot/api/media/aigc/result/list/v1?page_no=1&page_size=10&identity_id=${iid}`, {
21
- headers: {
22
- 'Authorization': token,
23
- 'X-Identity-Id': iid,
24
- 'X-Auth-Challenge': '',
25
- 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36'
26
- }
27
- });
28
-
29
- return res.data.data;
30
- };
31
-
32
- const processJob = async (jobId) => {
33
- const jobs = loadJobs();
34
- const job = jobs[jobId];
35
-
36
- if (!job || job.status === 'completed' || job.status === 'failed') {
37
- return;
38
- }
39
-
40
- try {
41
- console.log(`[Job ${jobId}] Checking result...`);
42
-
43
- const res = await checkres(job.token, job.identityId);
44
-
45
- if (res.list && res.list.length > 0) {
46
- const task = res.list[0];
47
- console.log(`[Job ${jobId}] Task status: ${task.status}, Image status: ${task.list[0].status}`);
48
-
49
- if (task.status === 1 && task.list[0].status === 1) {
50
- const resurl = task.list[0].url[0];
51
- console.log(`[Job ${jobId}] Image ready`);
52
-
53
- job.status = 'completed';
54
- job.result_url = resurl;
55
- job.completed_at = new Date().toISOString();
56
- saveJobs(jobs);
57
-
58
- console.log(`[Job ${jobId}] Completed successfully`);
59
-
60
- setTimeout(() => {
61
- const jobs = loadJobs();
62
- delete jobs[jobId];
63
- saveJobs(jobs);
64
- console.log(`[Job ${jobId}] Job data cleaned up`);
65
- }, 300000);
66
-
67
- return;
68
- }
69
-
70
- if (task.status === 2 || task.list[0].status === 2) {
71
- throw new Error('Generation failed');
72
- }
73
-
74
- console.log(`[Job ${jobId}] Still generating... will check again in 5s`);
75
- setTimeout(() => processJob(jobId), 5000);
76
- } else {
77
- console.log(`[Job ${jobId}] No results yet... will check again in 5s`);
78
- setTimeout(() => processJob(jobId), 5000);
79
- }
80
-
81
- } catch (error) {
82
- console.error(`[Job ${jobId}] Error:`, error.message);
83
- job.status = 'failed';
84
- job.error = error.message;
85
- job.failed_at = new Date().toISOString();
86
- saveJobs(jobs);
87
-
88
- setTimeout(() => {
89
- const jobs = loadJobs();
90
- delete jobs[jobId];
91
- saveJobs(jobs);
92
- }, 300000);
93
- }
94
- };
95
-
96
  const handler = async (req, res) => {
97
  try {
98
  const { id, key } = req.query;
@@ -111,10 +33,6 @@ const handler = async (req, res) => {
111
  });
112
  }
113
 
114
- if (job.status === 'processing' && job.stage === 'waiting_generation') {
115
- processJob(id);
116
- }
117
-
118
  const response = {
119
  author: "Herza",
120
  success: true,
@@ -123,17 +41,29 @@ const handler = async (req, res) => {
123
  status: job.status,
124
  stage: job.stage,
125
  prompt: job.prompt,
126
- ratio: job.ratio,
127
  created_at: job.created_at
128
  }
129
  };
130
 
131
  if (job.status === 'completed') {
132
- response.data.result_url = job.result_url;
133
  response.data.completed_at = job.completed_at;
 
 
 
 
 
 
 
134
  } else if (job.status === 'failed') {
135
  response.data.error = job.error;
136
  response.data.failed_at = job.failed_at;
 
 
 
 
 
 
137
  } else {
138
  response.data.message = 'Job is still processing. Check again in a few seconds.';
139
  }
@@ -151,7 +81,7 @@ const handler = async (req, res) => {
151
  };
152
 
153
  module.exports = {
154
- name: 'Job Status Checker',
155
  description: 'Check image generation job status and get result',
156
  type: 'GET',
157
  routes: ['api/job/imagegen'],
 
 
1
  const fs = require('fs');
2
  const path = require('path');
3
 
 
15
  fs.writeFileSync(jobsPath, JSON.stringify(jobs, null, 2));
16
  };
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  const handler = async (req, res) => {
19
  try {
20
  const { id, key } = req.query;
 
33
  });
34
  }
35
 
 
 
 
 
36
  const response = {
37
  author: "Herza",
38
  success: true,
 
41
  status: job.status,
42
  stage: job.stage,
43
  prompt: job.prompt,
 
44
  created_at: job.created_at
45
  }
46
  };
47
 
48
  if (job.status === 'completed') {
49
+ response.data.result = job.result;
50
  response.data.completed_at = job.completed_at;
51
+
52
+ setTimeout(() => {
53
+ const jobs = loadJobs();
54
+ delete jobs[id];
55
+ saveJobs(jobs);
56
+ console.log(`[Job ${id}] Job data cleaned up`);
57
+ }, 300000);
58
  } else if (job.status === 'failed') {
59
  response.data.error = job.error;
60
  response.data.failed_at = job.failed_at;
61
+
62
+ setTimeout(() => {
63
+ const jobs = loadJobs();
64
+ delete jobs[id];
65
+ saveJobs(jobs);
66
+ }, 300000);
67
  } else {
68
  response.data.message = 'Job is still processing. Check again in a few seconds.';
69
  }
 
81
  };
82
 
83
  module.exports = {
84
+ name: 'Check Image Generator result',
85
  description: 'Check image generation job status and get result',
86
  type: 'GET',
87
  routes: ['api/job/imagegen'],