From 2f874653bfd8bab0d870b0375ecb9840bef08815 Mon Sep 17 00:00:00 2001 From: Matchu Date: Wed, 3 Nov 2021 15:43:37 -0700 Subject: [PATCH] Update pm2 tasks to update the config correctly Previously, if you changed the pm2 ecosystem file content, it wouldn't actually be reflected in the running services. Now it will be! --- deploy/playbooks/setup.yml | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/deploy/playbooks/setup.yml b/deploy/playbooks/setup.yml index b7cf6b4..5e00605 100644 --- a/deploy/playbooks/setup.yml +++ b/deploy/playbooks/setup.yml @@ -65,7 +65,7 @@ - name: Create pm2 ecosystem file copy: - content: > + content: | module.exports = { apps: [ { @@ -78,12 +78,26 @@ } ] } - dest: "/srv/impress-2020/ecosystem.config.js" + dest: "~/ecosystem.config.js" + # Create a temporary backup file, so we can use it to delete the old + # version of the services. (This is important if e.g. a service is + # removed or renamed, in which case deleting from the *new* config file + # wouldn't include it.) + backup: yes + register: pm2_ecosystem_file - - name: Start pm2 ecosystem (even if the app isn't ready yet) - command: - chdir: "/srv/impress-2020" - cmd: "pm2 start ecosystem.config.js" + - name: Delete old pm2 services if config file changed + command: "pm2 delete {{ pm2_ecosystem_file.backup_file | quote }}" + when: pm2_ecosystem_file is changed and pm2_ecosystem_file.backup_file is defined + + - name: Delete old pm2 config file if it changed + file: + path: "{{ pm2_ecosystem_file.backup_file }}" + state: absent + when: pm2_ecosystem_file is changed and pm2_ecosystem_file.backup_file is defined + + - name: Start pm2 services + command: "pm2 start ~/ecosystem.config.js" - name: Save pm2 startup script command: pm2 save