thingsboard / thingsboard-edge

Apache License 2.0
93 stars 71 forks source link

systemd tb-edge.service fail to start at (re)boot #106

Closed AdrienAdB closed 2 months ago

AdrienAdB commented 2 months ago

Description

Not all reboot will be affected, but we realized tb-edge was not able to start time to time. Seems postgresql or network wasn't ready when systemd initialized tb-edge.service

Extract from /var/log/tb-edge/tb-edge.log

2024-04-30 10:16:58,491 [main] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 08001
2024-04-30 10:16:58,491 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
2024-04-30 10:16:58,499 [main] WARN  o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatServletWebServerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedTomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory]: Factory method 'tomcatServletWebServerFactory' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'httpTransportContext': Unsatisfied dependency expressed through field 'transportService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultTransportService' defined in URL [jar:file:/usr/share/tb-edge/bin/tb-edge.jar!/BOOT-INF/lib/transport-api-3.6.4EDGE.jar!/org/thingsboard/server/common/transport/service/DefaultTransportService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hashPartitionService': Invocation of init method failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection

We could fix issue by adding "network.target" and "postgresql.service" in tb-edge.service file However, it is being erased on upgrades.

cat /etc/systemd/system/multi-user.target.wants/tb-edge.service

[Unit]
Description=tb-edge

# added network.target postgresql.service
After=syslog.target network.target postgresql.service

[Service]
User=thingsboard
ExecStart=/usr/share/tb-edge/bin/tb-edge.jar
SuccessExitStatus=143

# Auto-restart service after 300 seconds
Restart=always
RestartSec=300

[Install]
WantedBy=multi-user.target

Question

Environment

volodymyr-babak commented 2 months ago

@AdrienAdB

Hello, thank you for reporting the issue and for your interest in the Edge Product.

Could you please check this file? https://github.com/thingsboard/thingsboard-edge/blob/master/packaging/java/scripts/control/template.service

I believe it contains what you are looking for. Your pull request would be greatly appreciated!

AdrienAdB commented 2 months ago

Thanks @volodymyr-babak, Here is the pull request: https://github.com/thingsboard/thingsboard-edge/pull/107

volodymyr-babak commented 2 months ago

@AdrienAdB

Thank you for your pull request. It has been merged successfully. I've made a couple of adjustments: I changed the default RestartSec to 30 seconds and added some additional comments for clarity. Everything else remains as it was originally.