Skip to content

Commit

Permalink
Rewrite cron in ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
johnf committed Nov 4, 2023
1 parent 4737107 commit 996015e
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 3,988 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ RUN apt-get update -qq && \
RUN apt-get install --no-install-recommends -y \
libcurl4-openssl-dev \
libmagic-dev \
libmagickwand-dev \
nodejs # For cron FIXME: Rewrite this in ruby
libmagickwand-dev

# Install application gems
COPY Gemfile Gemfile.lock ./
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ gem "streamio-ffmpeg" # ffmpeg interface
gem "rmagick" # Image processing
gem "whenever", :require => false # scheduling
gem "rubyzip" # Zip the large CSV files before emailing
gem 'curb' # Download CSVs for import
gem "curb" # Download CSVs for import
gem "rufus-scheduler" # Cron

group :development, :test do
gem "rspec-rails"
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ GEM
ruby2_keywords
e2mmap (0.1.0)
erubi (1.12.0)
et-orbi (1.2.7)
tzinfo
factory_bot (6.2.1)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
Expand All @@ -204,6 +206,9 @@ GEM
formtastic (5.0.0)
actionpack (>= 6.0.0)
formtastic_i18n (0.7.0)
fugit (1.9.0)
et-orbi (~> 1, >= 1.2.7)
raabro (~> 1.4)
globalid (1.2.1)
activesupport (>= 6.1)
graphiql-rails (1.8.0)
Expand Down Expand Up @@ -353,6 +358,7 @@ GEM
public_suffix (5.0.3)
puma (6.4.0)
nio4r (~> 2.0)
raabro (1.4.0)
racc (1.7.1)
rack (2.2.8)
rack-session (1.0.1)
Expand Down Expand Up @@ -484,6 +490,8 @@ GEM
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
rufus-scheduler (3.9.1)
fugit (~> 1.1, >= 1.1.6)
sassc (2.4.0)
ffi (~> 1.9)
sassc-rails (2.1.2)
Expand Down Expand Up @@ -623,6 +631,7 @@ DEPENDENCIES
rubocop-rspec
ruby-filemagic
rubyzip
rufus-scheduler
sassc-rails
selenium-webdriver
sentry-delayed_job
Expand Down
3 changes: 0 additions & 3 deletions cron-worker/.gitignore

This file was deleted.

70 changes: 70 additions & 0 deletions cron-worker/cron.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env ruby

require 'rufus-scheduler'
require 'aws-sdk-ses'
require 'stringio'

require File.expand_path('../config/environment', __dir__) # adjust the path as necessary

Rake::Task.clear # necessary to avoid duplicate tasks
Nabu::Application.load_tasks

ses = Aws::SES::Client.new(region: 'ap-southeast-2')

scheduler = Rufus::Scheduler.new

scheduler.cron '10 1 * * *' do
name = 'Mint Dois'
task = 'archive:mint_dois'

puts "#{Time.current}: Starting task #{name}"

output = StringIO.new
$stdout = output
$stderr = output

begin
Rake::Task[task].invoke
ensure
Rake::Task[task].reenable
$stdout = STDOUT # Reset stdout to its original value
$stderr = STDERR # Reset stderr to its original value
end

email_output = output.string
puts email_output

if email_output.size < 5
puts 'No output from task, not sending email.'

next
end

params = {
source: 'admin@paradisec.org.au',
destination: {
to_addresses: ['admin@paradisec.org.au', 'johnf@inodes.org', 'jferlito@gmail.com']
},
message: {
subject: {
data: "Paradisec Scheduled Job - #{name}"
},
body: {
text: {
data: "The job had the following output:\n\n#{email_output}"
}
}
}
}

begin
ses.send_email(params)
puts "Email sent with task output. #{email_output}"
rescue Aws::SES::Errors::ServiceError => e
puts "Email failed to send: #{e}"
end

puts "#{Time.current}: Finished task #{name}"
end

scheduler.join
60 changes: 0 additions & 60 deletions cron-worker/index.ts

This file was deleted.

Loading

0 comments on commit 996015e

Please sign in to comment.