2

I am using "electron": "^5.0.0" and "node-schedule": "^1.3.2" - node-schedule. I am trying to load my job-queue when starting the electron app.

My main.js from electron looks like the following:

async function main() {

  const {
    app,
    BrowserWindow
  } = require('electron')
  const schedule = require('node-schedule');
  const {
    Schedule
  } = require('./schedule')

  let win

  let sch = new Schedule()
  try {
    await sch.initScheduledContent()
    console.log("Imported of jobs done!");
  } catch (error) {
    console.log(error);
  }

  function createWindow() {
    win = new BrowserWindow({
      width: 800,
      height: 600
    })
    win.loadFile('index.html')
    win.on('closed', () => {
      win = null
    })
  }

  app.on('ready', createWindow)

  app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
      app.quit()
    }
  })

  app.on('activate', () => {
    if (win === null) {
      createWindow()
    }
  })
}

main()

My schedule.js looks like the following:

const schedule = require('node-schedule');

class Scheduler {

    constructor(){}

    randomDate(start, end) {
        return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
    }

    async initScheduledContent() {
        let start = new Date()
        let end = new Date()
        end.setSeconds(end.getSeconds() + 20) // add 20 seconds to current time

        let arr = []
        const numb_jobs = 50
        for (let i = 0; i < numb_jobs; i++) {
            d = randomDate(start, end)
            arr.push([i, d]) // job_number, date
            console.log('OPEN: Job  -- #' + arr[i][0] + ' -- scheduled at: ' + arr[i][1]);

            schedule.scheduleJob(arr[i][1], function () {
                console.log('DONE: Job -- #' + arr[i][0] + ' -- executed at: ' + arr[i][1]);
            });
        }
        console.log("\n Number of Jobs Scheduled: " + Object.keys(this.getQueue()).length + "\n");
    }
}

module.exports = {
    Scheduler
};

When using electron ., my app does not start and hangs up.

Any suggestions how to start my app and load all jobs into the queue?

Carol.Kar
  • 4,581
  • 36
  • 131
  • 264

1 Answers1

5

don't block main process with await..

instead call it inside app.on('ready', async () => {/* so your schedule process then call createWindow */}

and don't write everything inside function..

as now you can assign async to on.ready function..

alternate solution

do something like this

create mainWindow then call your scheduler in main through ipcRenderer..

this should work..

if window is not created then it must be your mistake somewhere..

code example for ipcRendrer

// main.js

const {ipcMain} = require('electron')

/* start app, createWindow and all other code */

ipcMain.on('scheduler', (event, dataPassedFromRenderer) => {
  /* do your schedule work */

  // pass any data asynchronously to renderer process with
  event.sender.send('scheduler', returnData) 
})

now in renderer code

/* renderer.js in html or required file in html */

const {ipcRenderer} = require('electron')

// this will send event in main.js
ipcRenderer.send('scheduler', 'send any type of data, can be left blank')

// this will listen for any response or event from main.js
ipcRenderer.on('scheduler', (event, data) => {/* do if required */})



Turnip
  • 35,836
  • 15
  • 89
  • 111
Excalibur
  • 367
  • 1
  • 7
  • I tried the following, `app.on('ready', async () => { try { let sch = new Schedule() await sch.initScheduledContent() console.log("Imported of jobs done!"); } catch (error) { console.log(error); } createWindow })`. However, the app still does not load. Maybe I have to put my scheduler code in another process. – Carol.Kar May 05 '19 at 08:12
  • Thx for your answer! Would you be so kind to give me an example how you would structrure the `ipcProcess`? – Carol.Kar May 05 '19 at 15:03
  • is this example is clearly understandable or not ? @Anna.Klee – Excalibur May 06 '19 at 17:35