Skip to content

Commit

Permalink
Fixed help text & added documentation for Cron Jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
wes-otf committed Sep 18, 2024
1 parent 899760a commit f792577
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
29 changes: 29 additions & 0 deletions docs/setup/administrators/cron-jobs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Possible Cron Commands

Hypha comes stock with management commands that can be utilized in tandem with a job scheduler to automate specific tasks

## Account Cleanup

Accounts that haven't been logged into in 5 months can be marked as inactive with the following command:

```shell
python3 manage.py accounts_cleanup
```

## Drafts Cleanup

Drafts that haven't been modified in a specified time (in days) can be deleted with the following command:

```shell
python3 manage.py drafts_cleanup [days]

# Or, to run without a confirmation prompt

python3 manage.py drafts_cleanup [days] --noinput
```

Example: to delete all drafts that haven't been modified in a year without a confirmation prompt:

```shell
python3 manage.py drafts_cleanup 365 --noinput
```
21 changes: 14 additions & 7 deletions hypha/apply/funds/management/commands/drafts_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
from hypha.apply.funds.workflow import DRAFT_STATE


def check_not_negative(value):
def check_not_negative(value) -> int:
"""Used to validate `older_than_days` argument
Needs to be a non-negative int
Args:
value: Argument to be validated
Returns:
int: Valid non-negative value
Raises:
argparse.ArgumentTypeError: if not non-negative integer
"""
try:
ivalue = int(value)
Expand All @@ -27,14 +34,14 @@ def check_not_negative(value):


class Command(BaseCommand):
help = "Delete all drafts that are older than 2 years"
help = "Delete all drafts that are older than the specified time in days"

def add_arguments(self, parser):
parser.add_argument(
"older_than_days",
action="store",
type=check_not_negative,
help="Clear all applications older than the days specified here",
help="Time in days to delete drafts older than",
)
parser.add_argument(
"--noinput",
Expand All @@ -60,21 +67,21 @@ def handle(self, *args, **options):

if not (draft_count := old_drafts.count()):
self.stdout.write(
f"No drafts older than {older_than} day{"s" if older_than > 1 else ""} exist."
f"No drafts older than {older_than} day{'s' if older_than > 1 else ''} exist."
)
return

if interactive:
confirm = input(
f"This action will permanently delete {draft_count} draft{"s" if draft_count != 1 else ""}.\nAre you sure you want to do this?\n\nType 'yes' to continue, or 'no' to cancel: "
f"This action will permanently delete {draft_count} draft{'s' if draft_count != 1 else ''}.\nAre you sure you want to do this?\n\nType 'yes' to continue, or 'no' to cancel: "
)
else:
confirm = "yes"

if confirm == "yes":
old_drafts.delete()
self.stdout.write(
f"{draft_count} draft{"s" if draft_count != 1 else ""} deleted."
f"{draft_count} draft{'s' if draft_count != 1 else ''} deleted."
)
else:
self.stdout.write("Deletion cancelled.")

0 comments on commit f792577

Please sign in to comment.