import DependenceService from '../services/dependence' ; import { exec } from 'child_process' ; import { Container } from 'typedi' ; import { Crontab , CrontabModel , CrontabStatus } from '../data/cron' ; import CronService from '../services/cron' ; import EnvService from '../services/env' ; import _ from 'lodash' ; import { DependenceModel } from '../data/dependence' ; import { Op } from 'sequelize' ; export default async ( ) => { const cronService = Container . get ( CronService ) ; const envService = Container . get ( EnvService ) ; const dependenceService = Container . get ( DependenceService ) ; await CrontabModel . update ( { status : CrontabStatus . idle } , { where : { status : [ CrontabStatus . running , CrontabStatus . queued ] } } ) ; DependenceModel . findAll ( { where : { } } ) . then ( async ( docs ) => { const groups = _ . groupBy ( docs , 'type' ) ; for ( const key in groups ) { if ( Object . prototype . hasOwnProperty . call ( groups , key ) ) { const group = groups [ key ] ; const depIds = group . map ( ( x ) => x . id ) ; for ( const dep of depIds ) { await dependenceService . reInstall ( [ dep ] ) ; } } } } ) ; CrontabModel . findAll ( { where : { isDisabled : { [ Op . ne ] : 1 } , command : { [ Op . or ] : [ { [ Op . like ] : `%ql repo%` } , { [ Op . like ] : `%$ql raw%` } ] , } , } , } ) . then ( ( docs ) => { for ( let i = 0 ; i < docs . length ; i ++ ) { const doc = docs [ i ] ; if ( doc ) { exec ( doc . command ) ; } } } ) ; await cronService . autosave_crontab ( ) ; await envService . set_envs ( ) ; } ; function randomSchedule ( from , to ) { const result = [ ] ; const arr = [ ... Array ( from ) . keys ( ) ] ; let count = arr . length ; for ( let i = 0 ; i < to ; i ++ ) { const index = ~ ~ ( Math . random ( ) * count ) + i ; if ( result . includes ( arr [ index ] ) ) { continue ; } result [ i ] = arr [ index ] ; arr [ index ] = arr [ i ] ; count -- ; } return result ; }