(jobId, updatedFields)
| 937 | } |
| 938 | |
| 939 | async updateJob(jobId, updatedFields) { |
| 940 | logger.debug({ jobId, status: updatedFields.status }, 'updateJob called'); |
| 941 | if (updatedFields.data && updatedFields.data.videos) { |
| 942 | logger.debug({ jobId, videoCount: updatedFields.data.videos.length }, 'updateJob data contains videos'); |
| 943 | } |
| 944 | |
| 945 | const job = this.jobs[jobId]; |
| 946 | if (!job) { |
| 947 | logger.warn('Job to update did not exist!'); |
| 948 | return; |
| 949 | } |
| 950 | |
| 951 | // Non-download jobs (e.g. Import Subscriptions) manage their own output field. |
| 952 | const jobIsDownload = isDownloadJob(job.jobType); |
| 953 | |
| 954 | if ( |
| 955 | jobIsDownload && ( |
| 956 | updatedFields.status === 'Complete' || |
| 957 | updatedFields.status === 'Error' || |
| 958 | updatedFields.status === 'Complete with Warnings' || |
| 959 | updatedFields.status === 'Terminated' |
| 960 | ) |
| 961 | ) { |
| 962 | // downloadModule already sends proper completion messages with finalSummary |
| 963 | // Only send the downloadComplete event for backwards compatibility |
| 964 | MessageEmitter.emitMessage( |
| 965 | 'broadcast', |
| 966 | null, |
| 967 | 'download', |
| 968 | 'downloadComplete', |
| 969 | { text: 'Download job completed.', videos: updatedFields.data?.videos || [] } |
| 970 | ); |
| 971 | |
| 972 | // Only modify output and status for actual completions, not terminations |
| 973 | if (updatedFields.status !== 'Terminated') { |
| 974 | let numVideos = updatedFields.data?.videos?.length || 0; |
| 975 | updatedFields.output = numVideos + ' videos.'; |
| 976 | if (updatedFields.status !== 'Complete with Warnings') { |
| 977 | updatedFields.status = 'Complete'; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | // Update in-memory job |
| 983 | for (let field in updatedFields) { |
| 984 | job[field] = updatedFields[field]; |
| 985 | } |
| 986 | |
| 987 | // Save only THIS job to DB, don't iterate through all jobs |
| 988 | const isCompletedJob = updatedFields.status === 'Complete' || |
| 989 | updatedFields.status === 'Complete with Warnings' || |
| 990 | updatedFields.status === 'Error' || |
| 991 | updatedFields.status === 'Terminated' || |
| 992 | updatedFields.status === 'Killed'; |
| 993 | |
| 994 | if (isCompletedJob && jobIsDownload) { |
| 995 | // For completed download jobs, reload videos from DB to ensure accurate counts |
| 996 | // This is especially important for multi-group downloads where each group |
no test coverage detected