Tranga-Website/Website/modules/Job.tsx

141 lines
5.1 KiB
TypeScript
Raw Normal View History

2024-10-18 19:45:04 +02:00
import {deleteData, getData, postData} from '../App';
import IJob from "./interfaces/IJob";
import IProgressToken from "./interfaces/IProgressToken";
export class Job
{
static IntervalStringFromDate(date: Date) : string {
return `${date.getDay()}.${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
}
2024-10-18 19:45:04 +02:00
static async GetAllJobs(): Promise<string[]> {
2024-10-19 21:08:11 +02:00
console.info("Getting all Jobs");
2024-10-18 19:45:04 +02:00
return getData("http://127.0.0.1:6531/v2/Jobs")
.then((json) => {
2024-10-19 21:08:11 +02:00
console.info("Got all Jobs");
const ret = json as string[];
console.debug(ret);
return (ret);
2024-10-18 19:45:04 +02:00
});
}
static async GetRunningJobs(): Promise<string[]> {
2024-10-19 21:08:11 +02:00
console.info("Getting all running Jobs");
2024-10-18 19:45:04 +02:00
return getData("http://127.0.0.1:6531/v2/Jobs/Running")
.then((json) => {
2024-10-19 21:08:11 +02:00
console.info("Got all running Jobs");
const ret = json as string[];
console.debug(ret);
return (ret);
2024-10-18 19:45:04 +02:00
});
}
static async GetWaitingJobs(): Promise<string[]> {
2024-10-19 21:08:11 +02:00
console.info("Getting all waiting Jobs");
2024-10-18 19:45:04 +02:00
return getData("http://127.0.0.1:6531/v2/Jobs/Waiting")
.then((json) => {
2024-10-19 21:08:11 +02:00
console.info("Got all waiting Jobs");
const ret = json as string[];
console.debug(ret);
return (ret);
});
}
static async GetStandbyJobs(): Promise<string[]> {
console.info("Getting all standby Jobs");
return getData("http://127.0.0.1:6531/v2/Jobs/Standby")
.then((json) => {
console.info("Got all standby Jobs");
const ret = json as string[];
console.debug(ret);
return (ret);
2024-10-18 19:45:04 +02:00
});
}
static async GetMonitoringJobs(): Promise<string[]> {
2024-10-19 21:08:11 +02:00
console.info("Getting all monitoring Jobs");
2024-10-18 19:45:04 +02:00
return getData("http://127.0.0.1:6531/v2/Jobs/Monitoring")
.then((json) => {
2024-10-19 21:08:11 +02:00
console.info("Got all monitoring Jobs");
const ret = json as string[];
console.debug(ret);
return (ret);
2024-10-18 19:45:04 +02:00
});
}
static async GetJob(jobId: string): Promise<IJob>{
if(jobId === undefined || jobId === null || jobId.length < 1) {
console.error(`JobId was not provided`);
return Promise.reject();
}
2024-10-19 21:08:11 +02:00
console.info(`Getting Job ${jobId}`);
2024-10-18 19:45:04 +02:00
return getData(`http://127.0.0.1:6531/v2/Job/${jobId}`)
.then((json) => {
2024-10-19 21:08:11 +02:00
console.info(`Got Job ${jobId}`);
const ret = json as IJob;
console.debug(ret);
return (ret);
2024-10-18 19:45:04 +02:00
});
}
static async GetJobs(jobIds: string[]): Promise<IJob[]> {
if(jobIds === undefined || jobIds === null || jobIds.length < 1) {
console.error(`JobIds was not provided`);
return Promise.reject();
}
let reqStr = jobIds.join(",");
2024-10-19 21:08:11 +02:00
console.info(`Getting Jobs ${reqStr}`);
2024-10-18 19:45:04 +02:00
return getData(`http://127.0.0.1:6531/v2/Job?jobIds=${reqStr}`)
.then((json) => {
2024-10-19 21:08:11 +02:00
console.info(`Got Jobs ${reqStr}`);
const ret = json as IJob[];
console.debug(ret);
return (ret);
2024-10-18 19:45:04 +02:00
});
}
static async GetJobProgress(jobId: string): Promise<IProgressToken> {
2024-10-19 21:08:11 +02:00
console.info(`Getting Job ${jobId} Progress`);
2024-10-18 19:45:04 +02:00
return getData(`http://127.0.0.1:6531/v2/Job/${jobId}/Progress`)
.then((json) => {
2024-10-19 21:08:11 +02:00
console.info(`Got Job ${jobId} Progress`);
const ret = json as IProgressToken;
console.debug(ret);
return (ret);
2024-10-18 19:45:04 +02:00
});
}
static async CreateJobDateInterval(internalId: string, jobType: string, interval: Date) : Promise<null> {
return this.CreateJob(internalId, jobType, this.IntervalStringFromDate(interval));
}
static async CreateJob(internalId: string, jobType: string, interval: string): Promise<null> {
const validate = /(?:[0-9]{1,2}\.)?[0-9]{1,2}:[0-9]{1,2}(?::[0-9]{1,2})?/
2024-10-19 21:08:11 +02:00
console.info(`Creating Job for Manga ${internalId} at ${interval} interval`);
if(!validate.test(interval)){
console.error("Interval was in incorrect format.");
return Promise.reject();
}
const data = {
2024-10-18 19:45:04 +02:00
internalId: internalId,
interval: interval
};
return postData(`http://127.0.0.1:6531/v2/Job/Create/${jobType}`, data)
.then((json) => {
2024-10-19 21:08:11 +02:00
console.info(`Created Job for Manga ${internalId} at ${interval} interval`);
return null;
2024-10-18 19:45:04 +02:00
});
}
static DeleteJob(jobId: string) : Promise<void> {
return deleteData(`http://127.0.0.1:6531/v2/Job/${jobId}`);
2024-10-18 19:45:04 +02:00
}
static StartJob(jobId: string) : Promise<object> {
return postData(`http://127.0.0.1:6531/v2/Job/${jobId}/StartNow`, {});
2024-10-18 19:45:04 +02:00
}
static CancelJob(jobId: string) : Promise<object> {
return postData(`http://127.0.0.1:6531/v2/Job/${jobId}/Cancel`, {});
2024-10-18 19:45:04 +02:00
}
}