Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2018
    Messaggi
    3

    GULP, eseguire tasks in sequenza quando la precedente è effettivamente finita

    Salve a tutti!

    Ho un problema con Gulp. In pratica ho due funzioni, la prima fa il Dump di un database, la secondo lo elimina. Mi serve fare il dump semplicemente perchè se erronamente una persona elimina il database questo per sicurezza prima viene backuppato...

    Ho due funzioni per questo:
    codice:
    // Dump the database, just to avoid accidental use of this task
    function dumpDB ( ) {
    
      var conn = mysql.createConnection({
        host     : config.db.dbHost,
        user     : config.db.dbUser,
        password : config.db.dbPassword,
      });
    
      conn.connect();
    
      // Check if is there are tables in the database to backup.
    
      return conn.query('SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = \'' + config.db.dbName + '\'', function (error, results, fields) {
        if (error) throw error;
    
        conn.end();
        count = results[0]['COUNT(*)'];
        
        if(count>0){
    
          // create trash folder if it doesn't exists
          if(! fs.existsSync(config.paths.trash)){
            fs.mkdirSync(config.paths.trash);
          }else{
            del.sync(config.paths.trash + '**/*');
          }
    
          // dump the result straight to a file
          mysqldump({
            connection: {
              host     : config.db.dbHost,
              user     : config.db.dbUser,
              password : config.db.dbPassword,
              database : config.db.dbName,
            },
            dumpToFile: config.paths.trash + 'dump_' + new Date().toISOString().slice(0,19).replace(/:/g, "-").replace("T", "@") + '.sql',
          });      
        }else{
    
          f.alert('Database <' + config.db.dbName + '> doesn\'t contain anything.\n# Nothing will be dumped.')
    
        }
    
      });
    
    }
    
    function deleteDB (done) {
    
      var pool = mysql.createPool({
        host     : config.db.dbHost,
        user     : config.db.dbUser,
        password : config.db.dbPassword
      });
    
      return pool.getConnection(function(err, connection) {
    
        if (err) throw err; // not connected!
    
        // Use the connection
        connection.query('SHOW DATABASES LIKE \'' + config.db.dbName + '\'', function (error, results, fields) {
    
          // When done with the connection, release it.
          connection.release();
    
          if (error) throw error;
    
          if(results.length>0){
            
            // Use the connection
            connection.query('DROP DATABASE `' + config.db.dbName + '`', function (error, results, fields) {
          
              if (error) throw error;
          
              f.alert('Database <' + config.db.dbName + '> successfully deleted.');
    
            });
    
          }else{
    
            f.alert('Database <' + config.db.dbName + '> doesn\'t exists.');
    
          } // end if(results.length==0){
          
          pool.end();
    
        }); // end connection.query('SHOW DATABASES
    
      }); // pool.getConnection
    
    }
    Come sicuramente si evincerà dal codice, sono completamente autodidatta e sicuramente avrò scritto qualche oscenità, ma lanciando le funzioni, il dump dà sempre errore perchè deleteDB viene lanciata quando ancora il dump è in esecuzione...

    Per lanciare le due funzioni le ho esportate così:

    codice:
    exports.delete = series(dumpDB, deleteDB)
    Come posso far lanciare deleteDB solo quando dumpDB ha finito il suo lavoro???

    Ho provato con gulp-sequence ma mi dà lo stesso problema.

    Ah! Ovviamente i task lanciati singolarmente funzionano e fanno il loro lavoro.

    L'errore che ricevo quando le eseguo insieme è questo:

    [14:58:14] Starting 'dumpDB'...
    [14:58:14] Finished 'dumpDB' after 77 ms
    [14:58:14] Starting 'deleteDB'...
    ##
    # Database <progetto-test> successfully deleted.
    ##
    (node:13740) UnhandledPromiseRejectionWarning: Error: Table 'progetto-test.mfxqd_aiowps_events' doesn't exist
    at PromiseConnection.query (D:\xampp\www\wp-init-v2\node_modules\mysql2\promise.js:92:22)
    at DB.<anonymous> (D:\xampp\www\wp-init-v2\node_modules\mysqldump\dist\cjs.js:704:46)
    at Generator.next (<anonymous>)
    at D:\xampp\www\wp-init-v2\node_modules\mysqldump\dist\cjs.js:43:71
    at new Promise (<anonymous>)
    at __awaiter (D:\xampp\www\wp-init-v2\node_modules\mysqldump\dist\cjs.js:39:12)
    at DB.multiQuery (D:\xampp\www\wp-init-v2\node_modules\mysqldump\dist\cjs.js:699:16)
    at D:\xampp\www\wp-init-v2\node_modules\mysqldump\dist\cjs.js:76:42
    at Generator.next (<anonymous>)
    at fulfilled (D:\xampp\www\wp-init-v2\node_modules\mysqldump\dist\cjs.js:40:58)
    (node:13740) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
    (node:13740) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
    Anche se dice dumpDB finished fater 77ms, in realtà quello sta ancora creando il file .sql!

  2. #2
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,320
    Da quel che vedo e che ho potuto leggere on-line, GULP è una libreria JavaScript. Credo sia meglio postare nel forum dedicato a JavaScript.

    Sposto.


    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  3. #3
    Utente di HTML.it L'avatar di m4rko80
    Registrato dal
    Aug 2008
    residenza
    Milano
    Messaggi
    2,655
    Ciao, plugin a parte per gestire le sequenze o esecuzioni in parallelo solitamente puoi usare
    codice:
    gulp.task('two',['one'],function(){
      // task 'one' is done now
    });
    Il task "two" viene eseguito quando "one" e' stato eseguito . L'array opzionale sono la lista dei task da eseguire prima di quello richiesto ("two"). Internamente ai task vari ovviamente devi gestire tu se andato a buon fine o meno e fare il necessario.

Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.