Skip to content

Commit

Permalink
add refreshChannels parameter to /update API
Browse files Browse the repository at this point in the history
  • Loading branch information
memo33 committed Dec 21, 2024
1 parent 9f30d93 commit 6771c34
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
### Fixed
- an issue where building a channel with file names containing spaces failed

### Changed
- API upgrade to 2.1:
- `/update` accepts a new parameter `refreshChannels` to clear cached data ([#14][gui14]).


[gui3]: https://github.com/memo33/sc4pac-gui/issues/3
[gui14]: https://github.com/memo33/sc4pac-gui/issues/14


## [0.5.0] - 2024-12-14
Expand Down
8 changes: 5 additions & 3 deletions api.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# API - version 2.0
# API - version 2.1

The API allows other programs to control *sc4pac* in a client-server fashion.

Expand Down Expand Up @@ -346,8 +346,10 @@ Opening a websocket at `/update` triggers the update process.
This mirrors the interactive `sc4pac update` command of the CLI.
The websocket sends a series of messages, some of which expect a specific response, such as a confirmation to continue.

Pass the parameter `simtropolisCookie` to re-use an authenticated session.
Otherwise, the environment variable will be used instead if available.
Parameters:
- `simtropolisCookie=<value>` to re-use an authenticated session.
Otherwise, the environment variable will be used instead if available.
- `refreshChannels` to clear the cached channel contents files before updating.

Example using Javascript in your web browser:
```javascript
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/sc4pac/Constants.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ object Constants {
val fuzzySearchThreshold = 80 // 0..100
val cacheTtl = 12.hours
val channelContentsTtl = 30.minutes
val channelContentsTtlRefresh = 0.minutes
val channelContentsTtlShort = 60.seconds
val interactivePromptTimeout = java.time.Duration.ofSeconds(240)
val urlConnectTimeout = java.time.Duration.ofSeconds(60)
Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/sc4pac/Sc4pac.scala
Original file line number Diff line number Diff line change
Expand Up @@ -549,16 +549,17 @@ object Sc4pac {
/** Limits parallel downloads to 2 (ST rejects too many connections). */
private[sc4pac] def createThreadPool() = coursier.cache.internal.ThreadUtil.fixedThreadPool(size = 2)

def init(config: JD.Config): RIO[ProfileRoot & Logger, Sc4pac] = {
def init(config: JD.Config, refreshChannels: Boolean = false): RIO[ProfileRoot & Logger, Sc4pac] = {
// val refreshLogger = coursier.cache.loggers.RefreshLogger.create(System.err) // TODO System.err seems to cause less collisions between refreshing progress and ordinary log messages
val coursierPool = createThreadPool()
val channelContentsTtl = if (refreshChannels) Constants.channelContentsTtlRefresh else Constants.channelContentsTtl // 0 or 30 minutes
for {
cacheRoot <- config.cacheRootAbs
logger <- ZIO.service[Logger]
cache = FileCache(location = (cacheRoot / "coursier").toIO, logger = logger, pool = coursierPool)
.withTtl(Some(Constants.cacheTtl)) // 12 hours
// .withCachePolicies(Seq(coursier.cache.CachePolicy.ForceDownload)) // TODO cache policy
repos <- initializeRepositories(config.channels, cache, Constants.channelContentsTtl) // 30 minutes
repos <- initializeRepositories(config.channels, cache, channelContentsTtl)
tempRoot <- config.tempRootAbs
profileRoot <- ZIO.service[ProfileRoot]
context = new ResolutionContext(repos, cache, logger, profileRoot.path)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/sc4pac/api/api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class Api(options: sc4pac.cli.Commands.ServerOptions) {
val cookies = Downloader.Cookies(req.url.queryParams.getAll("simtropolisCookie").headOption.orElse(Constants.simtropolisCookie))
val cookieDesc = cookies.simtropolisCookie.map(c => s"with cookie: ${c.length} bytes").getOrElse("without cookie")
for {
pac <- Sc4pac.init(pluginsData.config)
pac <- Sc4pac.init(pluginsData.config, refreshChannels = req.url.queryParams.getAll("refreshChannels").nonEmpty)
pluginsRoot <- pluginsData.config.pluginsRootAbs
wsLogger <- ZIO.service[WebSocketLogger]
_ <- ZIO.succeed(wsLogger.log(s"Updating... ($cookieDesc)"))
Expand Down

0 comments on commit 6771c34

Please sign in to comment.