-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New pwpush-worker Docker container (#2601)
* Add SolidQueue and background jobs * Add worker to Procfiles * Docker container build * Worker container: updated entrypoint
- Loading branch information
1 parent
b7376ed
commit a886e69
Showing
16 changed files
with
564 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
release: bundle exec rails db:migrate | ||
web: bundle exec puma -C config/puma.rb | ||
console: bundle exec rails console | ||
worker: bundle exec rake solid_queue:start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
web: bin/rails server -p 5100 | ||
js: yarn watch | ||
worker: bundle exec rake solid_queue:start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
class CleanUpPushesJob < ApplicationJob | ||
queue_as :default | ||
|
||
# Delete Anonymous Expired Pushes | ||
# | ||
# When a push expires, the payload is deleted but the metadata record still exists. This | ||
# includes information such as creation date, audit logs, duration etc.. When the record | ||
# was created by an anonymous user, this data is no longer needed and we delete it (we | ||
# don't want it). | ||
# | ||
# If a user attempts to retrieve a secret link that doesn't exist anymore, we still show | ||
# the standard "This secret link has expired" message. This strategy provides two benefits: | ||
# | ||
# 1. It hides the fact that if a secret ever exists or not (more secure) | ||
# 2. It allows us to delete data that we don't want | ||
# | ||
# This task will run through all expired and anonymous records and delete them entirely. | ||
# | ||
# Because of the above, expired and anonymous secret URLs still will show the same | ||
# expiration message | ||
# | ||
# Note: This applies to anonymous pushes. For logged-in user records, we don't do this | ||
# to maintain user audit logs. | ||
# | ||
def perform(*args) | ||
# Log task start | ||
logger.info("--> #{self.class.name}: Starting...") | ||
|
||
counter = 0 | ||
|
||
Password.includes(:views) | ||
.where(expired: true) | ||
.where(user_id: nil) | ||
.find_each do |push| | ||
counter += 1 | ||
push.destroy | ||
end | ||
|
||
if Settings.enable_file_pushes | ||
FilePush.includes(:views) | ||
.where(expired: true) | ||
.where(user_id: nil) | ||
.find_each do |push| | ||
counter += 1 | ||
push.destroy | ||
end | ||
end | ||
|
||
if Settings.enable_url_pushes | ||
Url.includes(:views) | ||
.where(expired: true) | ||
.where(user_id: nil) | ||
.find_each do |push| | ||
counter += 1 | ||
push.destroy | ||
end | ||
end | ||
|
||
logger.info(" -> #{counter} total anonymous expired pushes deleted.") | ||
|
||
# Log completion | ||
logger.info(" -> #{self.class.name}: Finished.") | ||
end | ||
|
||
private | ||
|
||
def logger | ||
@logger ||= if ENV.key?(PWP_WORKER) | ||
# We are running inside the pwpush-worker container. Log to STDOUT | ||
Logger.new($stdout) | ||
else | ||
Logger.new(Rails.root.join("log", "recurring.log")) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
class ExpirePushesJob < ApplicationJob | ||
queue_as :default | ||
|
||
## | ||
# This job is responsible for scanning all unexpired password pushes and | ||
# conditionally expiring them. This is a preemptive measure to expire pushes | ||
# periodically. It saves some CPU and DB calls on live requests. | ||
# | ||
def perform(*args) | ||
# Log task start | ||
logger.info("--> #{self.class.name}: Starting...") | ||
|
||
counter = 0 | ||
expiration_count = 0 | ||
|
||
Password.where(expired: false).find_each do |push| | ||
counter += 1 | ||
push.validate! | ||
expiration_count += 1 if push.expired | ||
end | ||
|
||
logger.info(" -> Finished validating #{counter} unexpired password pushes. #{expiration_count} total pushes expired...") | ||
|
||
if Settings.enable_file_pushes | ||
counter = 0 | ||
expiration_count = 0 | ||
FilePush.where(expired: false).find_each do |push| | ||
counter += 1 | ||
push.validate! | ||
expiration_count += 1 if push.expired | ||
end | ||
logger.info(" -> Finished validating #{counter} unexpired File pushes. #{expiration_count} total pushes expired...") | ||
end | ||
|
||
if Settings.enable_url_pushes | ||
counter = 0 | ||
expiration_count = 0 | ||
Url.where(expired: false).find_each do |push| | ||
counter += 1 | ||
push.validate! | ||
expiration_count += 1 if push.expired | ||
end | ||
logger.info(" -> Finished validating #{counter} unexpired URL pushes. #{expiration_count} total pushes expired...") | ||
end | ||
|
||
# Log results | ||
logger.info(" -> #{self.class.name}: #{counter} anonymous and expired pushes have been deleted.") | ||
|
||
# Log completion | ||
logger.info(" -> #{self.class.name}: Finished.") | ||
end | ||
|
||
private | ||
|
||
def logger | ||
@logger ||= if ENV.key?(PWP_WORKER) | ||
# We are running inside the pwpush-worker container. Log to STDOUT | ||
# so that docker logs works to investigate problems. | ||
Logger.new($stdout) | ||
else | ||
Logger.new(Rails.root.join("log", "recurring.log")) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require_relative "../config/environment" | ||
require "solid_queue/cli" | ||
|
||
SolidQueue::Cli.start(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
default: &default | ||
dispatchers: | ||
- polling_interval: 1 | ||
batch_size: 500 | ||
workers: | ||
- queues: "*" | ||
threads: 3 | ||
processes: <%= ENV.fetch("JOB_CONCURRENCY", 1) %> | ||
polling_interval: 0.1 | ||
|
||
development: | ||
<<: *default | ||
|
||
test: | ||
<<: *default | ||
|
||
production: | ||
<<: *default |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# production: | ||
# periodic_cleanup: | ||
# class: CleanSoftDeletedRecordsJob | ||
# queue: background | ||
# args: [ 1000, { batch_size: 500 } ] | ||
# schedule: every hour | ||
# periodic_command: | ||
# command: "SoftDeletedRecord.due.delete_all" | ||
# priority: 2 | ||
# schedule: at 5am every day | ||
|
||
production: | ||
expire_pushes: | ||
class: ExpirePushesJob | ||
queue: background | ||
schedule: every 2 hours | ||
|
||
# expire_pulls: | ||
# class: ExpirePullsJob | ||
# queue: background | ||
# schedule: every 3 hours | ||
|
||
cleanup_pushes: | ||
class: CleanUpPushesJob | ||
queue: background | ||
schedule: every day at 5am | ||
|
||
development: | ||
expire_pushes: | ||
class: ExpirePushesJob | ||
queue: background | ||
schedule: every 4 hours | ||
|
||
# expire_pulls: | ||
# class: ExpirePullsJob | ||
# queue: background | ||
# schedule: every 5 hours | ||
|
||
cleanup_pushes: | ||
class: CleanUpPushesJob | ||
queue: background | ||
schedule: every 7 hours |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ | |
|
||
root to: "users#index" | ||
end | ||
# mount MissionControl::Jobs::Engine, at: "/admin/jobs" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM pglombardo/pwpush:latest | ||
|
||
ENV PWP_WORKER=true | ||
|
||
USER pwpusher | ||
ENTRYPOINT ["containers/docker/worker-entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
export RAILS_ENV=production | ||
|
||
echo "" | ||
if [ -z "$DATABASE_URL" ] | ||
then | ||
echo "DATABASE_URL not specified. This worker container only works with a PostgreSQL, MySQL or MariaDB database." | ||
echo "Please specify DATABASE_URL and use the same settings & environment variables as you do with other pwpush containers." | ||
echo "" | ||
echo "See https://docs.pwpush.com/docs/database_url/ for more information on DATABASE_URL." | ||
exit 1 | ||
elif [[ "$DATABASE_URL" == sqlite3://* ]]; then | ||
echo "Error: sqlite3 isn't supported for the pwpush-worker container." | ||
exit 1 | ||
else | ||
echo "According to DATABASE_URL database backend is set to $(echo $DATABASE_URL|cut -d ":" -f 1):..." | ||
fi | ||
echo "" | ||
|
||
echo "Password Pusher: migrating database to latest..." | ||
bundle exec rake db:migrate | ||
|
||
echo "Password Pusher: starting background workers..." | ||
bundle exec solid_queue:start | ||
|
||
exec "$@" |
Oops, something went wrong.