[go: up one dir, main page]

Skip to content

Commit

Permalink
feat(core): create intial document releasing cron (#463)
Browse files Browse the repository at this point in the history
Create the intial document releasing cron relying on setTimeout for
scheduling.

Future updates should move to a worker thread or child process strategy
and create/use a robot account rather than the first created account.

Co-authored-by: Birkir Gudjonsson <birkir.gudjonsson@gmail.com>
  • Loading branch information
its-lucas and birkir committed Jul 17, 2020
1 parent 2e94286 commit dddfe34
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { InjectRepository } from 'typeorm-typedi-extensions';
import { Document } from '../../../entities/Document';
import { Release } from '../../../entities/Release';
import { User } from '../../../entities/User';
import { Context } from '../../../interfaces/Context';
import { processWebhooks } from '../../../utils/processWebhooks';
import { DocumentRepository } from '../repositories/DocumentRepository';
import { ReleaseRepository } from '../repositories/ReleaseRepository';
Expand All @@ -24,7 +25,6 @@ import { ReleaseInput } from '../types/ReleaseInput';
import { Authorized } from '../utils/Authorized';
import { ExtendedConnection } from '../utils/ExtendedConnection';
import { DocumentResolver } from './DocumentResolver';
import { Context } from '../../../interfaces/Context';

const ReleaseConnection = createConnectionType(Release);

Expand Down
2 changes: 2 additions & 0 deletions packages/prime-core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { fields } from './utils/fields';
import { log } from './utils/log';
import { previewRoutes } from './utils/preview';
import { pubSub } from './utils/pubSub';
import { ReleaseCron } from './utils/releaseCron';
import { serveUI } from './utils/serveUI';

useContainer(Container);
Expand Down Expand Up @@ -111,6 +112,7 @@ export const createServer = async ({ port, connection }: ServerConfig) => {

previewRoutes(app);
serveUI(app);
ReleaseCron.run();

return server.listen(port, () => {
log(`🚀 Server ready at http://localhost:${port}${apollo.graphqlPath}`);
Expand Down
92 changes: 92 additions & 0 deletions packages/prime-core/src/utils/releaseCron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { User } from '@accounts/typeorm';
import debug from 'debug';
import Container, { Service } from 'typedi';
import { LessThan } from 'typeorm';
import { InjectRepository } from 'typeorm-typedi-extensions';
import { DocumentRepository } from '../modules/internal/repositories/DocumentRepository';
import { ReleaseRepository } from '../modules/internal/repositories/ReleaseRepository';
import { UserRepository } from '../modules/internal/repositories/UserRepository';

const log = debug('prime:release:cron');

const ONE_MINUTE = 1000 * 60;

@Service()
export class ReleaseCron {
/**
* Run the cron every minute to check for releases
*/
public static run() {
if (!this.self) {
this.self = Container.get(ReleaseCron);
}

this.self.tick();

setTimeout(() => this.run(), ONE_MINUTE);
}

private static self: ReleaseCron;
private user?: User;

@InjectRepository()
private readonly releaseRepository: ReleaseRepository;

@InjectRepository()
private readonly userRepository: UserRepository;

@InjectRepository()
private readonly documentRepository: DocumentRepository;

/**
* Gets the user that will be publishing the releases
*/
private async getUser(): Promise<User | undefined> {
// Get the first user for now
this.user = await this.userRepository.findOne({
order: { createdAt: 'ASC' },
});

return this.user;
}

private async tick() {
log('checking for pending releases');

// Get all the releases that are scheduled for the past and haven't yet been published
const releases = await this.releaseRepository.find({
relations: ['documents'],
where: {
scheduledAt: LessThan(new Date()),
publishedAt: null,
},
});

// Check that their is a valid user to use
if (!this.user) {
await this.getUser();
}

// If we have a user
if (this.user) {
// For every release
for (const release of releases) {
log(`releasing, ${release.name}`);

// Publish the related documents
const docs = release.documents.map(x => this.documentRepository.publish(x, this.user!.id));
await Promise.all(docs);

// Then update any documents with the release id to no longer contain the release id
await this.documentRepository.update({ releaseId: release.id }, { releaseId: null as any });

// Update the release to have a publishedAt date and publishedBy user
release.publishedAt = new Date();
release.publishedBy = this.user.id;

// And then save the updated release
await this.releaseRepository.save(release);
}
}
}
}

0 comments on commit dddfe34

Please sign in to comment.