Skip to content

Commit

Permalink
Added support for Nth day of the Month
Browse files Browse the repository at this point in the history
  • Loading branch information
AnykeyNL committed Jun 13, 2022
1 parent bb9e25a commit d33eed5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
22 changes: 18 additions & 4 deletions AutoScaleALL.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
current_host_time = datetime.datetime.today()
current_utc_time = datetime.datetime.utcnow()


##########################################################################
# Print header centered
##########################################################################
Expand Down Expand Up @@ -98,9 +97,20 @@ def get_current_hour(region, ignore_region_time=False):
iCurrentHour = current_time.hour
iDayOfMonth = current_time.date().day # Day of the month as a number

return iDayOfWeek, iDay, iCurrentHour, iDayOfMonth
# Get what N-th day of the monday it is. Like 1st, 2nd, 3rd Saturday
cal = calendar.monthcalendar(current_time.year, current_time.month)
iDayNr = 0
daynrcounter = 0
for week in cal:
if cal[daynrcounter][iDayOfWeek] == current_time.day:
if cal[0][iDayOfWeek]:
iDayNr = daynrcounter + 1
else:
iDayNr = daynrcounter

daynrcounter = daynrcounter + 1

return iDayOfWeek, iDay, iCurrentHour, iDayOfMonth, iDayNr


##########################################################################
Expand Down Expand Up @@ -369,14 +379,14 @@ def autoscale_region(region):
###############################################
# Get Current Day, time
###############################################
DayOfWeek, Day, CurrentHour, CurrentDayOfMonth = get_current_hour(region, cmd.ignore_region_time)
DayOfWeek, Day, CurrentHour, CurrentDayOfMonth, DayNr = get_current_hour(region, cmd.ignore_region_time)

if AlternativeWeekend:
MakeLog("Using Alternative weekend (Friday and Saturday as weekend")
if cmd.ignore_region_time:
MakeLog("Ignoring Region Datetime, Using local time")

MakeLog("Day of week: {}, IsWeekday: {}, Current hour: {}, Current DayOfMonth: {}".format(Day, isWeekDay(DayOfWeek), CurrentHour, CurrentDayOfMonth))
MakeLog("Day of week: {}, Nth day in Month: {}, IsWeekday: {}, Current hour: {}, Current DayOfMonth: {}".format(Day, DayNr, isWeekDay(DayOfWeek), CurrentHour, CurrentDayOfMonth))

# Investigatin BUG: temporary disabling below logic
# Array start with 0 so decrease CurrentHour with 1, if hour = 0 then 23
Expand Down Expand Up @@ -518,6 +528,7 @@ def autoscale_region(region):
# - Anyday
# - WeekDay or Weekend
# - Name of Day (Monday, Tuesday....)
# - Name of Day, ending with a number to indicate Nth of the month (Saturday1, Saturday2)
# - Day of month

if AnyDay in schedule:
Expand All @@ -532,6 +543,9 @@ def autoscale_region(region):
if Day in schedule: # Check for day specific tag (today)
ActiveSchedule = schedule[Day]

if "{}{}".format(Day, DayNr) in schedule: # Check for Nth day of the Month
ActiveSchedule = schedule["{}{}".format(Day, DayNr)]

if DayOfMonth in schedule:
specificDays = schedule[DayOfMonth].split(",")
for specificDay in specificDays:
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Welcome to the Scheduled Auto Scaling Script for OCI (Oracle Cloud Infrastructur
The **AutoScaleALL** script: A single Auto Scaling script for all OCI resources that support scaling up/down and power on/off operations.

# NEW
- Support for Nth day of the Month Scheuld (Like 1st saturday or 3rd Saturday)
- Support for a Day of the Month Schedule (Like 1st of the month or 15th of the month)
- Support running on all regions
- Added flags as parameters for execution:
Expand Down Expand Up @@ -103,9 +104,16 @@ The below example tag schedules the resource on the 1st of the month to 4, on th

Schedule.DayOfMonth : 1:4,3:2,28:4

![Scaling Example Instance Pool](http://oc-blog.com/wp-content/uploads/2019/06/ScaleExamplePool.png)
### Values for Nth day of the Month
If you want to have a schedule for the Nth day of the month, like 1st Saturday or 3rd Saturday, you need to add extra Tag Key definitions.

![Power Off Example DB VM](http://oc-blog.com/wp-content/uploads/2019/06/ScaleExampleDB.png)
For example, if you want to have an schedule for the 2nd Saturday of the months, create an extra Tag Key definition called **Saturday2**

A specific Nth day of month schedule overwrites a normal day of the month schedule. So a **Saturday2** overwrites a **Saturday** schedule.

![Scaling Example Instance Pool](http://oc-blog.com/wp-content/uploads/2022/06/Screenshot-2022-06-13-at-11.10.31.png)

![Power Off Example DB VM](https://oc-blog.com/wp-content/uploads/2019/06/ScaleExampleDB.png)

The script supports 3 running methods: All, Up, Down

Expand Down

0 comments on commit d33eed5

Please sign in to comment.