Scheduling redeployment based on external API response
larssont
HOBBYOP

4 months ago

I have a service running an Astro site which features a blog.

The blog may contain content that is scheduled for publication at a future date at a specific time, and I need a way to redeploy the service so that content is built and served.

My plan to make this work in railway was to have an endpoint served by astro such as /api/build.json that responds with something like:

{next: "some-datetime" }

The date would match the closest upcoming content publication date from the blog collection.

Then I could have cron job in railway running every 5 minutes, and if now > next, then trigger a rebuild. A rebuild will also update the content served by the endpoint, since it's static.

  1. Is this a reasonable approach? I want to avoid triggering unnecessary rebuilds, and a normal cron job that rebuilds the service daily is both inaccurate and resource demanding.
  2. Is there a simple way to trigger a rebuild from a railway function? I figured running a railway function that called the endpoint and optionally triggered a rebuild would be the way to go, but it seems like it's not as straight forward as I anticipated.
  3. Do I need to go through the Public API, or are there any better alternative solutions for triggering a rebuild based on the date provided by the API?

9 Replies

4 months ago

Curious as to how you are publishing content, is it like a date check in the build process that skips content if the target date isn't met?


larssont
HOBBYOP

4 months ago

Yepp pretty much.

This is an example for any given blog post.

---
import ExternalLink from "@components/ExternalLink.astro";
import { getAllPosts } from "@js/blogUtils";
import PostLayout from "@layouts/PostLayout.astro";
import { type InferGetStaticPropsType } from "astro";
import { render } from "astro:content";

export async function getStaticPaths() {
    const posts = await getAllPosts({
        includeDraft: false,
        includeFuture: false,
    });

    return posts.map((post) => ({
        params: { slug: post.id },
        props: post,
    }));
}
type Props = InferGetStaticPropsType;

const post = Astro.props as Props;
const { Content, headings } = await render(post);
---


    <content>
</content>

getAllPosts(...) gets posts from the content collection API of Astro, and filters out draft and unpublished stuff based on the given parameters.


4 months ago

Astro is either rebuild or SSR

You can use the Railway API to trigger a redeployment

https://railway.com/graphiql



larssont
HOBBYOP

4 months ago

I know, I was mostly concerned with how I would handle the Railway part. If I was forced to use the Railway API via graphql, or if there was easier way to deal with it (for instance, via railway functions).


larssont
HOBBYOP

4 months ago

Interesting, I'll take a look.


4 months ago

You could use a Railway function to keep it simple and within the private network

But there is no other way to trigger a redeployment afaik


larssont
HOBBYOP

4 months ago

Alright, fair enough. Thanks!

Cool website BTW :)


4 months ago

This would be very easy to do with that SDK no doubt about that, but my take is that you should find a way to do this that is completely platform agnostic.


Welcome!

Sign in to your Railway account to join the conversation.

Loading...