Skip to content

Commit 637dae9

Browse files
authored
Merge pull request #743 from Flow-Launcher/UpdateCheckLock
Rename with async suffix and add lock to prevent concurrent update check
2 parents 3baaa2d + 2af1867 commit 637dae9

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

Flow.Launcher.Core/Updater.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Flow.Launcher.Infrastructure.UserSettings;
1717
using Flow.Launcher.Plugin;
1818
using System.Text.Json.Serialization;
19+
using System.Threading;
1920

2021
namespace Flow.Launcher.Core
2122
{
@@ -28,21 +29,21 @@ public Updater(string gitHubRepository)
2829
GitHubRepository = gitHubRepository;
2930
}
3031

31-
public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
32+
private SemaphoreSlim UpdateLock { get; } = new SemaphoreSlim(1);
33+
34+
public async Task UpdateAppAsync(IPublicAPI api, bool silentUpdate = true)
3235
{
36+
await UpdateLock.WaitAsync();
3337
try
3438
{
35-
UpdateInfo newUpdateInfo;
36-
3739
if (!silentUpdate)
3840
api.ShowMsg(api.GetTranslation("pleaseWait"),
39-
api.GetTranslation("update_flowlauncher_update_check"));
41+
api.GetTranslation("update_flowlauncher_update_check"));
4042

4143
using var updateManager = await GitHubUpdateManager(GitHubRepository).ConfigureAwait(false);
4244

43-
4445
// UpdateApp CheckForUpdate will return value only if the app is squirrel installed
45-
newUpdateInfo = await updateManager.CheckForUpdate().NonNull().ConfigureAwait(false);
46+
var newUpdateInfo = await updateManager.CheckForUpdate().NonNull().ConfigureAwait(false);
4647

4748
var newReleaseVersion = Version.Parse(newUpdateInfo.FutureReleaseEntry.Version.ToString());
4849
var currentVersion = Version.Parse(Constant.Version);
@@ -58,7 +59,7 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
5859

5960
if (!silentUpdate)
6061
api.ShowMsg(api.GetTranslation("update_flowlauncher_update_found"),
61-
api.GetTranslation("update_flowlauncher_updating"));
62+
api.GetTranslation("update_flowlauncher_updating"));
6263

6364
await updateManager.DownloadReleases(newUpdateInfo.ReleasesToApply).ConfigureAwait(false);
6465

@@ -70,8 +71,8 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
7071
FilesFolders.CopyAll(DataLocation.PortableDataPath, targetDestination);
7172
if (!FilesFolders.VerifyBothFolderFilesEqual(DataLocation.PortableDataPath, targetDestination))
7273
MessageBox.Show(string.Format(api.GetTranslation("update_flowlauncher_fail_moving_portable_user_profile_data"),
73-
DataLocation.PortableDataPath,
74-
targetDestination));
74+
DataLocation.PortableDataPath,
75+
targetDestination));
7576
}
7677
else
7778
{
@@ -87,12 +88,15 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
8788
UpdateManager.RestartApp(Constant.ApplicationFileName);
8889
}
8990
}
90-
catch (Exception e) when (e is HttpRequestException || e is WebException || e is SocketException || e.InnerException is TimeoutException)
91+
catch (Exception e) when (e is HttpRequestException or WebException or SocketException || e.InnerException is TimeoutException)
9192
{
9293
Log.Exception($"|Updater.UpdateApp|Check your connection and proxy settings to github-cloud.s3.amazonaws.com.", e);
9394
api.ShowMsg(api.GetTranslation("update_flowlauncher_fail"),
94-
api.GetTranslation("update_flowlauncher_check_connection"));
95-
return;
95+
api.GetTranslation("update_flowlauncher_check_connection"));
96+
}
97+
finally
98+
{
99+
UpdateLock.Release();
96100
}
97101
}
98102

@@ -115,13 +119,16 @@ private async Task<UpdateManager> GitHubUpdateManager(string repository)
115119
var uri = new Uri(repository);
116120
var api = $"https://api.github.com/repos{uri.AbsolutePath}/releases";
117121

118-
var jsonStream = await Http.GetStreamAsync(api).ConfigureAwait(false);
122+
await using var jsonStream = await Http.GetStreamAsync(api).ConfigureAwait(false);
119123

120124
var releases = await System.Text.Json.JsonSerializer.DeserializeAsync<List<GithubRelease>>(jsonStream).ConfigureAwait(false);
121125
var latest = releases.Where(r => !r.Prerelease).OrderByDescending(r => r.PublishedAt).First();
122126
var latestUrl = latest.HtmlUrl.Replace("/tag/", "/download/");
123-
124-
var client = new WebClient { Proxy = Http.WebProxy };
127+
128+
var client = new WebClient
129+
{
130+
Proxy = Http.WebProxy
131+
};
125132
var downloader = new FileDownloader(client);
126133

127134
var manager = new UpdateManager(latestUrl, urlDownloader: downloader);

Flow.Launcher/App.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ private void AutoUpdates()
129129
var timer = new Timer(1000 * 60 * 60 * 5);
130130
timer.Elapsed += async (s, e) =>
131131
{
132-
await _updater.UpdateApp(API);
132+
await _updater.UpdateAppAsync(API);
133133
};
134134
timer.Start();
135135

136136
// check updates on startup
137-
await _updater.UpdateApp(API);
137+
await _updater.UpdateAppAsync(API);
138138
}
139139
});
140140
}

Flow.Launcher/ViewModel/SettingWindowViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public SettingWindowViewModel(Updater updater, IPortable portable)
4646

4747
public async void UpdateApp()
4848
{
49-
await _updater.UpdateApp(App.API, false);
49+
await _updater.UpdateAppAsync(App.API, false);
5050
}
5151

5252
public bool AutoUpdates

0 commit comments

Comments
 (0)