MiraiForum

    • Register
    • Login
    • Search
    • Popular
    • Recent
    • Unsolved
    • Tags
    • Groups
    • 友情链接

    自动重载配置

    技术交流板块
    1
    1
    105
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cssxsh
      cssxsh last edited by

      
          lateinit var service: WatchService
      
          override fun onEnable() {
              DebugSetting.reload()
      
              service = configFolderPath.fileSystem.newWatchService()
              configFolderPath.register(
                  service,
                  arrayOf(StandardWatchEventKinds.ENTRY_MODIFY),
                  SensitivityWatchEventModifier.LOW
              )
      
              launch {
                  while (isActive) {
                      val key = runInterruptible(Dispatchers.IO, service::take)
                      for (event in key.pollEvents()) {
                          val path = event.context() as? Path ?: continue
                          if (path.extension != "yml") continue
                          when (val name = path.nameWithoutExtension) {
                              DebugSetting.saveName -> launch {
                                  logger.info { "$name reload for ${event.kind().name()} - ${event.count()}." }
                                  try {
                                      DebugSetting.reload()
                                  } catch (cause: Throwable) {
                                      logger.warning({ "$name reload failure." }, cause)
                                  }
                              }
                              else -> Unit
                          }
                      }
                      key.reset()
                  }
              }
          }
          override fun onDisable() {
              service.close()
          }
      
      1 Reply Last reply Reply Quote 1
      • 1 / 1
      • First post
        Last post
      Powered by Mamoe Technologies & NodeBB | 友情链接 | 服务监控 | Contact