Skip to content

Commit e64e244

Browse files
authored
Merge pull request #101 from TDT-AG/tdt-bootup-delay
Add random bootup delay #100
2 parents 8fc3389 + 134e1a3 commit e64e244

5 files changed

Lines changed: 34 additions & 4 deletions

File tree

README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ UCI configuration options must go in ``/etc/config/openwisp``.
7171
- ``key``: key required to download the configuration
7272
- ``hardware_id_script``: custom script to read out a hardware id (e.g. a serial number), read more about this feature in `Hardware ID`_
7373
- ``hardware_id_key``: whether to use the hardware id for key generation or not, defaults to ``1``
74+
- ``bootup_delay``: maximum value in seconds of a random delay after bootup, defaults to ``0``, see `Bootup Delay`_
7475
- ``unmanaged``: list of config sections which won't be overwritten, see `Unmanaged Configurations`_
7576
- ``capath``: value passed to curl ``--capath`` argument, by default is empty; see also `curl capath argument <https://curl.haxx.se/docs/manpage.html#--capath>`_
7677
- ``cacert``: value passed to curl ``--cacert`` argument, by default is empty; see also `curl cacert argument <https://curl.haxx.se/docs/manpage.html#--cacert>`_
@@ -166,6 +167,16 @@ generation then set ``hardware_id_key`` to ``0``.
166167

167168
For settings in ``django-netjsonconfig`` related to the hardware id, take a look at the `README <https://github.com/openwisp/django-netjsonconfig/#netjsonconfig-hardware-id-enabled>`_
168169

170+
Bootup Delay
171+
------------
172+
173+
The option ``bootup_delay`` can be used to make the agent wait for a random amount of seconds after the bootup of
174+
the device. Allowed random values range from 0 up to the value of ``bootup_delay``. The delay is applied only after the
175+
device has been registered.
176+
177+
The random bootup delay reduces the load on the OpenWISP controller when a large amount of devices boot up at the
178+
same time after a power failure, all trying to connect to the controller.
179+
169180
Unmanaged Configurations
170181
------------------------
171182

openwisp-config/files/openwisp-nossl.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ config controller 'http'
1414
#option test_script '/usr/sbin/mytest'
1515
#option hardware_id_script '/usr/sbin/read_hw_id'
1616
#option hardware_id_key '1'
17+
#option bootup_delay '0'
1718
option uuid ''
1819
option key ''
1920
# curl options

openwisp-config/files/openwisp-ssl.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ config controller 'http'
1414
#option test_script '/usr/sbin/mytest'
1515
#option hardware_id_script '/usr/sbin/read_hw_id'
1616
#option hardware_id_key '1'
17+
#option bootup_delay '0'
1718
option uuid ''
1819
option key ''
1920
# curl options

openwisp-config/files/openwisp.agent

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ while [ -n "$1" ]; do
1313
--consistent-key) export CONSISTENT_KEY="$2"; shift;;
1414
--hardware-id-script) export HARDWARE_ID_SCRIPT="$2"; shift;;
1515
--hardware-id-key) export HARDWARE_ID_KEY="$2"; shift;;
16+
--bootup-delay) export BOOTUP_DELAY="$2"; shift;;
1617
--merge-config) export MERGE_CONFIG="$2"; shift;;
1718
--unmanaged) export UNMANAGED="$2"; shift;;
1819
--test-config) export TEST_CONFIG="$2"; shift;;
@@ -61,6 +62,7 @@ MERGE_CONFIG=${MERGE_CONFIG:-1}
6162
TEST_CONFIG=${TEST_CONFIG:-1}
6263
CONSISTENT_KEY=${CONSISTENT_KEY:-1}
6364
HARDWARE_ID_KEY=${HARDWARE_ID_KEY:-1}
65+
BOOTUP_DELAY=${BOOTUP_DELAY:-0}
6466
CONNECT_TIMEOUT=${CONNECT_TIMEOUT:-15}
6567
MAX_TIME=${MAX_TIME:-30}
6668
MAC_INTERFACE=${MAC_INTERFACE:-eth0}
@@ -76,6 +78,7 @@ REGISTRATION_PARAMETERS="$WORKING_DIR/registration_parameters"
7678
TEST_CHECKSUM="$WORKING_DIR/test_checksum"
7779
STATUS_REPORT="$WORKING_DIR/status_report"
7880
APPLYING_CONF="$WORKING_DIR/applying_conf"
81+
BOOTUP="$WORKING_DIR/bootup"
7982
REGISTRATION_URL="$URL/controller/register/"
8083
UNMANAGED_DIR="$WORKING_DIR/unmanaged"
8184
FETCH_COMMAND="curl -s --connect-timeout $CONNECT_TIMEOUT --max-time $MAX_TIME"
@@ -557,6 +560,18 @@ if [ -z "$UUID" ] || [ -z "$KEY" ]; then
557560
do
558561
sleep $(expr $INTERVAL / 4)
559562
done
563+
touch "$BOOTUP"
564+
else
565+
# add delay only after bootup, not after registration or restart
566+
if [ "$BOOTUP_DELAY" != "0" ] && [ ! -f "$BOOTUP" ]; then
567+
# random bootup delay between 0..300 seconds
568+
DELAY="$(($RANDOM%$BOOTUP_DELAY))"
569+
logger "Delaying startup for $DELAY seconds..." \
570+
-t openwisp \
571+
-p daemon.info
572+
sleep $DELAY
573+
touch "$BOOTUP"
574+
fi
560575
fi
561576

562577
# these variables are evaluated here because "register()" might set UUID and KEY

openwisp-config/files/openwisp.init

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ start_service() {
1717
consistent_key=$(config_get http consistent_key)
1818
hardware_id_script=$(config_get http hardware_id_script)
1919
hardware_id_key=$(config_get http hardware_id_key)
20+
bootup_delay=$(config_get http bootup_delay)
2021
unmanaged=$(config_get http unmanaged)
2122
merge_config=$(config_get http merge_config)
2223
test_config=$(config_get http test_config)
@@ -39,6 +40,7 @@ start_service() {
3940
if [ $consistent_key ]; then consistent_key="--consistent-key $consistent_key"; fi
4041
if [ $hardware_id_script ]; then hardware_id_script="--hardware-id-script $hardware_id_script"; fi
4142
if [ $hardware_id_key ]; then hardware_id_key="--hardware-id-key $hardware_id_key"; fi
43+
if [ $bootup_delay ]; then bootup_delay="--bootup-delay $bootup_delay"; fi
4244
if [ -n "$unmanaged" ]; then
4345
# replace spaces with commas to avoid problems when
4446
# passing this arg to procd_set_param command
@@ -75,10 +77,10 @@ start_service() {
7577
procd_open_instance
7678
procd_set_param command $PROG $url $interval $verify_ssl $uuid $key $shared_secret \
7779
$consistent_key $hardware_id_script $hardware_id_key \
78-
$unmanaged $merge_config $test_config $test_script \
79-
$connect_timeout $max_time $capath $cacert $mac_interface \
80-
$management_interface $pre_reload_hook $post_reload_hook \
81-
$post_registration_hook
80+
$bootup_delay $unmanaged $merge_config $test_config \
81+
$test_script $connect_timeout $max_time $capath $cacert \
82+
$mac_interface $management_interface $pre_reload_hook \
83+
$post_reload_hook $post_registration_hook
8284
procd_set_param respawn
8385
procd_close_instance
8486
logger -s "$PROG_NAME started" \

0 commit comments

Comments
 (0)