2

I am trying to run protractor with multi capabilities (around 30 browsers with diff versions) The data sheet is xlsx and is one sheet, which will be consumed. After each run the xlsx row will be updated that it has been 'USED'

I use exceljs to write the flag. But it throws error if its already been used/opened by another process. I handled the exception, but instead of failing , i would like the muti processes to wait and retry accessing the file.

Please suggest how to read/write one xlsx at same time by multiple processes - where the processed has to wait for any previous process to complete its access.

Write Function:

updateRunnerXLS: function(fileName) {
    var Excel = require('exceljs'); //Require the exceljs package
    var workbook = new Excel.Workbook(); //create a workbook reader
    workbook.xlsx.readFile(fileName)
    .then(function(workbook) {
        var rowValues = []; //initialize empty array
        var worksheet = workbook.getWorksheet('sheet2');
        var nameCol = worksheet.getColumn('M');
        nameCol.eachCell(function(cell, rowNumber) { //Loop through the cells
            if (cell.value == 'Y') { //get all rows with Y
                rowValues.push(rowNumber); //Fill the array with row number
            }
        });
         var row = worksheet.getRow(rowValues[0]); //Get the first row from the array
        row.getCell('M').value = 'X'; //update the first row that has a  Y to X
        row.commit(); //Commit the row change
        workbook.xlsx.writeFile(fileName).then(function() {
            //done
        }).catch(function (err) {
            console.log('Handled the error - ' + err);
        })
    })
}

Read Function: (simpler that reading using exceljs)

var Parser = require('parse-xlsx');
sheet = new Parser(testDataFolder + 'dataSheet.xlsx', 'sheet2');
erchristopher
  • 53
  • 1
  • 1
  • 5
  • 1
    Could you post the code how are you reading/writing the file currently? Thanks. – alecxe Apr 30 '16 at 21:07
  • i am using parse-xlsx to read, The read doesnt care if the file is open or not. Which is another problem. As if process 1 is using row 1 and marking it as being used, process 2 should be aware of that and pick row 2. I tired to add a random wait in the function to wait and write the file. but there is a possibility that two wait values are generated closer and results in "File already being used" error. – erchristopher May 02 '16 at 11:59
  • Either use some cross process communication to share the lock state, which is locked until then( function(){ lock = false;}). Or use a locking file. If the locking file can't be opened in write mode, delay your process. if the locking file is opened in write mode, open it, update your xls file, on then() then close lock file. – ChrisAdmin Oct 31 '16 at 05:34

0 Answers0