| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { administers, organizationRoleOf |
| 3 | import { |
| 4 | Inject, |
| 5 | Injectable, |
| 6 | Logger, |
| 7 | type OnApplicationBootstrap, |
| 8 | type OnApplicationShutdown, |
| 9 | } from '@nestjs/common'; |
| 10 | import { and, eq |
| 11 | |
| 12 | import { DATABASE } |
| 13 | import { repositoryFullNameOf } |
| 14 | import { MailService } |
| 15 | import { errorMessage } |
| 16 | |
| 17 | const SWEEP_INTERVAL_MS = 60 |
| 18 | |
| 19 | /** Runs in the API rather than apps/delivery because it emails people, and delivery never reads users. */ |
| 20 | @Injectable() |
| 21 | export class WebhookBreakerService |
| 22 | implements OnApplicationBootstrap |
| 23 | { |
| 24 | private readonly logger |
| 25 | private timer?: |
| 26 | |
| 27 | constructor( |
| 28 | @Inject(DATABASE) |
| 29 | private readonly mail |
| 30 | ) {} |
| 31 | |
| 32 | onApplicationBootstrap() { |
| 33 | this.timer = |
| 34 | this.sweep().catch |
| 35 | this.logger.error |
| 36 | ); |
| 37 | }, SWEEP_INTERVAL_MS); |
| 38 | } |
| 39 | |
| 40 | onApplicationShutdown() { |
| 41 | clearInterval(this.timer |
| 42 | } |
| 43 | |
| 44 | async sweep() { |
| 45 | await this.disableFailingEndpoints |
| 46 | await this.notifyDisabled |
| 47 | await this.pruneOutbox |
| 48 | } |
| 49 | |
| 50 | /** Off once a delivery gave up (three days of retries) since the owner last changed the endpoint, with nothing delivered in those three days. */ |
| 51 | async disableFailingEndpoints() { |
| 52 | return this.db |
| 53 | .update(schema.webhookEndpoint |
| 54 | .set({ |
| 55 | active: false, |
| 56 | disabledReason: 'Every delivery failed for three days.', |
| 57 | }) |
| 58 | .where( |
| 59 | and( |
| 60 | eq(schema.webhookEndpoint |
| 61 | sql`exists ( |
| 62 | select 1 from ${schema.deliveryJob} j |
| 63 | where j.endpoint_id = ${schema.webhookEndpoint.id |
| 64 | and j.status = 'dead' |
| 65 | and j.updated_at > ${schema.webhookEndpoint.updatedAt |
| 66 | )`, |
| 67 | sql`not exists ( |
| 68 | select 1 from ${schema.deliveryJob} j |
| 69 | where j.endpoint_id = ${schema.webhookEndpoint.id |
| 70 | and j.status = 'succeeded' |
| 71 | and j.updated_at > now() - interval '3 days' |
| 72 | )`, |
| 73 | ), |
| 74 | ) |
| 75 | .returning({ id: |
| 76 | } |
| 77 | |
| 78 | /** Emails the admins of endpoints Ghost turned off, here or in apps/delivery on a 410. Claiming the row before sending means one email however many API instances sweep. */ |
| 79 | async notifyDisabled() { |
| 80 | const disabled = |
| 81 | .update(schema.webhookEndpoint |
| 82 | .set({ disabledNotifiedAt: |
| 83 | .where( |
| 84 | and( |
| 85 | eq(schema.webhookEndpoint |
| 86 | isNotNull(schema.webhookEndpoint |
| 87 | isNull(schema.webhookEndpoint |
| 88 | ), |
| 89 | ) |
| 90 | .returning({ |
| 91 | id: schema.webhookEndpoint |
| 92 | url: schema.webhookEndpoint |
| 93 | reason: sql<string |
| 94 | ownerId: sql<string |
| 95 | }); |
| 96 | if (disabled.length |
| 97 | |
| 98 | const ownerIds = |
| 99 | // ponytail: a user's repository emails its owner only, not collaborators with admin; add them if owners turn out not to be the ones who set webhooks up. |
| 100 | const [repositoryOwners, |
| 101 | this.db |
| 102 | .select({ |
| 103 | ownerId: schema.repository |
| 104 | owner: repositoryFullNameOf( |
| 105 | schema.user, |
| 106 | schema.organization, |
| 107 | schema.repository, |
| 108 | ), |
| 109 | name: schema.user |
| 110 | email: schema.user |
| 111 | }) |
| 112 | .from(schema.repository |
| 113 | .innerJoin( |
| 114 | schema.user, |
| 115 | and( |
| 116 | eq(schema.user |
| 117 | eq(schema.user |
| 118 | ), |
| 119 | ) |
| 120 | .leftJoin( |
| 121 | schema.organization, |
| 122 | eq(schema.organization |
| 123 | ) |
| 124 | .where(inArray(schema |
| 125 | this.db |
| 126 | .select({ |
| 127 | ownerId: schema.organization |
| 128 | owner: schema.organization |
| 129 | name: schema.user |
| 130 | email: schema.user |
| 131 | role: schema.member |
| 132 | }) |
| 133 | .from(schema.organization |
| 134 | .innerJoin( |
| 135 | schema.member, |
| 136 | eq(schema.member |
| 137 | ) |
| 138 | .innerJoin( |
| 139 | schema.user, |
| 140 | and( |
| 141 | eq(schema.user |
| 142 | eq(schema.user |
| 143 | ), |
| 144 | ) |
| 145 | .where(inArray(schema |
| 146 | ]); |
| 147 | const recipients = |
| 148 | ...repositoryOwners, |
| 149 | ...organizationMembers.filter |
| 150 | administers(organizationRoleOf(member |
| 151 | ), |
| 152 | ]; |
| 153 | |
| 154 | for (const endpoint |
| 155 | for (const recipient |
| 156 | if (recipient.ownerId |
| 157 | await this.mail |
| 158 | .sendWebhookDisabledEmail(recipient.email |
| 159 | name: recipient.name |
| 160 | owner: recipient.owner |
| 161 | url: endpoint.url |
| 162 | reason: endpoint.reason |
| 163 | webhookId: endpoint.id |
| 164 | }) |
| 165 | .catch((error: |
| 166 | this.logger.warn |
| 167 | `Emailing ${recipient.email |
| 168 | ), |
| 169 | ); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** Handled events are history once every consumer has run; nothing reads them after a month. */ |
| 175 | async pruneOutbox() { |
| 176 | await this.db |
| 177 | .delete(schema.outboxEvent |
| 178 | .where( |
| 179 | and( |
| 180 | isNotNull(schema.outboxEvent |
| 181 | lt(schema.outboxEvent |
| 182 | ), |
| 183 | ); |
| 184 | } |
| 185 | } |