Skip to main content
  1. Note/

Push-based CLI Workflow on MacOS

·3 mins· ·
Blog En
Liu Zhe You
Author
Liu Zhe You
Skilled in full-stack development and DevOps, currently focusing on Backend.
Table of Contents

Push-based CLI Workflow on MacOS
#

Push-based vs. Pull-based in System Design
#

In system design, when you need to monitor the state of data—such as whether an entity has been updated or if a long-running job has completed—there are generally two models: Push-based and Pull-based.

sequenceDiagram participant Client as Client participant Server as Server Note over Client, Server: Pull-Based (Client keeps polling) rect rgba(255, 255, 255, 0.31) loop Repeatedly Client->>Server: Request state Server-->>Client: Respond with state end end Note over Client, Server: Push-Based (Server notifies Client) rect rgba(255, 255, 255, 0.31) Server->>Server: State changes Server-->>Client: Notify update end

The upper part represents Pull-based, while the lower part represents Push-based.

It is clear that:

  • Pull-based requires the client to continuously query the server for the current state.
  • Push-based has the server proactively notify the client only when the state changes.

Context Switching in CLI Workflows
#

When using the CLI, you often run commands that take a long time to execute—for instance, a test that runs for several minutes or a build command.

In these situations, you might switch to other tasks but still need to repeatedly check the command’s execution status, resulting in unnecessary context switching.

Reducing Context Switching with Push-based Notifications
#

Imagine if the CLI could actively notify you when a command finishes. Just like a push-based system, you wouldn’t need to repeatedly check the command’s status.

That’s why there should be a simple script to actively notify you once a CLI command completes.

For example, check out this snippet from my dotfiles/.zshrc at ee0772 · jason810496/dotfiles:

144
145
146
147
148
149
150
151
152
153
154
155
# Utils
notify() {
  # Usage: <your long-running command>; notify
  # Useful for docker build, etc.
  if [[ $? -eq 0 ]]; then
    osascript -e 'display notification "✅ Success!" with title "Command Completed"'
    afplay /System/Library/Sounds/Glass.aiff
  else
    osascript -e 'display notification "❌ Failed!" with title "Command Failed"'
    afplay /System/Library/Sounds/Basso.aiff
  fi
}

Currently, I add the notify script to my .zshrc (which uses macOS’s osascript to invoke display notification).

How to Use
#

For example, when building a project, you might chain commands using ; rather than && or || so that you can capture the build result (i.e., to know whether the build succeeded or failed):

make build ; notify "Build done"

After completion, the following notifications will appear in the top right corner:

On success:

notify-success

On failure:

notify-failed

Conclusion
#

By using push-based notifications, you can reduce context switching in your CLI workflows, allowing you to concentrate more on other tasks. If you have similar needs, consider implementing a solution based on the script above!

I often use push-based notifications when contributing to Apache Airflow. For instance, when rebuilding the Kubernetes Docker image:

breeze k8s build-k8s-image --rebuild-base-image ; notify

For long-running commands like these, you can focus on other work without constantly monitoring the terminal.

For example, while writing this article, I was actually building the k8s image xD

Related

What is an Embedded Database? A brief introduction to RocksDB
·3 mins
Blog En Database
A brief introduction to Embedded Database and concept of RocksDB
2024 Dcard Summer Intern Interview
·5 mins
Blog En Intern
2024 Dcard Summer Intern Interview
NCKU CSIE Freshman First Semester
·10 mins
Blog En
What did I do in the first semester of freshman year?
Life Is Short Use 'uv'
·1 min
Blog En Python
How to use uv to manage Python packages and projects
2024 Appier Summer Internship Reflection
·12 mins
Blog En Intern
Reflection on my backend summer internship in the Data Platform department at Appier
2024 Dcard Backend Intern Assignment
·7 mins
Blog En Backend Intern System-Design Dcard
2024 Dcard Backend Intern Assignment