mirror of
				https://github.com/whyour/qinglong.git
				synced 2025-11-04 20:06:08 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			78 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			78 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import { Service, Inject } from 'typedi';
 | 
						|
import winston from 'winston';
 | 
						|
import path, { join } from 'path';
 | 
						|
import SockService from './sock';
 | 
						|
import CronService from './cron';
 | 
						|
import ScheduleService, { TaskCallbacks } from './schedule';
 | 
						|
import config from '../config';
 | 
						|
import { TASK_COMMAND } from '../config/const';
 | 
						|
import { getFileContentByName, getPid, killTask, rmPath } from '../config/util';
 | 
						|
import taskLimit from '../shared/pLimit';
 | 
						|
 | 
						|
@Service()
 | 
						|
export default class ScriptService {
 | 
						|
  constructor(
 | 
						|
    @Inject('logger') private logger: winston.Logger,
 | 
						|
    private sockService: SockService,
 | 
						|
    private cronService: CronService,
 | 
						|
    private scheduleService: ScheduleService,
 | 
						|
  ) {}
 | 
						|
 | 
						|
  private taskCallbacks(filePath: string): TaskCallbacks {
 | 
						|
    return {
 | 
						|
      onEnd: async (cp, endTime, diff) => {
 | 
						|
        await rmPath(filePath);
 | 
						|
      },
 | 
						|
      onError: async (message: string) => {
 | 
						|
        this.sockService.sendMessage({
 | 
						|
          type: 'manuallyRunScript',
 | 
						|
          message,
 | 
						|
        });
 | 
						|
      },
 | 
						|
      onLog: async (message: string) => {
 | 
						|
        this.sockService.sendMessage({
 | 
						|
          type: 'manuallyRunScript',
 | 
						|
          message,
 | 
						|
        });
 | 
						|
      },
 | 
						|
    };
 | 
						|
  }
 | 
						|
 | 
						|
  public async runScript(filePath: string) {
 | 
						|
    const relativePath = path.relative(config.scriptPath, filePath);
 | 
						|
    const command = `${TASK_COMMAND} ${relativePath} now`;
 | 
						|
    const pid = await this.scheduleService.runTask(
 | 
						|
      `real_time=true ${command}`,
 | 
						|
      this.taskCallbacks(filePath),
 | 
						|
      { command, id: relativePath.replace(/ /g, '-'), runOrigin: 'script' },
 | 
						|
      'start',
 | 
						|
    );
 | 
						|
 | 
						|
    return { code: 200, data: pid };
 | 
						|
  }
 | 
						|
 | 
						|
  public async stopScript(filePath: string, pid: number) {
 | 
						|
    if (!pid) {
 | 
						|
      const relativePath = path.relative(config.scriptPath, filePath);
 | 
						|
      taskLimit.removeQueuedCron(relativePath.replace(/ /g, '-'));
 | 
						|
      pid = (await getPid(`${TASK_COMMAND} ${relativePath} now`)) as number;
 | 
						|
    }
 | 
						|
    try {
 | 
						|
      await killTask(pid);
 | 
						|
    } catch (error) {}
 | 
						|
 | 
						|
    return { code: 200 };
 | 
						|
  }
 | 
						|
 | 
						|
  public async getFile(filePath: string, fileName: string) {
 | 
						|
    const finalPath = path.resolve(config.scriptPath, filePath, fileName);
 | 
						|
 | 
						|
    if (!finalPath.startsWith(config.scriptPath)) {
 | 
						|
      return '';
 | 
						|
    }
 | 
						|
 | 
						|
    const content = await getFileContentByName(finalPath);
 | 
						|
    return content;
 | 
						|
  }
 | 
						|
}
 |