This is the last post in the research Electron Automatic Update series. Thank you for your patience.

Series 1 provides an in-depth look at how this works from the auto-update scenario, as well as two packaging options.

Series 2 lists three problems in development, namely “Can not find Squirrel”, “Packages folder and update. exe program Can not be found in installation directory” and “Error: spawn UNKNOWN”, and analyzes and answers them from different perspectives.

This article will continue series 2 with other issues encountered.

Problems in development

This article was first published on the public account “Full stack big guy’s Cultivation road”, welcome to follow.

(四) Error Downloading Update: Command failed: 4294967295

1. The background

Error Downloading Update: Command failed: 4294967295 is displayed during the automatic Update. This Error is consistent with Error 3 in Series 2. Spawn UNKNOWN is similar, and because this problem is so common, I’m going to pick it out.

2. Cause analysis

This question in the Squirrel. Windows issues (https://GitHub.com/Squirrel/Squirrel.Windows/issues/833) are, in some of the answer but around it: Program bugs, remote distribution files that are empty or corrupted affect our updates.

3. Solutions

For developers, I need to re-upload the new installer. There may also be an error in updating the url provided by the server to download NUPkg, which needs to be checked with squirrelsetup.log.

For users, you may need to uninstall and then reinstall the new version.

(5) After the update, the old version is not replaced

1. The background

If the current version of the application is 0.0.1 on the computer, the latest version is 0.0.2 on the server. After the automatic update was completed, a new version of the directory app0.0.2 was added, but XXX project was not covered, and the desktop shortcut opened the old version of XXX project.

The reason is that NSIS did not integrate the updateManage mechanism. Series I and II have been described and will not be repeated.

Figure 7 Installation directory

2. Solutions

  1. Send a current version request to the server at regular intervals, asking if it has a new version of the application (setFeedURLcheckForUpdatesMethod implementation);
  2. When an update comes inerror,checking-for-update,update-availableandupdate-not-availableThese hook methods are written to the log;
  3. Update to enterupdate-downloadedTo prompt the user that the update is complete and manually restart. Then, a child process is started to executebatScript to replace the old version under the installation directory.

For the update code of the XXX project, see update.js:

import {autoUpdater} from 'electron'
// Server address
const server = 'XXXXXXX'
const url = `${server}/update/${process.platform}/${app.getVersion()}/stable`
logger.info(`url:${url}`)
// Set the request address
autoUpdater.setFeedURL({
  url
})
logger.info(`process.ExecPath:${process.ExecPath}`)
// Check for updates
setInterval(() = > {
  autoUpdater.checkForUpdates()
  logger.info('checkForUpdates')},900000)

const appName = 'Apply updates'
const message = {
  error: 'Check update error'.checking: 'Checking for updates... '.updateAva: 'Update package downloaded successfully'.updateNotAva: 'This is the latest version, don't update it'.downloaded: 'Update completed, please restart manually'
}
autoUpdater.on('error'.error= > {
  logger.error('There was a problem updating the application')
  logger.error(error)
})
.on('checking-for-update'.function () {
    logger.info('Triggered when checking for updates begins')
  })
  .on('update-available'.function () {
    logger.info('Issued when an update is available, the update will download automatically')
  })
  .on('update-not-available'.function () {
    logger.info('No update yet')
  })
autoUpdater.on('update-downloaded'.(event, releaseNotes, releaseName) = > {
  logger.info('update-downloaded')
  logger.info(`releaseNotes:${releaseNotes}`)
  logger.info(`releaseName:${releaseName}`)

  dialog.showMessageBox({
    type: 'info'.buttons: ['sure'].title: appName,
    message: process.platform === 'win32' ? releaseNotes : releaseName,
    detail: message.downloaded
  }).then((returnValue) = > {
    if (returnValue.response === 0) {
      fs.writeFile('.. /releaseName.txt', releaseName, (err) = > {
        if (err) {
          logger.error(err)
          throw err
        } else {
          var ls
          ls = childProcess.spawn('libs/Windows/adb/adb'['kill-server'])
          ls.stdout.on('data'.function (data) {
            logger.info('stdout: ' + data)
          })
          ls.stderr.on('data'.function (data) {
            logger.error('stderr: ' + data)
          })
          ls.on('exit'.function (code) {
            logger.info('Directory replacement program running')
            / / address
            const a = process.cwd()
            logger.info('a ' + a)
            const arr = a.split('\ \')
            logger.info('arr ' + arr)
            const pre = a.slice(0, -arr[arr.length - 1].length)
            logger.info('pre' + pre)
            process.chdir(pre)
            childProcess.Exec(`start /min  "" "${pre}replace.bat" ${releaseName}`)
            setTimeout(() = > {
              logger.info('XXX project exit')
              app.quit()
            }, 1000)})}})}})Copy the code

The new version of the XXX project replaces the old version of the script, see replace.bat:

chcp 65001
echo** Updates are about to be completed, please do not close the window! ** >> replace.log ping -n 5 127.0.0.1 >> replace.log COPY".\ XXX item \Uninstall XXX item. Exe" app-%1 >> replace.log
COPY ". \ \ uninstallerIcon XXX project ico" app-%1 >> replace.log
RD /q /s ". \ "XXX project >> replace.log
ren app-%1 "XXX project" >> replace.log
del Exe "XXX project." >> replace.log
exit
Copy the code

(6) Operations other than update. exe do not log

1. The background

If console is added to the main process, only the logs are printed on the terminal and cannot be persisted.

Logs generated during the Update process are stored in squirrelsetup.log, but only those generated by update.exe. However, many steps require more logging. Some update logs in the automation tool use the Log4JS scheme to output different log types in different files.

2. Solutions

For the log configuration of the XXX project, see log4js.js:

const log4js = require('log4js')
const programName = 'XXX project'
log4js.configure({
  appenders: {
    console: { // logger 1: output to the console
      type: 'console'
    },
    log_file: { // logger 2: outputs to a file
      type: 'file'.filename: `./logs/${programName}.log`.maxLogSize: 20971520.backups: 3.encoding: 'utf-8' 
    },
    data_file: { // : logger 3: outputs to the date file
      type: 'dateFile'.filename: `./logs/${programName}`.alwaysIncludePattern: true.daysToKeep: 7.pattern: 'yyyy-MM-dd-hh.log'.encoding: 'utf-8' 
    },
    error_file: { // : logger 4: output to error log
      type: 'dateFile'.filename: `./logs/${programName}_error`.alwaysIncludePattern: true.daysToKeep: 7.pattern: 'yyyy-MM-dd-hh.log'.encoding: 'utf-8'}},categories: {
    default: {
      appenders: ['data_file'.'console'.'log_file'].level: 'info'
    }, // The default log type, output to the console log file log date file and register greater than info
    production: {
      appenders: ['data_file'].level: 'warn'
    }, // The production environment log type is output only to files named by date, and only logs above warning are output
    console: {
      appenders: ['console'].level: 'debug'
    }, // The development environment outputs to the console
    debug: {
      appenders: ['console'.'log_file'].level: 'debug'
    }, // Debug environment output to log file and console
    error_log: {
      appenders: ['error_file'].level: 'error'
    } Errorlog output to any environment in the error file will be recorded as a separate date file}})module.exports = log4js
Copy the code

conclusion

There may be many ways to solve the problem, but the need to use a suitable way to study in the end, perseverance, will be able to get harvest.

This paper introduces the existing problems, automatic update scheme, two ways of packaging and the problems in development. Among them, XXX project adopts squirrel. Windows update mechanism and NSIS custom installation strategy.

After configuring the two using the electron builder, the different setup program setup.exe and the update program NUPkg are produced. Then upload nSIS setup.exe and squirrel. Windows nupkg to electron release-server. The electron relesion-server timing check strategy is used to compare the local version and online version, automatically download dependencies and programs, update and replace them, so that users do not feel, and the operation is not complicated.

These are not the only problems encountered during development. Due to space issues, I have summarized some common problems.

Finally, I hope you must point to like three times.

You can read my other posts at the blog address