From 07a9f4bf4dcb454833ab05cd0554302b6883731d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 30 Dec 2024 13:19:45 +0100 Subject: [PATCH] set up cronjob and timer --- handlers/main.yml | 4 ++++ tasks/chill/main.yml | 8 ++++++++ tasks/main.yml | 14 ++++++++++++++ templates/compose.yaml | 11 ++++++----- templates/systemd/chill-cronjob@.service | 9 +++++++++ templates/systemd/chill-cronjob@.timer | 10 ++++++++++ 6 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 templates/systemd/chill-cronjob@.service create mode 100644 templates/systemd/chill-cronjob@.timer diff --git a/handlers/main.yml b/handlers/main.yml index 03a24d8..961d0e1 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -7,6 +7,10 @@ state: present recreate: auto +- name: Reload systemd + ansible.builtin.systemd: + daemon_reload: true + # - name: Restart chill {{ item.chill_environment }} # community.docker.docker_compose_v2: # project_src: "{{ install_dir }}/{{ item['chill_environment'] }}" diff --git a/tasks/chill/main.yml b/tasks/chill/main.yml index 80c5629..96cfb16 100644 --- a/tasks/chill/main.yml +++ b/tasks/chill/main.yml @@ -66,3 +66,11 @@ group: "82" mode: '0766' state: directory + +# racing condition: on first execution, this is not available... +# +# - name: Ensure systemd timer for cronjob is up +# ansible.builtin.systemd_service: +# name: "chill-cronjob@{{ item['chill_environment'] }}.timer" +# state: restarted +# enabled: true diff --git a/tasks/main.yml b/tasks/main.yml index c3d72ae..3199474 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -40,6 +40,20 @@ - name: Install traefik ansible.builtin.include_tasks: traefik.yml +- name: Install systemd services + ansible.builtin.template: + src: "systemd/{{ file }}" + dest: "/etc/systemd/system/{{ file }}" + owner: root + group: root + mode: '0766' + notify: Reload systemd + loop: + - chill-cronjob@.service + - chill-cronjob@.timer + loop_control: + loop_var: file + - name: Install individual chill ansible.builtin.include_tasks: chill/main.yml with_items: "{{ chills }}" diff --git a/templates/compose.yaml b/templates/compose.yaml index 8a73966..45a14d1 100644 --- a/templates/compose.yaml +++ b/templates/compose.yaml @@ -46,6 +46,7 @@ services: post_start: - command: - rm + - -f - "/var/www/app/config/packages/chill_doc_store.yaml" user: root @@ -67,17 +68,17 @@ services: - "-c" - "touch /tmp/kill_me && bin/console messenger:stop-workers" + cron: + <<: *defaultApp + entrypoint: "/usr/bin/env" + command: ["bin/console", "chill:cron-job:execute", "-v"] + {% if item.add_postgres %} database: image: "{{ database_image }}" env_file: - postgres.env - environment: - POSTGRES_DB: ${POSTGRES_DB:-app} - # You should definitely change the password in production - POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-!ChangeMe!} - POSTGRES_USER: ${POSTGRES_USER:-app} volumes: - ./docker/db/data:/var/lib/postgresql/data:rw networks: diff --git a/templates/systemd/chill-cronjob@.service b/templates/systemd/chill-cronjob@.service new file mode 100644 index 0000000..0cedda9 --- /dev/null +++ b/templates/systemd/chill-cronjob@.service @@ -0,0 +1,9 @@ +[Unit] +Description=Execute cron job for chill with environment %i + +[Service] +User={{ as_user }} +ExecStart=/usr/bin/docker compose --file {{ install_dir }}/%i/compose.yaml up cron +Type=simple +# execute maximum 30 minutes +RuntimeMaxSec=1800 \ No newline at end of file diff --git a/templates/systemd/chill-cronjob@.timer b/templates/systemd/chill-cronjob@.timer new file mode 100644 index 0000000..13e5782 --- /dev/null +++ b/templates/systemd/chill-cronjob@.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Run chill cron-jobs + +[Timer] +Unit=chill-cronjob@%i.service +OnCalendar=*-*-* *:0,15,30,45:00 +RandomizedDelaySec=60 + +[Install] +WantedBy=multi-user.target \ No newline at end of file