定时脚本

例子

按照剩余空间限速

async () => {
  // 下载器 ID ['id1', 'id2', 'id3']
  const clientIds = ['21d08858'];
  // 限速, 单位 Byte
  const limitSpeed = 10 * 1024 * 1024;
  // 最小剩余空间, 单位 Byte
  const minFreeSpace = 20 * 1024 * 1024 * 1024;
  for (const clientId of clientIds) {
    const client = global.runningClient[clientId];
    // 获取下载器当前剩余空间以及当前限速信息
    const freeSpace = client.maindata.freeSpaceOnDisk;
    const limit = await client.getGlobalSpeedLimit('download');
    if (freeSpace < minFreeSpace) {
      if (+limit === limitSpeed) {
        continue;
      }
      logger.info(`定时脚本 ${this.alias} 达到限速标准, 执行限速`);
      await client.setGlobalSpeedLimit('download', limitSpeed);
    } else {
      if (+limit !== 0) {
        logger.info(`定时脚本 ${this.alias} 未达到限速标准, 恢复限速`);
        await client.setGlobalSpeedLimit('download', 0);
      }
    }
  }
}

定时备份至指定路径下

async () => {
  // 希望备份到的目录, 需映射至 docker
  const baseDir = '/';
  const fs = require('fs');
  const path = require('path');

  try {
    const backupFilename = await (new (require('../model/SettingMod'))()).backupVertex({});
    fs.copyFileSync(backupFilename, path.join(baseDir, path.basename(backupFilename)));
    logger.sc('备份成功,', backupFilename);
  } catch (e) {
    logger.error('备份失败, 错误信息:\n', e);
  }
};

定时备份至 Webdav

async () => {
  // 用户名
  const username = 'username';
  // 密码
  const password = 'password';
  // 希望上传到的目录
  const baseDir = '/';
  // webdav url
  const url = 'http://webdav.vertex.icu/';

  const { createClient } = require('webdav');
  const fs = require('fs');
  const path = require('path');

  const client = createClient(
    url,
    {
      username: username,
      password: password
    }
  );
  try {
    const backupFilename = await (new (require('../model/SettingMod'))()).backupVertex({});
    await client.putFileContents(path.join(baseDir, path.basename(backupFilename)), fs.readFileSync(backupFilename, { encoding: null }));
    logger.sc('备份成功,', backupFilename);
  } catch (e) {
    logger.error('备份失败, 错误信息:\n', e);
  }
};