Skip to content

Commit

Permalink
generic explirer removal jjob (#398)
Browse files Browse the repository at this point in the history
* generic explirer removal jjob

* fix bug

* fix tests

---------

Co-authored-by: Antoine de Chevigné <antoine@tryethernal.com>
  • Loading branch information
antoinedc and Antoine de Chevigné authored Dec 12, 2024
1 parent 097d3a6 commit 3c2df6e
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 44 deletions.
31 changes: 0 additions & 31 deletions run/jobs/cancelDemoExplorers.js

This file was deleted.

2 changes: 1 addition & 1 deletion run/jobs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
workspaceReset: require('./workspaceReset'),
batchBlockDelete: require('./batchBlockDelete'),
batchContractDelete: require('./batchContractDelete'),
cancelDemoExplorers: require('./cancelDemoExplorers'),
removeExpiredExplorers: require('./removeExpiredExplorers'),
deleteWorkspace: require('./deleteWorkspace'),
tokenTransferCleanup: require('./tokenTransferCleanup'),
queueMonitoring: require('./queueMonitoring'),
Expand Down
52 changes: 52 additions & 0 deletions run/jobs/removeExpiredExplorers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { Op } = require('sequelize');
const { Explorer, StripeSubscription, StripePlan, Workspace } = require('../models');
const { enqueue } = require('../lib/queue');

module.exports = async () => {
const explorers = (await Explorer.findAll({
include: [
{
model: StripeSubscription,
as: 'stripeSubscription',
include: {
model: StripePlan,
as: 'stripePlan',
where: {
capabilities: {
expiresAfter: {
[Op.not]: null
}
}
}
}
},
{
model: Workspace,
as: 'workspace'
}
]
})).filter(e => !!e.stripeSubscription);

const deleted = [];
for (let i = 0; i < explorers.length; i++) {
const explorer = explorers[i];
const expiresAfterDays = explorer.stripeSubscription.stripePlan.capabilities.expiresAfter;
const expirationDate = new Date(explorer.createdAt);
expirationDate.setDate(expirationDate.getDate() + expiresAfterDays);

const daysDiff = Math.floor((expirationDate - new Date()) / (1000 * 60 * 60 * 24));

if (daysDiff <= 0) {
await explorer.workspace.update({ pendingDeletion: true, public: false });
await explorer.safeDelete({ deleteSubscription: true });
await enqueue('workspaceReset', `workspaceReset-${explorer.workspaceId}`, {
workspaceId: explorer.workspaceId,
from: new Date(0),
to: new Date()
});
await enqueue('deleteWorkspace', `deleteWorkspace-${explorer.workspaceId}`, { workspaceId: explorer.workspaceId });
deleted.push(explorer.slug);
}
}
return deleted;
};
12 changes: 10 additions & 2 deletions run/models/explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,19 @@ module.exports = (sequelize, DataTypes) => {
return subscription && subscription.stripePlan.capabilities[capability];
}

async safeDelete() {
async safeDelete(opts = { deleteSubscription: false }) {
const stripeSubscription = await this.getStripeSubscription();
if (!stripeSubscription || stripeSubscription && stripeSubscription.isPendingCancelation || !isStripeEnabled()) {
if (
!stripeSubscription ||
stripeSubscription && stripeSubscription.isPendingCancelation ||
stripeSubscription && opts.deleteSubscription ||
!isStripeEnabled()
) {
const transaction = await sequelize.transaction();
try {
if (stripeSubscription && opts.deleteSubscription)
await stripeSubscription.destroy({ transaction });

const domains = await this.getDomains();
for (let i = 0; i < domains.length; i++)
await domains[i].destroy({ transaction });
Expand Down
4 changes: 2 additions & 2 deletions run/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const BLOCK_SYNC_MONITORING_INTERVAL = 60 * 1000;

(async () => {
await enqueue(
'cancelDemoExplorers',
'cancelDemoExplorers',
'removeExpiredExplorers',
'removeExpiredExplorers',
{},
10,
{ every: CANCEL_DEMO_INTERVAL }
Expand Down
2 changes: 1 addition & 1 deletion run/tests/jobs/deleteWorkspace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ beforeEach(() => jest.clearAllMocks());

jest.spyOn(env, 'getMaxBlockForSyncReset').mockReturnValue(2);

describe('cancelDemoExplorers', () => {
describe('deleteWorkspace', () => {
it('Should return if cannot find workspace', (done) => {
jest.spyOn(Workspace, 'findByPk').mockResolvedValueOnce(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ require('../mocks/lib/queue');
const { Explorer } = require('../mocks/models');
const { enqueue } = require('../../lib/queue');

const cancelDemoExplorers = require('../../jobs/cancelDemoExplorers');
const removeExpiredExplorers = require('../../jobs/removeExpiredExplorers');

beforeEach(() => jest.clearAllMocks());

const safeDeleteSubscription = jest.fn();
const safeDelete = jest.fn();
const update = jest.fn();

describe('cancelDemoExplorers', () => {
describe('removeExpiredExplorers', () => {
it('Should delete subscription, explorer, update workspace & enqueue ws reset & deletion', (done) => {
jest.useFakeTimers()
.setSystemTime(new Date('2023-12-26'));
Expand All @@ -19,18 +19,35 @@ describe('cancelDemoExplorers', () => {
{
workspaceId: 1,
slug: 'slug',
stripeSubscription: { stripeId: '123' },
stripeSubscription: { stripeId: '123', stripePlan: { capabilities: { expiresAfter: 7 }}},
createdAt: new Date('2023-11-26'),
safeDeleteSubscription,
safeDelete,
workspace: { update }
},
{
workspaceId: 1,
slug: 'slug',
stripeSubscription: { stripeId: '123', stripePlan: { capabilities: { expiresAfter: 7 }}},
createdAt: new Date('2023-12-25'),
safeDeleteSubscription,
safeDelete,
workspace: { update }
},
{
workspaceId: 1,
slug: 'slug',
stripeSubscription: { stripeId: '123', stripePlan: { capabilities: {} }},
safeDeleteSubscription,
safeDelete,
workspace: { update }
}
]);

cancelDemoExplorers()
removeExpiredExplorers()
.then(res => {
expect(safeDeleteSubscription).toHaveBeenCalledWith();
expect(safeDelete).toHaveBeenCalled();
expect(update).toHaveBeenCalledWith({ pendingDeletion: true, public: false });
expect(safeDelete).toHaveBeenNthCalledWith(1, { deleteSubscription: true });
expect(enqueue).toHaveBeenCalledWith('workspaceReset', 'workspaceReset-1', {
workspaceId: 1,
from: new Date(0),
Expand Down
2 changes: 1 addition & 1 deletion run/workers/priorities.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"workspaceReset",
"batchBlockDelete",
"batchContractDelete",
"cancelDemoExplorers",
"removeExpiredExplorers",
"deleteWorkspace",
"tokenTransferCleanup",
"removeStalledBlock",
Expand Down

0 comments on commit 3c2df6e

Please sign in to comment.