vendure-ecommerce / vendure

The commerce platform with customization in its DNA.
https://www.vendure.io
Other
5.75k stars 1.02k forks source link

Allow catalogue to be re-indexed periodically #1399

Open flowfalls opened 2 years ago

flowfalls commented 2 years ago

Is your feature request related to a problem? Please describe. Please add functionality to run the reindex() periodically. I need to have my customers know up-to-date information in regards to stock-levels.

Describe the solution you'd like I'd like it to easily run the reindex() job; just as we do with the admin-ui button but removing the manual process.

Describe alternatives you've considered I've had to use axios to login to the backend and make a graphql post in order to run the reindex via a cron job.

Additional context current rudimentary solution:

 @Cron(CronExpression.EVERY_10_SECONDS)
    async routineReindexing() {
        if (this.processContext.isWorker) {

            const jar = new CookieJar();
            const client = wrapper(axios.create({jar} as AxiosRequestConfig));

            await client({
                url: 'http://localhost:3000/admin-api',
                method: 'post',
                data: {
                    operationName: "AttemptLogin",
                    variables: {
                        "username": config.authOptions.superadminCredentials?.identifier,
                        "password": config.authOptions.superadminCredentials?.password,
                        "rememberMe": false
                    },
                    query: "mutation AttemptLogin($username: String!, $password: String!, $rememberMe: Boolean!) {\n  login(username: $username, password: $password, rememberMe: $rememberMe) {\n    ...CurrentUser\n    ...ErrorResult\n    __typename\n  }\n}\n\nfragment CurrentUser on CurrentUser {\n  id\n  identifier\n  channels {\n    id\n    code\n    token\n    permissions\n    __typename\n  }\n  __typename\n}\n\nfragment ErrorResult on ErrorResult {\n  errorCode\n  message\n  __typename\n}\n"

                }
            }).catch((reason => console.log(reason)));

            await client({
                url: 'http://localhost:3000/admin-api',
                method: 'post',
                withCredentials: true,
                data: {
                    query: `
                      mutation Reindex {
                          reindex {
                            id
                          }
                        }
                      `,
                },
            }).then((result) => {
                console.log(result.data)
            }).catch((reason => console.log(reason)));
        }
michaelbromley commented 2 years ago

Relates to #1425 - the same mechanism discussed in that issue would be used to handle this use-case too.