Scheduled actor option to rebuild before each run?

#1

I would like to create an actor with source in github. I have scheduled the actor to run once a day, however I noticed that it doesn’t sync with the latest source in github. Is there a way to schedule a build of the actor prior to each run?

I am aware of the git integration to rebuild on every commit, but that would eat up a lot of compute cycles building the actor every time something is committed. I would like it to build once prior to its scheduled run.

Thanks.

#2

Hi Sahir,

Lukas here from Apify.

We are working on adding webhooks to Actors which will allow us to compose workflows like yours. They should be in app in next months.

But until then, you will have to do this in your code. You just need call the build API endpoint at the beginning of each RUN, then wait for the build to finish, then call your Actor RUN once again with the same input but add for example ({built: true} to input so you know it is done and don’t end up in a loop.) and exit the current RUN.

These are the endpoints:
GET BUILDS
BUILD
RUN

The preferred way of calling the API is using the client instead because it automatically retries if the call fails. It maps the API endpoints, you can look to the docs

I hope this was not confusing. If you have trouble implementing this, let me know and I will show a code sample.

Best,
Lukas

#3

Thanks that makes sense.

I am creating a separate actor to build and run my actor.

What do you suggest is the best way to tell when the actor build is complete? From the docs I can see that the max wait time is 5 min, however my actor build may take longer than that. Is there any way to wait until it is complete so I can trigger a run and be sure that its using the latest version?

Thanks.

#4

I created an actor with the following:

const Apify = require(‘apify’)
const actId = “XXXX”

Apify.main(async () => {
    const apifyClient = Apify.client

    console.log("building act")
    let actBuild = await apifyClient.acts.buildAct({
        actId: actId,
        useCache: true,
        waitForFinish: 0,
        version: "0.0"
    })
    let buildId = actBuild.id
    
    console.log(actBuild)
    
    while (actBuild.status == "RUNNING") {
        console.log("Getting status of %s", buildId)
        actBuild = await apifyClient.acts.getBuild({
            buildId: buildId,
            waitForFinish: 60
        })
    }

    console.log("completed build")
    console.log(actBuild)
    console.log("running act")
    
    const actRun = await apifyClient.acts.runAct({
        actId: actId,
        waitForFinish: 0
    })
    
    console.log("kicked off act")
    console.log(actRun)
    
})

It seems however, that when the target act is triggered by this one, the default KV store is not there. I.e. the target app makes the following curl request which gets a 404:

curl -fsSLo input.json "https://api.apify.com/v2/key-value-stores/${APIFY_DEFAULT_KEY_VALUE_STORE_ID}/records/INPUT?disableRedirect=1"

Any ideas how to properly trigger another actor to run?

Thanks.

#5

Hi Sahir,

I’m sorry, I missed your post so I will reply at least now.

I just tried apifyClient.acts.runAct and it works fine. I think you expected to have the same default key value store in the called act, right? That is not possible since each actor run has its own dedicated kv store. If you want to do it this way, you can get the input from the key value store and pass it into the called one as a parameter in the runAct method.

The other option was as I presented before to do everything in one actor and just fork the build or run path at the begging. This way you have just one actor.