Skip to content

Commit

Permalink
rename job class
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBrax committed Apr 26, 2022
1 parent bbabc94 commit b065a8b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 43 deletions.
6 changes: 3 additions & 3 deletions server/src/Controllers/Jobs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TwitchAutomatorJob } from "../Core/TwitchAutomatorJob";
import { Job } from "../Core/Job";
import express from "express";
import { ApiErrorResponse, ApiJobsResponse } from "../../../common/Api/Api";

export async function ListJobs(req: express.Request, res: express.Response): Promise<void> {

const jobs = TwitchAutomatorJob.jobs;
const jobs = Job.jobs;

for (const job of jobs) {
await job.getStatus();
Expand All @@ -19,7 +19,7 @@ export async function ListJobs(req: express.Request, res: express.Response): Pro

export async function KillJob(req: express.Request, res: express.Response): Promise<void> {

const job = TwitchAutomatorJob.jobs.find(j => j.name === req.params.name);
const job = Job.jobs.find(j => j.name === req.params.name);
const clear = req.query.clear;

if (!job) {
Expand Down
12 changes: 6 additions & 6 deletions server/src/Core/Automator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EventSubResponse } from "../../../common/TwitchAPI/EventSub";
import { ChannelUpdateEvent } from "../../../common/TwitchAPI/EventSub/ChannelUpdate";
import { AppRoot, BaseConfigDataFolder } from "./BaseConfig";
import { KeyValue } from "./KeyValue";
import { TwitchAutomatorJob } from "./TwitchAutomatorJob";
import { Job } from "./Job";
import { TwitchChannel } from "./TwitchChannel";
import { Config } from "./Config";
import { Helper } from "./Helper";
Expand Down Expand Up @@ -51,8 +51,8 @@ export class Automator {
converted_filename = "";
chat_filename = "";

captureJob: TwitchAutomatorJob | undefined;
chatJob: TwitchAutomatorJob | undefined;
captureJob: Job | undefined;
chatJob: Job | undefined;

public basename() {
return `${this.getLogin()}_${replaceAll(this.getStartDate(), ":", "_")}_${this.getVodID()}`; // @todo: replaceAll
Expand Down Expand Up @@ -538,7 +538,7 @@ export class Automator {
}

// if running
const job = TwitchAutomatorJob.findJob(`capture_${basename}`);
const job = Job.findJob(`capture_${basename}`);
if (job && job.getStatus()) {
const meta = job.metadata as {
login: string;
Expand Down Expand Up @@ -860,12 +860,12 @@ export class Automator {
});

// make job for capture
let capture_job: TwitchAutomatorJob;
let capture_job: Job;
const jobName = `capture_${basename}`;

if (capture_process.pid) {
Log.logAdvanced(LOGLEVEL.SUCCESS, "automator", `Spawned process ${capture_process.pid} for ${jobName}`);
capture_job = TwitchAutomatorJob.create(jobName);
capture_job = Job.create(jobName);
capture_job.setPid(capture_process.pid);
capture_job.setProcess(capture_process);
capture_job.startLog(jobName, `$ ${bin} ${cmd.join(" ")}\n`);
Expand Down
4 changes: 2 additions & 2 deletions server/src/Core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Helper } from "./Helper";
import { KeyValue } from "./KeyValue";
import { Log, LOGLEVEL } from "./Log";
import { Scheduler } from "./Scheduler";
import { TwitchAutomatorJob } from "./TwitchAutomatorJob";
import { Job } from "./Job";
import { TwitchChannel } from "./TwitchChannel";
import { TwitchGame } from "./TwitchGame";
import { TwitchVOD } from "./TwitchVOD";
Expand Down Expand Up @@ -528,7 +528,7 @@ export class Config {
TwitchChannel.loadChannelsConfig();
TwitchChannel.loadChannelsCache();
await TwitchChannel.loadChannels();
TwitchAutomatorJob.loadJobsFromCache();
Job.loadJobsFromCache();

Config.getInstance().startWatchingConfig();

Expand Down
30 changes: 15 additions & 15 deletions server/src/Core/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { FFProbe } from "../../../common/FFProbe";
import { EventSubTypes } from "../../../common/TwitchAPI/Shared";
import { Subscriptions } from "../../../common/TwitchAPI/Subscriptions";
import { BaseConfigDataFolder } from "./BaseConfig";
import { TwitchAutomatorJob } from "./TwitchAutomatorJob";
import { Job } from "./Job";
import { Config } from "./Config";
import { LOGLEVEL, Log } from "./Log";
import { TwitchCommentDump } from "../../../common/Comments";
Expand Down Expand Up @@ -158,15 +158,15 @@ export class Helper {
const num = parseInt(match[1]);
const unit = match[2];
switch (unit) {
case "h":
seconds += num * 3600;
break;
case "m":
seconds += num * 60;
break;
case "s":
seconds += num;
break;
case "h":
seconds += num * 3600;
break;
case "m":
seconds += num * 60;
break;
case "s":
seconds += num;
break;
}
}
return seconds;
Expand Down Expand Up @@ -413,11 +413,11 @@ export class Helper {

Log.logAdvanced(LOGLEVEL.INFO, jobName, `Executing ${bin} ${args.join(" ")}`);

let job: TwitchAutomatorJob;
let job: Job;

if (process.pid) {
Log.logAdvanced(LOGLEVEL.SUCCESS, "helper", `Spawned process ${process.pid} for ${jobName}`);
job = TwitchAutomatorJob.create(jobName);
job = Job.create(jobName);
job.setPid(process.pid);
job.setProcess(process);
job.startLog(jobName, `$ ${bin} ${args.join(" ")}\n`);
Expand Down Expand Up @@ -464,7 +464,7 @@ export class Helper {
});
}

static startJob(jobName: string, bin: string, args: string[], env: Record<string, string> = {}): TwitchAutomatorJob | false {
static startJob(jobName: string, bin: string, args: string[], env: Record<string, string> = {}): Job | false {

const process = spawn(bin, args || [], {
// detached: true,
Expand All @@ -474,11 +474,11 @@ export class Helper {

Log.logAdvanced(LOGLEVEL.INFO, "helper", `Executing ${bin} ${args.join(" ")}`);

let job: TwitchAutomatorJob | false = false;
let job: Job | false = false;

if (process.pid) {
Log.logAdvanced(LOGLEVEL.SUCCESS, "helper", `Spawned process ${process.pid} for ${jobName}`);
job = TwitchAutomatorJob.create(jobName);
job = Job.create(jobName);
job.setPid(process.pid);
job.setProcess(process);
job.setMetadata({
Expand Down
26 changes: 13 additions & 13 deletions server/src/Core/TwitchAutomatorJob.ts → server/src/Core/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export interface TwitchAutomatorJobJSON {
dt_started_at: string;
}

export class TwitchAutomatorJob extends EventEmitter {
export class Job extends EventEmitter {

static jobs: TwitchAutomatorJob[] = [];
static jobs: Job[] = [];
static pidstatus: Record<number, boolean> = {};

static readonly NO_FILE = 1;
Expand Down Expand Up @@ -64,7 +64,7 @@ export class TwitchAutomatorJob extends EventEmitter {
public static loadJobsFromCache() {
const jobs = fs.readdirSync(BaseConfigDataFolder.pids).filter(f => f.endsWith(".json"));
for (const job_data of jobs) {
TwitchAutomatorJob.load(job_data.replace(".json", ""));
Job.load(job_data.replace(".json", ""));
}
Log.logAdvanced(LOGLEVEL.INFO, "job", `Loaded ${jobs.length} jobs from cache`);

Expand All @@ -86,7 +86,7 @@ export class TwitchAutomatorJob extends EventEmitter {
}
}

public static create(name: string): TwitchAutomatorJob {
public static create(name: string): Job {

const basepath = BaseConfigDataFolder.pids;

Expand All @@ -107,7 +107,7 @@ export class TwitchAutomatorJob extends EventEmitter {
return job;
}

public static load(name: string): TwitchAutomatorJob | false {
public static load(name: string): Job | false {

Log.logAdvanced(LOGLEVEL.DEBUG, "job", `Loading job ${name}`);

Expand Down Expand Up @@ -149,8 +149,8 @@ export class TwitchAutomatorJob extends EventEmitter {

// TwitchLog.logAdvanced(LOGLEVEL.DEBUG, "job", "Job {$this->name} loaded, proceed to get status.", $this->metadata);

if (!TwitchAutomatorJob.jobs.includes(job)) {
TwitchAutomatorJob.jobs.push(job);
if (!Job.jobs.includes(job)) {
Job.jobs.push(job);
Log.logAdvanced(LOGLEVEL.DEBUG, "job", `Loaded job ${job.name} added to jobs list`, job.metadata);
}

Expand All @@ -163,7 +163,7 @@ export class TwitchAutomatorJob extends EventEmitter {
return this.jobs.some(job => job.name === name);
}

public static findJob(search: string): TwitchAutomatorJob | false {
public static findJob(search: string): Job | false {
const job = this.jobs.find(job => job.name?.includes(search));
if (job) {
return job;
Expand Down Expand Up @@ -209,8 +209,8 @@ export class TwitchAutomatorJob extends EventEmitter {

Log.logAdvanced(LOGLEVEL.DEBUG, "job", `Job ${this.name} ${exists ? "saved" : "failed to save"}`, this.metadata);

if (exists && !TwitchAutomatorJob.jobs.includes(this)) {
TwitchAutomatorJob.jobs.push(this);
if (exists && !Job.jobs.includes(this)) {
Job.jobs.push(this);
Log.logAdvanced(LOGLEVEL.DEBUG, "job", `New job ${this.name} added to jobs list`, this.metadata);
}

Expand Down Expand Up @@ -247,8 +247,8 @@ export class TwitchAutomatorJob extends EventEmitter {
// return !fs.existsSync(this.pidfile);
}

if (TwitchAutomatorJob.hasJob(this.name || "")) {
TwitchAutomatorJob.jobs = TwitchAutomatorJob.jobs.filter(job => job.name !== this.name);
if (Job.hasJob(this.name || "")) {
Job.jobs = Job.jobs.filter(job => job.name !== this.name);
Log.logAdvanced(LOGLEVEL.SUCCESS, "job", `Job ${this.name} removed from jobs list`, this.metadata);
} else {
Log.logAdvanced(LOGLEVEL.WARNING, "job", `Job ${this.name} not found in jobs list`, this.metadata);
Expand Down Expand Up @@ -533,7 +533,7 @@ export class TwitchAutomatorJob extends EventEmitter {
console.debug(`Broadcasting job update for ${this.name}`);
this.emit("update", this.toAPI());
this._updateTimer = undefined;
Webhook.dispatch(TwitchAutomatorJob.hasJob(this.name || "") ? "job_update" : "job_clear", {
Webhook.dispatch(Job.hasJob(this.name || "") ? "job_update" : "job_clear", {
"job_name": this.name || "",
"job": this.toAPI(),
});
Expand Down
8 changes: 4 additions & 4 deletions server/src/Core/TwitchVOD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Video, VideosResponse } from "../../../common/TwitchAPI/Video";
import { VodUpdated } from "../../../common/Webhook";
import { TwitchVODChapterJSON, TwitchVODJSON } from "../Storage/JSON";
import { AppName, BaseConfigDataFolder } from "./BaseConfig";
import { TwitchAutomatorJob } from "./TwitchAutomatorJob";
import { Job } from "./Job";
import { TwitchChannel } from "./TwitchChannel";
import { Config } from "./Config";
import { TwitchGame } from "./TwitchGame";
Expand Down Expand Up @@ -1320,17 +1320,17 @@ export class TwitchVOD {
}

public async getChatDumpStatus(): Promise<number | false> {
const job = TwitchAutomatorJob.findJob(`chatdump_${this.basename}`);
const job = Job.findJob(`chatdump_${this.basename}`);
return job ? await job.getStatus() : false;
}

public async getCapturingStatus(use_command = false): Promise<number | false> {
const job = TwitchAutomatorJob.findJob(`capture_${this.basename}`);
const job = Job.findJob(`capture_${this.basename}`);
return job ? await job.getStatus(use_command) : false;
}

public async getConvertingStatus(): Promise<number | false> {
const job = TwitchAutomatorJob.findJob(`convert_${this.basename}`);
const job = Job.findJob(`convert_${this.basename}`);
return job ? await job.getStatus() : false;
}

Expand Down

0 comments on commit b065a8b

Please sign in to comment.