Skip to content

Commit

Permalink
implementing cronjob for deleting room and sending information email …
Browse files Browse the repository at this point in the history
…to room owner
  • Loading branch information
AlexanderUngefug committed Jun 6, 2024
1 parent b3cff86 commit 51325f8
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 11,571 deletions.
16 changes: 13 additions & 3 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ RUN apk add --no-cache \
ruby-json \
tzdata \
bash \
shared-mime-info
shared-mime-info \
cronie

# Builder stage for installing build dependencies
FROM base AS builder
Expand Down Expand Up @@ -58,11 +59,18 @@ RUN apk add --no-cache \
nodejs \
yarn \
yaml-dev \
zlib-dev
zlib-dev \
rsyslog

COPY --from=builder $RAILS_ROOT $RAILS_ROOT
RUN chmod +x ./bin/start

# Add the script to be run every minute
RUN mkdir -p /etc/periodic/1min
COPY ./cronjobs/delete_expired_rooms.sh /etc/periodic/1min/delete_expired_rooms.sh
RUN chmod +x /etc/periodic/1min/delete_expired_rooms.sh


# Set environment variables
ARG RAILS_ENV
ENV RAILS_ENV=${RAILS_ENV:-development}
Expand All @@ -72,4 +80,6 @@ ARG VERSION_CODE
ENV VERSION_CODE=$VERSION_CODE

EXPOSE 3000
CMD ["bash", "-c", "./bin/start"]

# Start rsyslog and cron daemon, then start the Rails application
CMD ["sh", "-c", "./bin/start"]
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,9 @@ GEM
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
sprockets-rails (3.4.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets-rails (3.5.0)
actionpack (>= 6.1)
activesupport (>= 6.1)
sprockets (>= 3.0.0)
stringio (3.1.0)
strscan (3.1.0)
Expand Down
12 changes: 11 additions & 1 deletion app/models/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def get_setting(name:)
.find_by(meeting_option: { name: })
end

# Autocreate all meeting options using the default values
# Autocreate all meeting options using the default
def create_meeting_options
configs = RoomsConfiguration.joins(:meeting_option).where(provider: user.provider).pluck(:name, :value).to_h

Expand All @@ -80,6 +80,16 @@ def public_recordings
recordings.where(visibility: [Recording::VISIBILITIES[:public], Recording::VISIBILITIES[:public_protected]])
end

def notify_room_deletion
begin
user_email = user.email
room_name = name
UserMailer.with(to: user_email, subject: "Your Room #{room_name} has been deleted").test_email.deliver_now
rescue => e
puts "Failed to send deletion email: #{e.message}"
end
end

private

def set_friendly_id
Expand Down
10 changes: 10 additions & 0 deletions bin/start
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ fi
./bin/rails db:create
./bin/rails db:migrate:with_data

# Write environment variables to crontab
env | grep -v 'no_proxy' | while read -r line; do
echo "$line"
done >/etc/crontabs/root

echo "* * * * * /usr/src/app/cronjobs/delete_expired_rooms.sh >> /var/log/cron.log 2>&1" >>/etc/crontabs/root

# Start cron daemon as a background process
crond

exec ./bin/rails s -b 0.0.0.0 -p $PORT
9 changes: 9 additions & 0 deletions cronjobs/delete_expired_rooms.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
echo "This is a script running every minute" >>/var/log/cron.log 2>&1
cd /usr/src/app || {
echo "Failed to change directory to /usr/src/app" >>/var/log/cron.log 2>&1
exit 1
}

./bin/rake rooms:delete_expired >>/var/log/cron.log 2>&1
echo "Finished rake task" >>/var/log/cron.log 2>&1
50 changes: 31 additions & 19 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,47 @@
version: '3'

volumes:
postgres-data:
redis-data:


services:
postgres:
image: postgres:14.6-alpine3.17
container_name: postgres
db:
image: postgres:14.8-alpine
container_name: db
restart: unless-stopped
ports:
- 5432:5432
volumes:
- ./data/postgres/14/database_data:/var/lib/postgresql/data
- postgres-data:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
PGDATA: /var/lib/postgresql/data
POSTGRES_HOST: db

redis:
image: redis:6.2-alpine3.17
image: redis:7.2.4-alpine
container_name: redis
restart: unless-stopped
volumes:
- ./data/redis/database_data:/data
- redis-data:/data

greenlight-v3:
entrypoint: [bin/start]
image: bigbluebutton/greenlight:v3
container_name: greenlight-v3
greenlight:
image: my-greenlight-dev
container_name: greenlight
restart: unless-stopped
env_file: .env
ports:
- 127.0.0.1:5050:3000
logging:
driver: journald
volumes:
- ./data/greenlight-v3/storage:/usr/src/app/storage
- "3000:3000"
- "587:587"
env_file:
- .env
depends_on:
- postgres
- db
- redis
logging:
driver: "json-file"
volumes:
- ./app/javascript:/usr/src/app/app/javascript
- ./log/cron.log:/var/log/cron.log

23 changes: 23 additions & 0 deletions lib/tasks/delete_expired_rooms.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace :rooms do
desc "Delete rooms where deletion_date has passed"
task delete_expired: :environment do
expired_rooms = Room.where('deletion_date < ?', Time.current)

if expired_rooms.any?
size = expired_rooms.size
expired_rooms.each do |room|
begin
room.notify_room_deletion
room.destroy
puts "Deleted room #{room.id}"
rescue => e
puts "Failed to delete room #{room.id}: #{e.message}"
end
end
puts "Deleted #{size} expired rooms."
else
puts "No expired rooms to delete."
end
end
end

Loading

0 comments on commit 51325f8

Please sign in to comment.