feat: initial user list (no edit/delete yet) and job schedules

This commit is contained in:
sct
2020-11-05 10:45:51 +00:00
parent 320432657e
commit 24a0423f3b
10 changed files with 213 additions and 7 deletions

View File

@@ -39,7 +39,7 @@ export class User {
public avatar: string;
@OneToMany(() => MediaRequest, (request) => request.requestedBy)
public requests: MediaRequest;
public requests: MediaRequest[];
@CreateDateColumn()
public createdAt: Date;

View File

@@ -13,6 +13,7 @@ import { OpenApiValidator } from 'express-openapi-validator';
import { Session } from './entity/Session';
import { getSettings } from './lib/settings';
import logger from './logger';
import { startJobs } from './job/schedule';
const API_SPEC_PATH = path.join(__dirname, '../overseerr-api.yml');
@@ -27,6 +28,9 @@ app
// Load Settings
getSettings().load();
// Start Jobs
startJobs();
const server = express();
server.use(cookieParser());
server.use(bodyParser.json());

View File

@@ -67,7 +67,8 @@ class JobPlexSync {
existing.status = MediaStatus.AVAILABLE;
mediaRepository.save(existing);
this.log(
`Request for ${metadata.title} exists. Setting status AVAILABLE`
`Request for ${metadata.title} exists. Setting status AVAILABLE`,
'info'
);
} else {
newMedia.status = MediaStatus.AVAILABLE;
@@ -90,7 +91,8 @@ class JobPlexSync {
existing.status = MediaStatus.AVAILABLE;
await mediaRepository.save(existing);
this.log(
`Request for ${plexitem.title} exists. Setting status AVAILABLE`
`Request for ${plexitem.title} exists. Setting status AVAILABLE`,
'info'
);
} else if (tmdbMovie) {
const newMedia = new Media();
@@ -139,8 +141,11 @@ class JobPlexSync {
}
}
private log(message: string): void {
logger.info(message, { label: 'Plex Sync' });
private log(
message: string,
level: 'info' | 'error' | 'debug' = 'debug'
): void {
logger[level](message, { label: 'Plex Sync' });
}
public async run(): Promise<void> {
@@ -160,7 +165,7 @@ class JobPlexSync {
for (const library of this.libraries) {
this.currentLibrary = library;
this.log(`Beginning to process library: ${library.name}`);
this.log(`Beginning to process library: ${library.name}`, 'info');
this.items = await this.plexClient.getLibraryContents(library.id);
await this.loop();
}

15
server/job/schedule.ts Normal file
View File

@@ -0,0 +1,15 @@
import schedule from 'node-schedule';
import jobPlexSync from './plexsync';
import logger from '../logger';
export const scheduledJobs: Record<string, schedule.Job> = {};
export const startJobs = (): void => {
// Run full plex sync every 6 hours
scheduledJobs.plexFullSync = schedule.scheduleJob('* */6 * * *', () => {
logger.info('Starting scheduled job: Plex Full Sync', { label: 'Jobs' });
jobPlexSync.run();
});
logger.info('Scheduled jobs loaded', { label: 'Jobs' });
};

View File

@@ -7,7 +7,7 @@ const router = Router();
router.get('/', async (req, res) => {
const userRepository = getRepository(User);
const users = await userRepository.find();
const users = await userRepository.find({ relations: ['requests'] });
return res.status(200).json(User.filterMany(users));
});