web47core
Core components used commonly among all web apps for both App47 and RedMonocle
We don't need no sticking badges
Requirements
- Ruby 2.7.0
Working with Bundler and RBENV
This project manages RBENV and manages dependencies via Bundler.
You must first install RBENV. Then install Ruby 2.7.0 via RBENV ``` shell script rbenv install 2.7.0
You'll now notice that this project (as well as other App47 ones) contains a .ruby-version file, which is executed upon opening the project's root directory in a terminal (and IDE's like RubyMine). The .ruby-version file simply states `2.7.0` which tells RBENV to ensure the Ruby version to use for this project is 2.7.0.
Please note, your Ruby `2.7.0` version might need bundler installed:
``` shell script
gem install bundler -v 2.1.4
To set up this project's dependencies, which are defined in the file, Gemfile
, you should first run
``` shell script
bundle install
## Development
Your `RubyMine` environment should be setup now, however to verify all is well, please run the test suite
``` shell script
bundle exec rake test
Deployment
The web47core
project is a gem that will be deployed via Ruby Gems. When an update is ready, the following steps should be followed
- Build the gem
gem build web47core.gemspec
- Post the gem to the DevOps channel in slack, ask for it to be added to the S3 repo.
Usage
Importing the gem
To use the web47core
gem in a project, first add the gem to your Gemfile in one of two ways
Using the gem from Ruby Gems
gem 'web47core'
If you need the gem immediately or need to pull from development branch, you can use the git repo
gem 'web47core', git: '[email protected]:App47/web47core.git', branch: :master
or from the develop branch
gem 'web47core', git: '[email protected]:App47/web47core.git', branch: :develop
Please do not ship to production code using the git repo, as the production servers will not have keys to pull from the web47core repo.
Remove the following files
-
StandardModel
- Includes the common set of includes, Mongoid, Auditable, AutoClearCache, etc. -
CdnUrl
- Provide CDN URLs of your classes URLs when cdn_url is configured in system configuration -
AutoClearCache
- Clears your objects cache out of Rails.cache using the object's id -
Formable
- Consolidated into StandardModel, was used to provide common form operations for mongoid documents -
EmailNotification
-
EmailTemplate
-
Notification
-
NotificationTemplate
-
SlackNotification
SmsNotification
Template
App47Logger
andApp47LoggerTest
Emailable
orEmailAble
andEamailbleTest
, be sure to changeEmailable
toEmailAble
RedisConfiguration
andRedisConfgurationTest
Searchable
andSearchableTest
, be sure to changeSearchable
toSearchAble
TimezoneAble
andTimezoneAbleTest
CronJobServer
andCronJobServerTest
JobCronTab
andJobCronTabTest
CronTab
SecureFields
Job
JobLog
CommandLogLog
TrimJobs
#### Models ##### Concerns-
StandardModel
- Includes the common set of includes, Mongoid, etc. -
CoreSystemConfiguration
- Base system configuration to be included in the app's SystemConfiguration Object -
CoreAccount
- Base account object that is related to notifications and the ilk. ##### System Configuration Define aSystemConfiguration
class in your project and import the core concern.
class SystemConfiguration
include StandardModel
include CoreSystemConfiguration
# Include your additional system configuration fields and methods
field :google_sso_client_id, type: String
end
Configuration has been broken out, or modularized as of 2.0.10, so if the project uses AWS
, SMTP
, Slack
, Switchboard
, Twilio
or Zendesk
within SystemConfiguration
, then you will also need to include those configurations in the Project's SystemConfiguration, for example
class SystemConfiguration
include StandardModel
include CoreSystemConfiguration
include ZendeskConfiguration
include TwilioConfiguration
include SwitchboardConfiguration
include DelayedJobConfiguration
# Include your additional system configuration fields and methods
field :google_sso_client_id, type: String
end
Account
Define a Account
class in your project and import the core concern.
class Account
include StandardModel
include CoreAccount
# Include your additional system configuration fields and methods
has_many :users
end
Cron
Cron infrastructure for running jobs every minute, hour, day, week, month or some combination
- Remove
config/initializers/job_cron_tabs.rb
that was previous used to ensure cron tabs, they will be loaded automatically now - Remove the
Delayed::Backend::Mongoid::Job
class definition, not the settings fromconfig/initializers/delayed_job.rb
, these are included in this gem now. - Cron jobs should extend from
Cron::Job
now and specifycron_tab_entry
within the class.ruby module Cron class MyJob < Job cron_tab_entry :daily def perform # do your work end end end
Allowed values of cron_tab_entry are -
:always
Run every minute -
:hourly
Run every hour -
:daily
Run every day -
:weekly
Run every week on Sunday -
:monthly
Run every month on the first day of the month -
'*/5 1,3 * 2 *'
Run according to unix crontab entry format. This would run every tuesday on the 1st and 3rd hour, for any minute divisible by 5, so 0, 5, 10, 15, etc..
Before starting the server, you need to run the database command
db.cron_tabs.updateMany({_type: 'JobCronTab'}, {$set: {_type: 'Cron::JobTab'}});
Abilities
Update abilities to new class names CronTab, JobCronTab, CronJobServer to Cron::Tab, Cron::JobTab, Cron::Server
Routes
The following routes should be added to the correct namespace for your applicaiton
#
# Cron job servers and Cron Tabs
# index - show both cron tabs and servers
# edit - edit a cron tab
# update - update a cron tab
# destroy - destroy a server
# demote - demote a server
# run_now - run a cron tab
#
resources :cron, only: %i[edit update destroy index] do
member do
get :run_now
get :demote
end
end
#
# System configuration
#
resource :system_configurations, only: %i[edit update show] do
get :sync
end
#
# Delayed jobs
#
resources :delayed_jobs, only: %i[index show destroy] do
collection do
get :resubmit_all
get :destroy_all
end
member do
get :resubmit
end
end
Controllers
CronController
Update the CronController with something like below, and remove the views and any localization you might have made.
# frozen_string_literal: true
#
# Manage all crontabs in the system Configuration
#
class CronController < AdminController
:cron_tab, only: %w[run_now update edit], id_param: :id, class: 'Cron::Tab'
:cron_server, only: %w[demote destroy], id_param: :id, class: 'Cron::Server'
include ::CoreCronController
rescue_from Mongoid::Errors::DocumentNotFound, with: :document_not_found_error
end
To start or stop the cron server, run the bundler command:
bundle exec cron_server start
or
bundle exec cron_server stop
DelayedJobController
Update the DelayedJobController with something like below, and remove the views and any localization you might have made.
# frozen_string_literal: true
#
# Manage access to delayed jobs for the admin UI
#
class DelayedJobsController < AdminController
include ::CoreDelayedJobsController
class: 'Delayed::Backend::Mongoid::Job'
rescue_from Mongoid::Errors::DocumentNotFound, with: :document_not_found_error
end
To start or stop the delayed jobs, run the bundler command:
bundle exec delayed_job start
or
bundle exec delayed_job stop
Delayed Job Management
Since 2.1.0, there is now a built in Delayed Job monitor that will monitor delayed jobs and auto restart them if they
exceed the max allowed time for a job to run. To configure this, update the delayed job initializer to add the TimeKeeper
plugin.
Delayed::Worker.plugins += [Delayed::Plugins::TimeKeeper]
This will add a few now cron jobs as well as objects to the project, you can view these by adding the following routes near the delayed jobs area
resources :delayed_job_workers, only: %i[index destroy]
resources :delayed_job_metrics, only: %i[index destroy]
with the following two controllers
# frozen_string_literal: true
#
# Manage access to metrics
#
class DelayedJobMetricsController < BaseStackUiController
rescue_from Mongoid::Errors::DocumentNotFound, with: :document_not_found_error
include ::CoreDelayedJobMetricsController
end
and
# frozen_string_literal: true
#
# Manage access to workers
#
class DelayedJobWorkersController < BaseStackUiController
rescue_from Mongoid::Errors::DocumentNotFound, with: :document_not_found_error
include ::CoreDelayedJobWorkersController
end
lastly, add the following three objects to ability to allow access
Delayed::Jobs::Worker,
Delayed::Jobs::Metric,
Delayed::Jobs::Run
StatusController
Remove controller, views and localizations
SystemConfigurationsController
Update the SystemConfigurationsController with something like below, and remove the views and any localization you might have made.
# frozen_string_literal: true
#
# Manage the system configuration page
#
class SystemConfigurationsController < AdminController
include ::CoreSystemConfigurationsController
:system_configuration
end
~/bin Directory
Remove the delayed_job, delayed_job.sh and cron_server.sh files
Notification Templates
Move any notification templates from ~/app/assets/templates
to ~/lib/templates