1
1
package updater
2
2
3
+ import java .util .concurrent .atomic .AtomicBoolean
4
+
3
5
import better .files .File
4
6
import com .typesafe .scalalogging .StrictLogging
5
- import javax .inject .Inject
7
+ import javax .inject .{ Inject , Singleton }
6
8
import lib .App ._
7
9
import lib .AppException .UpdateException
8
10
import monix .execution .{Cancelable , Scheduler }
@@ -11,16 +13,21 @@ import updater.GithubConnector.Release
11
13
import scala .concurrent .duration ._
12
14
import scala .util .control .NonFatal
13
15
16
+ @ Singleton
14
17
class Updater @ Inject ()(connector : GithubConnector , serviceUpdater : ServiceUpdater )(implicit scheduler : Scheduler ) extends StrictLogging {
18
+ private val updateRunning = new AtomicBoolean (false )
19
+
15
20
def start : Cancelable = {
16
21
logger.info(" Started updates checker" )
17
22
18
- scheduler.scheduleAtFixedRate(10 .seconds, 10 .seconds) {
23
+ scheduler.scheduleAtFixedRate(1 .seconds, 10 .seconds) {
19
24
connector.checkUpdate
20
25
.flatMap {
21
26
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(())
24
31
25
32
case None =>
26
33
logger.debug(" Didn't find update for current version" )
@@ -29,10 +36,12 @@ class Updater @Inject()(connector: GithubConnector, serviceUpdater: ServiceUpdat
29
36
.value
30
37
.onErrorRecover {
31
38
case e : java.net.ConnectException =>
39
+ updateRunning.set(false )
32
40
logger.warn(" Could not check for update" , e)
33
41
Left (UpdateException (" Could not update the app" , e))
34
42
35
43
case NonFatal (e) =>
44
+ updateRunning.set(false )
36
45
logger.warn(" Unknown error while updating the app" , e)
37
46
Left (UpdateException (" Could not update the app" , e))
38
47
}
@@ -41,10 +50,20 @@ class Updater @Inject()(connector: GithubConnector, serviceUpdater: ServiceUpdat
41
50
}
42
51
43
52
private def updateApp (release : Release ): Result [Unit ] = {
53
+ logger.info(s " Downloading update file for version ${release.tagName}" )
54
+
44
55
downloadUpdate(release).map { file =>
45
56
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 )
46
64
logger.debug(s " Updater unzipped the update to $dirWithUpdate" )
47
65
66
+ logger.info(" Starting the update" )
48
67
serviceUpdater.restartAndReplace(dirWithUpdate)
49
68
}
50
69
}
0 commit comments