Skip to content

Commit

Permalink
Fix demo migration (#321)
Browse files Browse the repository at this point in the history
* fix demo migration

* add tests
  • Loading branch information
antoinedc authored Jan 10, 2024
1 parent c14bc36 commit feaa465
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 7 deletions.
6 changes: 5 additions & 1 deletion run/jobs/updateExplorerSyncingProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ module.exports = async job => {
const pm2 = new PM2(process.env.PM2_HOST, process.env.PM2_SECRET);
const { data: existingProcess } = await pm2.find(data.explorerSlug);

if (!explorer && existingProcess) {
if (data.reset) {
await pm2.reset(explorer.slug, explorer.workspaceId);
return 'Process reset.';
}
else if (!explorer && existingProcess) {
await pm2.delete(data.explorerSlug);
return 'Process deleted: no explorer.';
}
Expand Down
7 changes: 7 additions & 0 deletions run/lib/pm2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class PM2 {
return withTimeout(axios.post(resource, { slug, workspaceId }));
}

async reset(slug, workspaceId) {
if (!slug || !workspaceId) throw new Error('Missing parameter');

await this.delete(slug);
return this.start(slug, workspaceId);
}

resume(slug) {
if (!slug) throw new Error('Missing parameter');

Expand Down
7 changes: 6 additions & 1 deletion run/lib/stripe.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const Analytics = require('./analytics');
const db = require('./firebase');
const { enqueue } = require('./queue');
const models = require('../models');
const analytics = new Analytics();

Expand Down Expand Up @@ -72,12 +73,16 @@ const updateExplorerSubscription = async (stripeSubscription) => {

if (!stripePlan)
return 'Cannot find plan';

const stripeCustomer = await stripe.customers.retrieve(stripeSubscription.customer);

if (explorer.stripeSubscription) {
if (explorer.isDemo && !explorer.stripeSubscription.stripeId) {
await db.migrateDemoExplorer(explorer.id, user.id, stripeSubscription);
await enqueue('updateExplorerSyncingProcess', `updateExplorerSyncingProcess-${explorer.id}`, {
explorerSlug: explorer.slug, reset: true
});

analytics.track(user.id, 'explorer:demo_migrate', {
is_trial: stripeSubscription.status == 'trialing',
plan_slug: stripePlan.slug
Expand Down
4 changes: 3 additions & 1 deletion run/models/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = (sequelize, DataTypes) => {
}, {
hooks: {
async afterCreate(block, options) {
const workspace = await block.getWorkspace();
const workspace = await block.getWorkspace({ include: 'explorer' });
if (workspace.public) {
await enqueue('removeStalledBlock', `removeStalledBlock-${block.id}`, { blockId: block.id }, null, null, STALLED_BLOCK_REMOVAL_DELAY);
const afterCreateFn = async () => {
Expand All @@ -96,6 +96,8 @@ module.exports = (sequelize, DataTypes) => {
}
await bulkEnqueue('processTransactionTrace', jobs);
}
if (!workspace.integrityCheckStartBlockNumber && workspace.explorer && workspace.explorer.isDemo)
await workspace.update({ integrityCheckStartBlockNumber: block.number });
}
if (options.transaction)
return options.transaction.afterCommit(afterCreateFn);
Expand Down
16 changes: 16 additions & 0 deletions run/tests/jobs/updateExplorerSyncingProcess.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ beforeEach(() => jest.clearAllMocks());
const hasReachedTransactionQuota = jest.fn().mockResolvedValue(false);

describe('updateExplorerSyncingProcess', () => {
it('Should reset if flag is passed', (done) => {
const reset = jest.fn();
PM2.mockImplementationOnce(() => ({
reset,
find: jest.fn().mockResolvedValue({ data: { pm2_env: { status: 'online' }}})
}));
jest.spyOn(Explorer, 'findOne').mockResolvedValue({ slug: 'slug', workspaceId: 1 });

updateExplorerSyncingProcess({ data: { explorerSlug: 'explorer', reset: true }})
.then(res => {
expect(reset).toHaveBeenCalledWith('slug', 1);
expect(res).toEqual('Process reset.');
done();
});
});

it('Should delete if no explorer', (done) => {
PM2.mockImplementationOnce(() => ({
delete: jest.fn(),
Expand Down
25 changes: 24 additions & 1 deletion run/tests/lib/pm2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,30 @@ const PM2 = require('../../lib/pm2');
beforeEach(() => jest.clearAllMocks());

const host = 'http://pm2';
const secret= 'secret';
const secret = 'secret';

describe('reset', () => {
it('Should throw an error if missing parameter', (done) => {
const pm2 = new PM2(host, secret);
pm2.reset()
.catch(error => {
expect(error).toEqual(new Error('Missing parameter'));
done();
});
});

it('Should reset the process', (done) => {
jest.spyOn(axios, 'get').mockResolvedValueOnce({ data: null });

const pm2 = new PM2(host, secret);
pm2.reset('slug', 1)
.then(() => {
expect(axios.post).toHaveBeenCalledWith('http://pm2/processes/slug/delete?secret=secret');
expect(axios.post).toHaveBeenCalledWith('http://pm2/processes?secret=secret', { slug: 'slug', workspaceId: 1 });
done();
});
});
});

describe('restart', () => {
it('Should throw an error if missing parameter', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/MigrateExplorerModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</ul>
</v-card-text>
<div align="center" class="mb-4">
<v-btn :disabled="!finalized" color="primary" @click="goToOverview()">Continue</v-btn>
<v-btn :disabled="!finalized" color="primary" @click="goToExplorer()">Continue</v-btn>
</div>
</template>
<template v-else>
Expand Down Expand Up @@ -102,8 +102,8 @@ export default {
this.justMigrated = true;
this.finalized = true;
},
goToOverview() {
document.location.assign(`//app.${this.mainDomain}/overview`);
goToExplorer() {
document.location.assign(`//app.${this.mainDomain}/explorers/${this.explorerId}`);
},
close(refresh) {
this.resolve(refresh);
Expand Down

0 comments on commit feaa465

Please sign in to comment.