mirror of
https://github.com/strapi/strapi.git
synced 2025-10-27 08:02:56 +00:00
Register event on startup
This commit is contained in:
parent
1166d80332
commit
a57354677d
@ -8,6 +8,7 @@ const { createQuery } = require('./queries');
|
|||||||
class DatabaseManager {
|
class DatabaseManager {
|
||||||
constructor(strapi) {
|
constructor(strapi) {
|
||||||
this.strapi = strapi;
|
this.strapi = strapi;
|
||||||
|
this.eventHub = strapi.eventHub;
|
||||||
|
|
||||||
this.initialized = false;
|
this.initialized = false;
|
||||||
|
|
||||||
@ -87,7 +88,12 @@ class DatabaseManager {
|
|||||||
.get(model.orm)
|
.get(model.orm)
|
||||||
.queries({ model, modelKey: normalizedName, strapi });
|
.queries({ model, modelKey: normalizedName, strapi });
|
||||||
|
|
||||||
const query = createQuery({ connectorQuery, model });
|
const query = createQuery({
|
||||||
|
connectorQuery,
|
||||||
|
model,
|
||||||
|
eventHub: this.eventHub,
|
||||||
|
});
|
||||||
|
|
||||||
this.queries.set(model.uid, query);
|
this.queries.set(model.uid, query);
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports = function createQuery({ connectorQuery, model }) {
|
module.exports = function createQuery(opts) {
|
||||||
return new Query(connectorQuery, model);
|
return new Query(opts);
|
||||||
};
|
};
|
||||||
|
|
||||||
class Query {
|
class Query {
|
||||||
constructor(connectorQuery, model) {
|
constructor({ model, connectorQuery, eventHub }) {
|
||||||
|
this.eventHub = eventHub;
|
||||||
this.connectorQuery = connectorQuery;
|
this.connectorQuery = connectorQuery;
|
||||||
this.model = model;
|
this.model = model;
|
||||||
}
|
}
|
||||||
@ -52,15 +53,21 @@ class Query {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async create(...args) {
|
async create(...args) {
|
||||||
return this.connectorQuery.create(...args);
|
const entry = await this.connectorQuery.create(...args);
|
||||||
|
this.eventHub.emit('entry.create', entry);
|
||||||
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(...args) {
|
async update(...args) {
|
||||||
return this.connectorQuery.update(...args);
|
const entry = await this.connectorQuery.update(...args);
|
||||||
|
this.eventHub.emit('entry.update', entry);
|
||||||
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(...args) {
|
async delete(...args) {
|
||||||
return this.connectorQuery.delete(...args);
|
const entry = await this.connectorQuery.delete(...args);
|
||||||
|
this.eventHub.emit('entry.delete', entry);
|
||||||
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
async count(...args) {
|
async count(...args) {
|
||||||
|
|||||||
@ -86,6 +86,8 @@ module.exports = {
|
|||||||
if (file.tmpPath) {
|
if (file.tmpPath) {
|
||||||
fs.unlinkSync(file.tmpPath);
|
fs.unlinkSync(file.tmpPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
strapi.eventHub.emit('media.create', res);
|
||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -126,6 +128,12 @@ module.exports = {
|
|||||||
await actions.delete(file);
|
await actions.delete(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const media = strapi.query('file', 'upload').findOne({
|
||||||
|
id: file.id,
|
||||||
|
});
|
||||||
|
|
||||||
|
strapi.eventHub.emit('media.delete', media);
|
||||||
|
|
||||||
return strapi.query('file', 'upload').delete({ id: file.id });
|
return strapi.query('file', 'upload').delete({ id: file.id });
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@ -90,7 +90,10 @@ class Strapi extends EventEmitter {
|
|||||||
// internal services.
|
// internal services.
|
||||||
this.fs = createStrapiFs(this);
|
this.fs = createStrapiFs(this);
|
||||||
this.eventHub = createEventHub();
|
this.eventHub = createEventHub();
|
||||||
this.webhookRunner = createWebhookRunner({ eventHub: this.eventHub });
|
this.webhookRunner = createWebhookRunner({
|
||||||
|
eventHub: this.eventHub,
|
||||||
|
logger: this.log,
|
||||||
|
});
|
||||||
|
|
||||||
this.initConfig(opts);
|
this.initConfig(opts);
|
||||||
this.requireProjectBootstrap();
|
this.requireProjectBootstrap();
|
||||||
@ -377,11 +380,18 @@ class Strapi extends EventEmitter {
|
|||||||
|
|
||||||
this.webhookStore = createWebhookStore({ db: this.db });
|
this.webhookStore = createWebhookStore({ db: this.db });
|
||||||
|
|
||||||
|
await this.startWebhooks();
|
||||||
|
|
||||||
// Initialize hooks and middlewares.
|
// Initialize hooks and middlewares.
|
||||||
await initializeMiddlewares.call(this);
|
await initializeMiddlewares.call(this);
|
||||||
await initializeHooks.call(this);
|
await initializeHooks.call(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async startWebhooks() {
|
||||||
|
const webhooks = await this.webhookStore.findWebhooks();
|
||||||
|
this.webhookRunner.register(webhooks);
|
||||||
|
}
|
||||||
|
|
||||||
reload() {
|
reload() {
|
||||||
const state = {
|
const state = {
|
||||||
shouldReload: 0,
|
shouldReload: 0,
|
||||||
|
|||||||
@ -6,8 +6,9 @@
|
|||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
|
|
||||||
class WebhookRunner {
|
class WebhookRunner {
|
||||||
constructor({ eventHub }) {
|
constructor({ eventHub, logger }) {
|
||||||
this.eventHub = eventHub;
|
this.eventHub = eventHub;
|
||||||
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
run(webhook, event, info = {}) {
|
run(webhook, event, info = {}) {
|
||||||
@ -25,20 +26,23 @@ class WebhookRunner {
|
|||||||
'X-Strapi-Event': event,
|
'X-Strapi-Event': event,
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
|
timeout: 10000,
|
||||||
})
|
})
|
||||||
.then(res => {
|
.then(res => {
|
||||||
console.log(res.status);
|
this.logger.info(res.status);
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.log('Error', err);
|
this.logger.error('Error', err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
register(webhook) {
|
register(webhooks) {
|
||||||
const { events } = webhook;
|
webhooks.forEach(webhook => {
|
||||||
|
const { events } = webhook;
|
||||||
|
|
||||||
events.forEach(event => {
|
events.forEach(event => {
|
||||||
this.eventHub.on(event, this.run.bind(this, webhook, event));
|
this.eventHub.on(event, info => this.run(webhook, event, info));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user