Skip to content

Commit c319d5c

Browse files
committedDec 8, 2018
Updater WIP
Windows updating
1 parent 27f14bb commit c319d5c

9 files changed

+90
-25
lines changed
 

‎app/AppModule.scala

+5-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ class AppModule(environment: Environment, configuration: Configuration)
5858
bind[StateManager].toInstance(stateManager)
5959

6060
bind[GithubConnector].toInstance {
61-
new GithubConnector(Http1Client[Task]().runSyncUnsafe(Duration.Inf),
62-
Uri.unsafeFromString("http://localhost:80/releases.json"),
63-
AppVersion(0, 1, 0))
61+
new GithubConnector(
62+
Http1Client[Task]().runSyncUnsafe(Duration.Inf),
63+
Uri.unsafeFromString(config.getString("updater.releasesUrl")),
64+
AppVersion(config.getString("appVersion")).getOrElse(throw new IllegalArgumentException("Could not parse appVersion"))
65+
)
6466
}
6567

6668
bind[Monitor].annotatedWithName("FilesHandler").toInstance(rootMonitor.named("fileshandler"))

‎app/lib/StateManager.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class StateManager(deviceId: DeviceId, cloudConnector: CloudConnector, dao: Dao,
5656
}
5757

5858
def downloadRemoteFilesList(implicit session: ServerSession): Result[Unit] = {
59-
logger.debug("Downloading remote files list")
59+
logger.info("Downloading remote files list")
6060

6161
for {
6262
allFiles <- cloudConnector.listFiles(Some(deviceId)).subflatMap {

‎app/lib/db/Dao.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class Dao(executor: ExecutorService) extends StrictLogging {
159159

160160
def deleteAllFiles(): Result[Unit] = EitherT {
161161
Task {
162-
logger.info(s"Deleting all files")
162+
logger.debug(s"Deleting all files")
163163

164164
DB.autoCommit { implicit session =>
165165
sql"DELETE FROM files".executeUpdate().apply()

‎app/updater/GithubConnector.scala

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import cats.syntax.all._
99
import io.circe.Decoder
1010
import lib.App.Result
1111
import lib.AppException
12-
import lib.AppException.{FileException, InvalidResponseException, ParsingFailure}
12+
import lib.AppException._
1313
import monix.eval.Task
1414
import monix.execution.Scheduler
1515
import org.http4s.client.Client
@@ -42,7 +42,7 @@ class GithubConnector(httpClient: Client[Task], uri: Uri, appVersion: AppVersion
4242
EitherT {
4343
httpClient
4444
.get(asset.browserDownloadUrl) { resp =>
45-
val file = File("./update")
45+
val file = File.temporaryFile("rbackup", s"update-${asset.name}").get
4646

4747
resp.body
4848
.through(fs2.io.writeOutputStreamAsync(Task {
@@ -66,8 +66,6 @@ object GithubConnector {
6666
import io.circe.parser._
6767
import utils.CirceImplicits._
6868

69-
final val ReleasesUrl = Uri.uri("https://github.com/jendakol/rbackup-scala-client/releases")
70-
7169
def parse(str: String): Either[ParsingFailure, Seq[Release]] = {
7270
decode[Seq[Release]](str).leftMap(ParsingFailure(str, _))
7371
}

‎app/updater/ServiceUpdater.scala

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
package updater
22

33
import better.files.File
4+
import com.typesafe.scalalogging.StrictLogging
45

56
import scala.language.postfixOps
6-
import scala.sys.process._
77

88
sealed trait ServiceUpdater {
99
def restartAndReplace(dirWithUpdate: File): Unit
1010
}
1111

12-
class WindowsServiceUpdater extends ServiceUpdater {
12+
class WindowsServiceUpdater extends ServiceUpdater with StrictLogging {
1313
override def restartAndReplace(dirWithUpdate: File): Unit = {
14-
"%COMPSPEC% /C /D \"scripts\\restart_replace.cmd\" Arg1 Arg2 Arg3" !
14+
Runtime.getRuntime.exec(
15+
Array(
16+
"cmd",
17+
"/C",
18+
"start",
19+
"\"\"",
20+
"restart_replace.cmd",
21+
dirWithUpdate.pathAsString
22+
))
23+
()
1524
}
1625
}
1726

1827
class LinuxServiceUpdater extends ServiceUpdater {
1928
override def restartAndReplace(dirWithUpdate: File): Unit = {
20-
"/bin/bash -c \"restart_replace.sh\"" !
29+
Runtime.getRuntime.exec(
30+
Array(
31+
"/bin/bash",
32+
"-c",
33+
"restart_replace.sh",
34+
dirWithUpdate.pathAsString,
35+
"&",
36+
))
37+
()
2138
}
2239
}

‎app/updater/Updater.scala

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package updater
22

3+
import java.util.concurrent.atomic.AtomicBoolean
4+
35
import better.files.File
46
import com.typesafe.scalalogging.StrictLogging
5-
import javax.inject.Inject
7+
import javax.inject.{Inject, Singleton}
68
import lib.App._
79
import lib.AppException.UpdateException
810
import monix.execution.{Cancelable, Scheduler}
@@ -11,16 +13,21 @@ import updater.GithubConnector.Release
1113
import scala.concurrent.duration._
1214
import scala.util.control.NonFatal
1315

16+
@Singleton
1417
class Updater @Inject()(connector: GithubConnector, serviceUpdater: ServiceUpdater)(implicit scheduler: Scheduler) extends StrictLogging {
18+
private val updateRunning = new AtomicBoolean(false)
19+
1520
def start: Cancelable = {
1621
logger.info("Started updates checker")
1722

18-
scheduler.scheduleAtFixedRate(10.seconds, 10.seconds) {
23+
scheduler.scheduleAtFixedRate(1.seconds, 10.seconds) {
1924
connector.checkUpdate
2025
.flatMap {
2126
case Some(rel) =>
22-
logger.debug(s"Found update: $rel")
23-
updateApp(rel)
27+
if (updateRunning.compareAndSet(false, true)) {
28+
logger.debug(s"Found update: $rel")
29+
updateApp(rel)
30+
} else pureResult(())
2431

2532
case None =>
2633
logger.debug("Didn't find update for current version")
@@ -29,10 +36,12 @@ class Updater @Inject()(connector: GithubConnector, serviceUpdater: ServiceUpdat
2936
.value
3037
.onErrorRecover {
3138
case e: java.net.ConnectException =>
39+
updateRunning.set(false)
3240
logger.warn("Could not check for update", e)
3341
Left(UpdateException("Could not update the app", e))
3442

3543
case NonFatal(e) =>
44+
updateRunning.set(false)
3645
logger.warn("Unknown error while updating the app", e)
3746
Left(UpdateException("Could not update the app", e))
3847
}
@@ -41,10 +50,20 @@ class Updater @Inject()(connector: GithubConnector, serviceUpdater: ServiceUpdat
4150
}
4251

4352
private def updateApp(release: Release): Result[Unit] = {
53+
logger.info(s"Downloading update file for version ${release.tagName}")
54+
4455
downloadUpdate(release).map { file =>
4556
val dirWithUpdate = file.unzipTo(File(s"update-${release.tagName}"))
57+
// the data are in subfolder, move them up
58+
dirWithUpdate.children.next().children.foreach { sub =>
59+
logger.debug(s"Moving $sub to $dirWithUpdate")
60+
sub.moveToDirectory(dirWithUpdate)
61+
}
62+
63+
file.delete(true)
4664
logger.debug(s"Updater unzipped the update to $dirWithUpdate")
4765

66+
logger.info("Starting the update")
4867
serviceUpdater.restartAndReplace(dirWithUpdate)
4968
}
5069
}

‎conf/reference.conf

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// deviceId = "" // REQUIRED
22

3+
appVersion = "0.1.0"
4+
35
cloudConnectorDefaults {
46
requestTimeout = 30 minutes
57
socketTimeout = 30 seconds
@@ -15,6 +17,10 @@ fileHandler {
1517
retries = 2
1618
}
1719

20+
updater {
21+
releasesUrl = "https://github.com/jendakol/rbackup-scala-client/releases"
22+
}
23+
1824
allowedWsApiOrigins = ["http://localhost:3370"]
1925

2026
play.filters {

‎scripts/restart_replace.cmd

-8
This file was deleted.

‎windeploy/restart_replace.cmd

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
echo Stopping the service...
2+
net stop rbackup-client
3+
del RUNNING_PID > NUL
4+
5+
echo Updating files...
6+
7+
if exist %1\conf (
8+
rd /s /q conf.old > NUL 2>&1
9+
move conf conf.old
10+
move %1\conf conf
11+
) && ^
12+
if exist %1\lib (
13+
rd /s /q lib.old > NUL 2>&1
14+
move lib lib.old
15+
move %1\lib lib
16+
) && ^
17+
if exist %1\public (
18+
rd /s /q public.old > NUL 2>&1
19+
move public public.old
20+
move %1\public public
21+
) && ^
22+
if exist %1\restart_replace.cmd (
23+
del restart_replace.cmd.old > NUL 2>&1
24+
move restart_replace.cmd restart_replace.cmd.old
25+
move %1\restart_replace.cmd restart_replace.cmd
26+
)
27+
28+
echo Starting the service...
29+
30+
net start rbackup-client && ^
31+
rd /s /q %1 > NUL 2>&1

0 commit comments

Comments
 (0)