Context
I am developing a service for macOS that runs in the background and periodically performs certain tasks.
The service is restarted automatically by launchd
whenever it crashes or when it is automatically updated.
I have noticed that when the machine is asleep it will still execute programs during 1 or 2 minutes per hour.
Objective
My objective is to interrupt the execution of those tasks whenever the host is in the sleep state. For that I am processing the sleep and wake up notifications the system sends the application(Apple Docs: Registering and unregistering for sleep and wake notifications).
Problem
The problem I have found is that if the service is updated or it restarts while the system is in the sleep state, the service starts and assumes the state is "awake". Thus it tries to run things it shouldn't.
I am wondering if there is a way to query the system if it is in the "sleep" or "awake" state.