Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: batch set #47

Open
willin opened this issue Mar 9, 2021 · 4 comments
Open

Bug: batch set #47

willin opened this issue Mar 9, 2021 · 4 comments

Comments

@willin
Copy link

willin commented Mar 9, 2021

import hostile from 'hostile';
import { promisify } from 'util';

const hostileSet = promisify(hostile.set);

Promise.all([
  hostileSet('1.2.3.4', 'test1'),
  hostileSet('1.2.3.5', 'test2'),
  hostileSet('1.2.3.4', 'test1'),
  hostileSet('1.2.3.4', 'test3')
]).then(console.log);
// [ undefined, undefined, undefined, undefined ]

in host file:

1.2.3.4 test1

only add one line


import hostile from 'hostile';
import sleep from 'sleep-promise';
import { promisify } from 'util';

const hostileSet = promisify(hostile.set);

Promise.all([
  (async () => {
    await sleep(200);
    await hostileSet('1.2.3.4', 'test1');
  })(),
  (async () => {
    await sleep(200);
    await hostileSet('1.2.3.5', 'test2');
  })(),
  (async () => {
    await sleep(200);
    await hostileSet('1.2.3.4', 'test1');
  })(),
  (async () => {
    await sleep(200);
    await hostileSet('1.2.3.4', 'test3');
  })()
]).then(console.log);
// [ undefined, undefined, undefined, undefined ]

in host file:

1.2.3.4 test1

only add one line

@Ricardonacif
Copy link

Since they're all modifying the file, I suggest not running them concurrently with Promise.all, but sequentially using https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Statements/for-await...of

@gnrlbzik
Copy link

gnrlbzik commented Jul 8, 2021

I just tried two paths and no luck with batching things up, cmd line stuff works fine

# 1

const hostile = require('hostile')

const HOSTS_TO_ADD = [
  ['127.0.0.1',	'api.domain.devwork'],
  ['127.0.0.1',	'app.domain.devwork'],
]

HOSTS_TO_ADD.forEach(async ([ip, host] = hostDefinition) => {
  await hostile.set(ip, host, function (err) {
    if (err) {
      console.error(err)
    } else {
      console.log(`set '${ip} ${host}' in '/etc/hosts' successfully!`)
    }
  })
})

# 2

const hostile = require('hostile')

function* hosts() {
  yield ['127.0.0.1',	'api.domain.devwork'];
  yield ['127.0.0.1',	'app.domain.devwork'];
}

(async function() {
  try {
    for await (const hostDefinition of hosts()) {
      const [ip, host] = hostDefinition
      hostile.set(ip, host, function (err) {
        if (err) {
          console.error(err)
        } else {
          console.log(`set '${ip} ${host}' in '/etc/hosts' successfully!`)
        }
      })
    }
  } catch (e) {
    console.log('caught', e)
  }
})();

@unilynx
Copy link
Collaborator

unilynx commented Jul 8, 2021

hostile.set is not an async function and does not return a promise, so 'await hostile.set' will not actually wait until the get/set cycle is complete. (it would probably be a nice addition to the API though)

you will need to explicitly wait for the callback to execute if you want to forEach around it. something like

await new Promise( (resolve,reject) => hostile.set(ip, host, err => err ? reject(err) : resolve()))

@gnrlbzik
Copy link

gnrlbzik commented Jul 8, 2021

hostile.set is not an async function and does not return a promise, so 'await hostile.set' will not actually wait until the get/set cycle is complete. (it would probably be a nice addition to the API though)

you will need to explicitly wait for the callback to execute if you want to forEach around it. something like

await new Promise( (resolve,reject) => hostile.set(ip, host, err => err ? reject(err) : resolve()))

thank you for point out things, thank you for code snippet but this also did not do the trick. just writes one line at time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants