# 定时脚本

## 例子

### 按照剩余空间限速

```javascript
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);
      }
    }
  }
}
```

### 定时备份至指定路径下

```javascript
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

```javascript
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);
  }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.vertex-app.top/zhu-yao-mo-kuai/ding-shi-jiao-ben.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
