Skip to content

Commit

Permalink
0.4 release - patches
Browse files Browse the repository at this point in the history
  • Loading branch information
cerebrate committed Sep 12, 2021
1 parent dcf1868 commit 837560a
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/drop_cache_if_idle
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,26 @@ def load_check():

def last_run_check():
try:
mod_time_since_epoch = os.path.getmtime('/root/drop_caches_last_run')
mod_time_since_epoch = os.path.getmtime('/run/drop_caches_last_run')
current_time_since_epoch = time.time()
last_run_time_in_seconds = current_time_since_epoch - mod_time_since_epoch
print(
f"INFO: The last run was before {round(last_run_time_in_seconds)} {'second' if round(last_run_time_in_seconds) <= 1 else 'seconds'}.")

except OSError:
print("INFO: Path '/root/drop_caches_last_run' does not exists or is inaccessible. Continuing...")
print("INFO: Path '/run/drop_caches_last_run' does not exists or is inaccessible. Continuing...")
except Exception as e:
print(
f"ERROR: The last run of the command is cannot be checked due to an unknown cause: {e}")
f"ERROR: The last run time of the command cannot be checked due to an unknown cause: {e}")


def apply():
try:
out = subprocess.run(
"sync; echo 3 > /proc/sys/vm/drop_caches; touch /root/drop_caches_last_run", shell=True)
"sync; echo 3 > /proc/sys/vm/drop_caches; touch /run/drop_caches_last_run", shell=True)
except Exception as e:
print(
f"ERROR: The command is cannot be executed due to an unknown cause: {e}")
f"ERROR: The command cannot be executed due to an unknown cause: {e}")
return False
else:
if isinstance(out, subprocess.CompletedProcess):
Expand All @@ -74,15 +74,14 @@ def main():
last_run_check()

# Check load and execute if load is low
is_apply_succeed = False
if load_check():
is_apply_succeed = apply()

# Let's give the proper output to the user in case of failure
if is_apply_succeed:
print("INFO: The command is executed successfully.")
else:
print("INFO: There was an error preventing the command being executed.")
# Let's give the proper output to the user in case of failure
if is_apply_succeed:
print("INFO: The command is executed successfully.")
else:
print("INFO: There was an error preventing the command being executed.")

exit(0)

Expand Down

0 comments on commit 837560a

Please sign in to comment.