ansible-collection-mac icon indicating copy to clipboard operation
ansible-collection-mac copied to clipboard

Explore using `retry` instead of 7-second pause for dockutil changes

Open geerlingguy opened this issue 3 years ago • 5 comments

After #43, we added a 7-second pause between dock item changes since the commands can sometimes fail with an error like "Move failed for 'App Name' in com.apple.dock.plist" since Ansible runs the command before the Dock has completed its restart. Annoyingly, it's something that would be better handled by dockutil not exiting until the operation is complete/confirmed... but it is what it is.

If we used retry with like a 1 or 2 second backoff to the tasks that can fail (e.g. "Move dock item to the correct position"), it might go faster when you have a bunch of dock items to move.

geerlingguy avatar Jul 28 '22 21:07 geerlingguy

This issue has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution!

Please read this blog post to see the reasons why I mark issues as stale.

stale[bot] avatar Oct 30 '22 11:10 stale[bot]

This issue is no longer marked for closure.

stale[bot] avatar Oct 31 '22 01:10 stale[bot]

I looked at #43 and see that it is closed so I'm adding my comments here. An alternative way to approach adding items via dockutil and avoid the 7 seconds wait between each change is to use the --no-restart option for all but the last item. I'm not sure when this option was added but it is present now.

If the task in the main playbook is modified to include extended loop information like this:

- name: Ensure required dock items exist.
  ansible.builtin.include_tasks: dock-add.yml
  loop: "{{ dockitems_persist }}"
  loop_control:
    extended: true
    extended_allitems: false
  tags: ['dock']

The task in the add playbook can be modified to take advantage of the ansible_loop.last variable like this:

- name: Ensure Dock item {{ item.name | default(item) }} exists.
  ansible.builtin.command: "dockutil --add '{{ item.path }}' --label '{{ item.name }}' {% if ansible_loop.last is sameas true %}--no-restart{% endif %}"
  when: dockitem_exists.rc >0 or
        dockitem_exists.rc == 0 and current_section == 'recent-apps'
  tags: ['dock']

I've not used ansible in a while so my syntax above could be incorrect. Please feel free to correct / improve the suggestion.

karthicraghupathi avatar Jul 05 '23 04:07 karthicraghupathi

@karthicraghupathi - Oh... something like that could work, and would make the setup of the Dock a bit more robust (I hope). If someone can work on a PR implementing that change it could be good to go. I haven't had a chance to dig into that or any other little issues on this project in a couple months.

geerlingguy avatar Jul 07 '23 23:07 geerlingguy

@geerlingguy Thank you for taking a look. I will look to submit a PR soon. Things are a bit hectic at work atm...

karthicraghupathi avatar Jul 09 '23 00:07 karthicraghupathi