2020-11-26 18:12:12 +03:00
const { parentPort } = require ( 'bthreads' ) ;
2021-06-15 19:01:22 +03:00
const debug = require ( '@tryghost/debug' ) ( 'jobs:email-analytics:fetch-latest' ) ;
2020-11-26 16:09:38 +03:00
// recurring job to fetch analytics since the most recently seen event timestamp
// Exit early when cancelled to prevent stalling shutdown. No cleanup needed when cancelling as everything is idempotent and will pick up
// where it left off on next run
function cancel ( ) {
2021-02-22 09:35:04 +03:00
parentPort . postMessage ( 'Email analytics fetch-latest job cancelled before completion' ) ;
2020-11-26 16:09:38 +03:00
if ( parentPort ) {
parentPort . postMessage ( 'cancelled' ) ;
} else {
setTimeout ( ( ) => {
process . exit ( 0 ) ;
} , 1000 ) ;
}
}
if ( parentPort ) {
parentPort . once ( 'message' , ( message ) => {
if ( message === 'cancel' ) {
return cancel ( ) ;
}
} ) ;
}
( async ( ) => {
2021-02-22 15:10:19 +03:00
const config = require ( '../../../../shared/config' ) ;
const db = require ( '../../../data/db' ) ;
2020-11-26 16:09:38 +03:00
2021-02-22 15:10:19 +03:00
const logging = {
info ( message ) {
parentPort . postMessage ( message ) ;
} ,
warn ( message ) {
parentPort . postMessage ( message ) ;
} ,
error ( message ) {
parentPort . postMessage ( message ) ;
}
} ;
const settingsRows = await db . knex ( 'settings' )
. whereIn ( 'key' , [ 'mailgun_api_key' , 'mailgun_domain' , 'mailgun_base_url' ] ) ;
const settingsCache = { } ;
settingsRows . forEach ( ( row ) => {
settingsCache [ row . key ] = row . value ;
} ) ;
2020-11-26 16:09:38 +03:00
2021-02-22 15:10:19 +03:00
const settings = {
get ( key ) {
return settingsCache [ key ] ;
}
} ;
2021-03-02 11:22:11 +03:00
const { EmailAnalyticsService } = require ( '@tryghost/email-analytics-service' ) ;
const EventProcessor = require ( '../lib/event-processor' ) ;
const MailgunProvider = require ( '@tryghost/email-analytics-provider-mailgun' ) ;
const queries = require ( '../lib/queries' ) ;
2021-02-22 15:10:19 +03:00
const emailAnalyticsService = new EmailAnalyticsService ( {
config ,
settings ,
2021-03-02 11:22:11 +03:00
logging ,
eventProcessor : new EventProcessor ( { db , logging } ) ,
providers : [
new MailgunProvider ( { config , settings , logging } )
] ,
queries
2021-02-22 15:10:19 +03:00
} ) ;
2020-11-26 16:09:38 +03:00
2020-12-07 06:43:57 +03:00
const fetchStartDate = new Date ( ) ;
debug ( 'Starting email analytics fetch of latest events' ) ;
const eventStats = await emailAnalyticsService . fetchLatest ( ) ;
const fetchEndDate = new Date ( ) ;
debug ( ` Finished fetching ${ eventStats . totalEvents } analytics events in ${ fetchEndDate - fetchStartDate } ms ` ) ;
2020-11-26 16:09:38 +03:00
2020-12-07 06:43:57 +03:00
const aggregateStartDate = new Date ( ) ;
debug ( ` Starting email analytics aggregation for ${ eventStats . emailIds . length } emails ` ) ;
await emailAnalyticsService . aggregateStats ( eventStats ) ;
const aggregateEndDate = new Date ( ) ;
debug ( ` Finished aggregating email analytics in ${ aggregateEndDate - aggregateStartDate } ms ` ) ;
2020-11-26 16:09:38 +03:00
2021-02-22 09:35:04 +03:00
parentPort . postMessage ( ` Fetched ${ eventStats . totalEvents } events and aggregated stats for ${ eventStats . emailIds . length } emails in ${ aggregateEndDate - fetchStartDate } ms ` ) ;
2020-11-26 16:09:38 +03:00
2020-12-07 06:43:57 +03:00
if ( parentPort ) {
parentPort . postMessage ( 'done' ) ;
} else {
2020-11-26 16:09:38 +03:00
// give the logging pipes time finish writing before exit
setTimeout ( ( ) => {
2020-12-07 06:43:57 +03:00
process . exit ( 0 ) ;
2020-11-26 16:09:38 +03:00
} , 1000 ) ;
}
} ) ( ) ;