-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_init.qmd
More file actions
131 lines (105 loc) · 4.25 KB
/
Copy path_init.qmd
File metadata and controls
131 lines (105 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
```{java}
//| output: false
//| echo: false
String script = """
export DB_HOST=${DB_HOST:-dind}
export DB_PORT=${DB_PORT:-5432}
export DB_USERNAME=${DB_USERNAME:-dba}
export DB_PASSWORD=${DB_PASSWORD:-secretsecret}
export DB_NAME=${DB_NAME:-notebook-db}
docker network inspect notebooknet >/dev/null 2>&1 || docker network create notebooknet
echo "--> Reset du conteneur (Immuabilité)..."
# 1. Force la suppression pour garantir l'application de la config env
docker rm -f db >/dev/null 2>&1
# 2. Lancement frais sans volume (plus rapide, plus stable pour les tests)
# On utilise --tmpfs pour des performances optimales en RAM
docker run --quiet -d --name db \\
--env POSTGRES_USER=${DB_USERNAME} \\
--env POSTGRES_PASSWORD=${DB_PASSWORD} \\
--env POSTGRES_DB=${DB_NAME} \\
--network=notebooknet \\
--tmpfs /var/lib/postgresql/data \\
--health-cmd "pg_isready -U ${DB_USERNAME} -d ${DB_NAME}" \\
--health-interval 2s \\
--health-timeout 2s \\
--health-retries 10 \\
-p 5432:5432 \\
postgres:17
# 3. Attente avec Timeout (30 tentatives de 0.5s = 15s max)
echo "--> Attente de la disponibilité réseau sur ${DB_HOST}..."
TIMEOUT=30
ITER=0
# L'ASTUCE : On utilise -h pour tester la connexion réseau
# depuis le point de vue du réseau, pas juste en local.
until docker exec db pg_isready -h ${DB_HOST} -U ${DB_USERNAME} -d ${DB_NAME} >/dev/null 2>&1 || [ $ITER -eq $TIMEOUT ]; do
sleep 0.5
ITER=$((ITER + 1))
done
if [ $ITER -eq $TIMEOUT ]; then
echo "ERREUR : La base sur ${DB_HOST} ne répond pas."
exit 1
fi
""";
IJava.getKernelInstance().getMagics().applyCellMagic("shell", List.of(""), script);
String script = """
PROVIDER="github"
REPO="ebpro/sample-hellojpa"
BRANCH="develop"
gitpull.sh --provider github --quiet \
--branch ${BRANCH} ${REPO} \
--message "Les exemples suivants sont accessibles dans le dépôt :"
source get_src_dir.sh ${PROVIDER} ${REPO}
cd ${SRC_DIR}
./mvnw package -DskipTests -T 1C
""";
IJava.getKernelInstance().getMagics().applyCellMagic("shell", List.of(""), script);
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
// On récupère le contexte Logback
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
// On fait taire spécifiquement le logger qui pose problème à Quarto
loggerContext.getLogger("org.hibernate.orm.jdbc.warn").setLevel(Level.ERROR);
// On peut aussi calmer Hibernate globalement pour plus de clarté
loggerContext.getLogger("org.hibernate").setLevel(Level.WARN);
Logger log = LoggerFactory.getLogger("notebook");
// SET DB CONNECTION PROPERTIES
System.setProperty("db.username","dba");
System.setProperty("db.password","secretsecret");
System.setProperty("db.name","notebook-db");
System.setProperty("db.url","jdbc:postgresql://dind/notebook-db");
//LOAD SAMPLE CLASSES
%jars "/home/jovyan/work/examples/github/ebpro/sample-hellojpa/target/*.jar"
%jars "/home/jovyan/work/examples/github/ebpro/sample-hellojpa/target/lib/*.jar"
//INIT THE DATABASE
import jakarta.persistence.*;
import fr.univtln.bruno.demos.jpa.hello.DatabaseManager;
// 1. Sécurité : On ferme une éventuelle session précédente si le kernel a survécu
try { DatabaseManager.close(); } catch(Exception e) {}
// 2. Initialisation avec Retry
EntityManagerFactory emf = null;
int maxAttempts = 5;
for (int i = 1; i <= maxAttempts; i++) {
try {
// Déclenche l'initialisation Lazy
emf = DatabaseManager.getEntityManagerFactory();
// Validation réelle de la connexion
try (EntityManager em = emf.createEntityManager()) {
System.out.println("--> Connexion JPA réussie (Tentative " + i + ") !");
break;
}
} catch (Throwable t) {
if (i == maxAttempts) {
System.err.println("!!! Échec définitif après " + i + " tentatives");
throw t;
}
System.out.println("... Base non prête, nouvelle tentative dans 2s (" + i + "/" + maxAttempts + ")");
Thread.sleep(2000);
}
}
// 3. Setup final
System.setProperty("java.awt.headless", "true");
System.setProperty("plantuml.fontpath", "/usr/share/fonts/truetype/dejavu");
Logger log = LoggerFactory.getLogger("notebook");
```