Skip to content

Commit

Permalink
Merge pull request #4 from sitewards/adhoc-better-cron-setup
Browse files Browse the repository at this point in the history
Adhoc better cron setup
  • Loading branch information
aboritskiy authored Aug 19, 2019
2 parents 729eabc + 1fc3eeb commit 782c06d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
10 changes: 8 additions & 2 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ magento_group: "www-data"

## The name for the service that executes magento. When the application is updated, the PHP runtime must be reset so that
## any data that's in the opcache.
magento_php_service: "php7.0-fpm"
magento_php_service: "php7.2-fpm"

## Whether or not to skip the release section of Magento 2. Note: THe configuration and env.php file will still be
## overrideen; just the installation itself will not be managed.
Expand Down Expand Up @@ -278,4 +278,10 @@ magento_cache_types:
## During the upgrade of Magneto, there are certain commands that must be run to ensure the database is up to date
## and that content is successfully migrated. These commands are run every release, and are expected to be idempotent.
magento_upgrade_commands:
- "setup:upgrade --keep-generated"
- "setup:upgrade --keep-generated"

## In case you want to manage queue consumers separately - activate that
## See https://magento.stackexchange.com/questions/285385/magento-2-3-2-ce-and-cron-jobs-flock-and-rabbitmq/285386#285386
magento_run_message_queue_consumers_separately: false

magento_queue_consumers_cron_template: "/usr/bin/php {{ magento_app_root }}/bin/magento queue:consumers:start {{ item.name }}"
23 changes: 21 additions & 2 deletions tasks/installation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
disabled: "yes"
with_items: "{{ magento_cron_tasks }}"

- name: "Wait untill eventually running cron is finished"
become: "yes"
become_user: "{{ magento_user }}"
command: "flock /run/lock/{{ item.code }}.lock echo '{{ item.code }} has finished.'"
with_items: "{{ magento_cron_tasks }}"

- name: "Get the magento filename from the storage path"
set_fact:
magento_file_name: "{{ magento_storage_path | basename }}"
Expand All @@ -41,7 +47,6 @@
when:
- not magento_release_stat.stat.exists
- magento_skip_release == False

- name: "Set the workdir for later tasks"
set_fact:
magento_workdir: "{{ tmpdir.path }}"
Expand Down Expand Up @@ -142,7 +147,7 @@
# var/sitewards/path/to/resource
#
# where "sitewards/path/to" does not exist.
- name: "Ensure the destion container directories exist"
- name: "Ensure the destination container directories exist"
file:
path: "{{ item.dest | dirname }}"
state: "directory"
Expand Down Expand Up @@ -176,6 +181,20 @@
- magento_release_updated.changed
- magento_skip_release == False

- name: "Check if Magento database exists"
shell:
cmd: |
mysql --user="{{ magento_config_db.username }}" --password="{{ magento_config_db.password }}" --database="{{ magento_config_db.name }}" --host="{{ magento_config_db.host }}" <<EOF
SHOW TABLES LIKE 'core_config_data';
EOF
register: magento_db_status

- name: "Create the Magento database if missing"
become: "yes"
become_user: "{{ magento_user }}"
command: "/usr/bin/php {{ magento_app_root }}/bin/magento setup:db-schema:upgrade"
when: magento_db_status.stdout_lines|length == 0

- name: "Perform any schema upgrades that are required"
become: "yes"
become_user: "{{ magento_user }}"
Expand Down
1 change: 1 addition & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
- include: "dependencies.yml"
- include: "setup.yml"
- include: "installation.yml"
- include: "message-queue-consumers.yml"
- include: "admin.yml"
32 changes: 32 additions & 0 deletions tasks/message-queue-consumers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
- name: "Get the list of magento message queue message consumers"
become: "yes"
become_user: "{{ magento_user }}"
command: "/usr/bin/php {{ magento_app_root }}/bin/magento queue:consumers:list"
register: magento_queue_consumers_list_output

- set_fact:
code: "magento_mq_{{ item | regex_replace('[^A-Za-z]', '_') | lower }}"
name: "{{ item }}"
with_items: "{{ magento_queue_consumers_list_output.stdout_lines }}"
register: magento_queue_consumers_list_wrapped

- set_fact:
magento_queue_consumers_list: "{{ magento_queue_consumers_list_wrapped|json_query('results[*].ansible_facts') }}"

- name: "List message queue consumers that are going to be verified (i.e. installed or removed)"
debug:
var: magento_queue_consumers_list

- name: "Setup separate cron task for each magento message consumer"
cron:
name: "Magento MQ Consumer:{{ item.code }}"
user: "{{ magento_user }}"
minute: "*"
hour: "*"
day: "*"
month: "*"
job: "{{ magento_cron_invoker }} {{ magento_queue_consumers_cron_template }}; {{ magento_cron_responder }}"
disabled: "{{ not magento_run_message_queue_consumers_separately }}"
with_items: "{{ magento_queue_consumers_list }}"

19 changes: 19 additions & 0 deletions templates/etc/env.php.j2
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,23 @@ return array (
array (
'date' => '{{ magento_install_date }}',
),
{% if magento_queue_amqp %}
'queue' =>
array (
'amqp' =>
array (
'host' => '{{ magento_queue_amqp.host }}',
'port' => '{{ magento_queue_amqp.port }}',
'user' => '{{ magento_queue_amqp.user }}',
'password' => '{{ magento_queue_amqp.password }}',
'virtualhost' => '{{ magento_queue_amqp.virtualhost }}',
'ssl' => '{{ magento_queue_amqp.ssl }}',
),
),
{% endif %}
{% if magento_run_message_queue_consumers_separately %}
'cron_consumers_runner' => array(
'cron_run' => false
)
{% endif %}
);

0 comments on commit 782c06d

Please sign in to comment.