prevent more than one instance

This commit is contained in:
Eva Ho 2023-07-06 16:59:36 -04:00
parent 7c2bb76421
commit 6fbd96dab1

View file

@ -1,11 +1,13 @@
import { spawn, exec } from 'child_process' import { spawn, exec } from 'child_process'
import { app, autoUpdater, dialog, Tray, Menu, nativeTheme } from 'electron' import { app, autoUpdater, dialog, Tray, Menu, nativeTheme, Notification } from 'electron'
import * as path from 'path' import * as path from 'path'
import * as fs from 'fs' import * as fs from 'fs'
require('@electron/remote/main').initialize() require('@electron/remote/main').initialize()
let tray: Tray | null = null let tray: Tray | null = null
let secondInstance: Boolean = false
const SingleInstanceLock = app.requestSingleInstanceLock()
const createSystemtray = () => { const createSystemtray = () => {
let brightModeIconPath = path.join(__dirname, '..', '..', 'assets', 'ollama_icon_dark_16x16.png') let brightModeIconPath = path.join(__dirname, '..', '..', 'assets', 'ollama_icon_dark_16x16.png')
@ -108,52 +110,53 @@ function installCLI() {
}) })
} }
// This method will be called when Electron has finished if (!SingleInstanceLock) {
// initialization and is ready to create browser windows. app.quit()
// Some APIs can only be used after this event occurs. } else {
app.on('ready', () => { app.on('ready', () => {
if (process.platform === 'darwin') {
if (process.platform === 'darwin') { app.dock.hide()
app.dock.hide()
if (!app.isInApplicationsFolder()) {
if (!app.isInApplicationsFolder()) { const chosen = dialog.showMessageBoxSync({
const chosen = dialog.showMessageBoxSync({ type: 'question',
type: 'question', buttons: ['Move to Applications', 'Do Not Move'],
buttons: ['Move to Applications', 'Do Not Move'], message: 'Move Ollama to the Applications directory?',
message: 'Move Ollama to the Applications directory?', defaultId: 0,
defaultId: 0, cancelId: 1
cancelId: 1 })
})
if (chosen === 0) {
if (chosen === 0) { try {
try { app.moveToApplicationsFolder({
app.moveToApplicationsFolder({ conflictHandler: (conflictType) => {
conflictHandler: (conflictType) => { if (conflictType === 'existsAndRunning') {
if (conflictType === 'existsAndRunning') { dialog.showMessageBoxSync({
dialog.showMessageBoxSync({ type: 'info',
type: 'info', message: 'Cannot move to Applications directory',
message: 'Cannot move to Applications directory', detail: 'Another version of Ollama is currently running from your Applications directory. Close it first and try again.'
detail: 'Another version of Ollama is currently running from your Applications directory. Close it first and try again.' })
}) }
return true
} }
return true })
} return
}) } catch (e) {
return console.error('Failed to move to applications folder')
} catch (e) { console.error(e)
console.error('Failed to move to applications folder') }
console.error(e)
} }
} }
} }
}
createSystemtray()
if (app.isPackaged) {
installCLI()
}
})
}
createSystemtray()
if (app.isPackaged) {
installCLI()
}
})
// Quit when all windows are closed, except on macOS. There, it's common // Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits // for applications and their menu bar to stay active until the user quits