poad 2.60.0 → 2.61.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ // Verrou d'un projet local : une seule commande qui démarre, arrête ou met à jour ses conteneurs à la fois.
2
+ //
3
+ // Sans lui, un « poad start » lancé pendant un « poad upgrade » voyait les ports du projet tenus un instant
4
+ // par l'engine qui redémarrait, les croyait pris par un autre programme et les réécrivait dans config.toml.
5
+ //
6
+ // Fichier poad/data/.poad-lock (data/ est hors git) : { pid, command, at }. Réentrant dans un même
7
+ // processus (upgrade appelle stop puis start). Un verrou laissé par un processus mort est repris.
8
+ const fs = require('fs');
9
+ const path = require('path');
10
+
11
+ const lockFile = (root) => path.join(path.dirname(root), 'data', '.poad-lock');
12
+
13
+ const alive = (pid) => {
14
+ try {
15
+ process.kill(pid, 0);
16
+ return true;
17
+ } catch (err) {
18
+ // EPERM : le processus existe, mais appartient à un autre utilisateur
19
+ return Boolean(err && err.code === 'EPERM');
20
+ }
21
+ };
22
+
23
+ const read = (file) => {
24
+ try {
25
+ const held = JSON.parse(fs.readFileSync(file, 'utf8'));
26
+ return Number.isInteger(held.pid) ? held : null;
27
+ } catch {
28
+ return null;
29
+ }
30
+ };
31
+
32
+ /**
33
+ * Prend le verrou du projet pour `command`. Renvoie { ok: true, release } ou { ok: false, held } (le
34
+ * verrou d'une autre commande en cours). Le verrou est rendu à la sortie du processus dans tous les cas.
35
+ */
36
+ const acquire = (root, command, { pid = process.pid, isAlive = alive } = {}) => {
37
+ const file = lockFile(root);
38
+ fs.mkdirSync(path.dirname(file), { recursive: true });
39
+ for (let attempt = 0; attempt < 2; attempt++) {
40
+ try {
41
+ const fd = fs.openSync(file, 'wx');
42
+ fs.writeSync(fd, JSON.stringify({ pid, command, at: new Date().toISOString() }));
43
+ fs.closeSync(fd);
44
+ const release = () => {
45
+ const held = read(file);
46
+ if (held && held.pid === pid) fs.rmSync(file, { force: true });
47
+ };
48
+ if (pid === process.pid) process.once('exit', release);
49
+ return { ok: true, release };
50
+ } catch (err) {
51
+ if (err.code !== 'EEXIST') throw err;
52
+ const held = read(file);
53
+ // Même processus (upgrade → stop → start) : déjà à lui
54
+ if (held && held.pid === pid) return { ok: true, release: () => {} };
55
+ if (held && isAlive(held.pid)) return { ok: false, held };
56
+ // Laissé par un processus disparu, ou illisible : repris
57
+ fs.rmSync(file, { force: true });
58
+ }
59
+ }
60
+ return { ok: false, held: read(file) || { command: 'unknown' } };
61
+ };
62
+
63
+ /** Message d'une commande refusée parce qu'une autre tient le projet */
64
+ const busyMessage = (held) =>
65
+ `Another poad command is running on this project ("poad ${held.command}", pid ${held.pid}${held.at ? `, since ${held.at}` : ''}): wait for it to finish, then run this one again.`;
66
+
67
+ module.exports = { acquire, busyMessage, lockFile };
@@ -17,18 +17,21 @@ const files = (root) => {
17
17
  };
18
18
 
19
19
  /**
20
- * Demande la synchronisation et l'affiche. Renvoie false si une table n'a pas
20
+ * Demande la synchronisation et l'affiche. force : l'engine applique aussi une suppression massive
21
+ * (fichier vide ou tronqué) qu'il refuse sinon (poad migrate --force-reference). Renvoie false si une table n'a pas
21
22
  * pu être synchronisée ; true sinon (y compris sans table, ou avec un engine
22
23
  * qui ne connaît pas encore les tables de référence).
23
24
  */
24
- const sync = async (root, config) => {
25
+ const sync = async (root, config, { force = false } = {}) => {
25
26
  const chalk = require('chalk-v2');
26
27
  if (!files(root).length) return true;
27
28
  let res;
28
29
  try {
29
30
  res = await fetch(`http://localhost:${config.web.port}/meta/v1/reference/sync`, {
30
31
  method: 'POST',
31
- headers: { apikey: config.auth.service, authorization: `Bearer ${config.auth.service}` },
32
+ headers: { apikey: config.auth.service, authorization: `Bearer ${config.auth.service}`, 'content-type': 'application/json' },
33
+ // force : une suppression massive (fichier vide ou tronqué) est refusée par l'engine sans elle
34
+ body: JSON.stringify(force ? { force: true } : {}),
32
35
  signal: AbortSignal.timeout(120000),
33
36
  });
34
37
  } catch (err) {