permaweb / ao-cookbook

Get cooking with ao - the decentralized computer with infinite threads 🐰 🕳️ 👈
https://cookbook_ao.arweave.dev
56 stars 60 forks source link

How could I realize `aos [myProcess] --cron 5-minutes` in sub-process‘ lua code when using ao.spawn #225

Open xgocn opened 1 week ago

xgocn commented 1 week ago
    local newProcess = ao.spawn(ao.env.Module.Id, {
      Authority = ao.id
    }).receive()

    ao.send({
      Target = newProcess.Process,
      Action = 'Eval',
      Data = [[
            Handlers.add(
                'CronTick', 
                Handlers.utils.hasMatchingTag('Action', 'Cron'), 
                 function(msg)
                    msg.reply({ Data = 'Pong'})
                end
            )
        ]]
    })

How could I realize aos [myProcess] --cron 5-minutes in sub-process‘ lua code when using ao.spawn

twilson63 commented 1 week ago
Spawn(ao.env.Module.Id, {
  Authority = ao.id,
  ["Cron-Tag-Action"] = "Cron",
  ["Cron-Interval"] = "5-minutes"
 })

You will have to use the MU REST endpoint to engage monitoring...

POST https://mu.ao-testnet.xyz/monitor/:processid

xgocn commented 1 week ago

When we define spawn, newprocess has not defined handler yet. Is cron already running? When we add handlers.add in the later part, it has business logic. Before that, is cron executing an empty loop?

twilson63 commented 1 week ago

Yes cron calculates implicit messages based on interval, if you don't handle them, they will go into your inbox

On Tue, Sep 10, 2024 at 6:10 PM farmer @.***> wrote:

When we define spawn, newprocess has not defined handler yet. Is cron already running? When we add handlers.add in the later part, it has business logic. Before that, is cron executing an empty loop?

— Reply to this email directly, view it on GitHub https://github.com/permaweb/ao-cookbook/issues/225#issuecomment-2342114141, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAFGLGN2CAQFYBNSKFWGF3ZV5U4RAVCNFSM6AAAAABN55L7QOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBSGEYTIMJUGE . You are receiving this because you commented.Message ID: @.***>

xgocn commented 1 week ago

I just ran curl -X POST https://mu.ao-testnet.xyz/monitor/b3WAvKqYrdsLBtGkNVPD5RpeJERy1WiCA3LBbf6LmPI

got {"error":"this.binary.subarray is not a function"}

xgocn commented 1 week ago

I believe the newprocess is running,because I send a Ping and got a Pong

xgocn commented 1 week ago

this is my code

    -- parent process : l7ph6gka-mmrVE5P23lRa-1BWgQC8MXH8FcTbs6wk_U 
    Handlers.add(
    'ping',
    Handlers.utils.hasMatchingTag('Action', 'Ping'),
    function(msg)
        print("Received Ping from: " .. msg.From)
        msg.reply({ Data = 'Pong from Main Process:' .. tostring(ao.id)})
    end
   )

    local newProcess = ao.spawn(ao.env.Module.Id, {
        Authority = ao.id,
        ["Cron-Tag-Action"] = "Cron",
        ["Cron-Interval"] = "10-seconds"
    }).receive()

    print(newProcess.Process)
   -- got jSj0KUIDJN8gx8PW5-MLZ_gTAaY6_EQd0N3Kl6AT6YY

    DB:exec(string.format("UPDATE npc SET proc_id = '%s' WHERE id = %d", newProcess.Process,npcId))

    ao.send({
      Target = newProcess.Process,
      Action = 'Eval',
      Data = [[
            Handlers.add(
                'ping',
                Handlers.utils.hasMatchingTag('Action', 'Ping'),
                function(msg)
                    ao.send({Target = 'l7ph6gka-mmrVE5P23lRa-1BWgQC8MXH8FcTbs6wk_U',Action = 'Ping'})
                    msg.reply({ Data = 'Pong'})
                end
            )

            Handlers.add(
                'CronTick', 
                Handlers.utils.hasMatchingTag('Action', 'Cron'), 
                function(msg)
                    ao.send({Target ='l7ph6gka-mmrVE5P23lRa-1BWgQC8MXH8FcTbs6wk_U',Action = 'Ping'})
                    msg.reply({ Data = "NPC has died." })
                end
            )
        ]]
    })

I make test like:

SUS@aos-2.0.0[Inbox:90]> Send({Target="jSj0KUIDJN8gx8PW5-MLZ_gTAaY6_EQd0N3Kl6AT6YY",Action="Ping"})
{
   onReply = function: 0x227dc60,
   output = "Message added to outbox",
   receive = function: 0x227dd00
}
Received Ping from: jSj0KUIDJN8gx8PW5-MLZ_gTAaY6_EQd0N3Kl6AT6YY

but the cron part didn't work,so I make a futher test: I call Cron as a common handler,it responed

SUS@aos-2.0.0[Inbox:91]> Send({Target="jSj0KUIDJN8gx8PW5-MLZ_gTAaY6_EQd0N3Kl6AT6YY",Action="Cron"})
{
   onReply = function: 0x227d9e0,
   output = "Message added to outbox",
   receive = function: 0x227d840
}
Received Ping from: jSj0KUIDJN8gx8PW5-MLZ_gTAaY6_EQd0N3Kl6AT6YY

by far,I consider the handler's code is correct and the cron works too,but can't ao.send a Ping under Cron mode

what could the problem be?

xgocn commented 1 week ago

who is the singer of a cron handler in sub-process,maybe no one?

twilson63 commented 1 week ago

singer?

If you mean signer, cron messages are generated implicitly.

Thx

Tom

On Wed, Sep 11, 2024 at 9:41 AM farmer @.***> wrote:

who is the singer of a cron handler,maybe no one?

— Reply to this email directly, view it on GitHub https://github.com/permaweb/ao-cookbook/issues/225#issuecomment-2343710824, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAFGLCIJPJFO7EO66LSVKTZWBCBLAVCNFSM6AAAAABN55L7QOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBTG4YTAOBSGQ . You are receiving this because you commented.Message ID: @.***>

xgocn commented 1 week ago

yes, signer,if cron messages are generated implicitly,the

Handlers.add(
                'CronTick', 
                Handlers.utils.hasMatchingTag('Action', 'Cron'), 
                function(msg)
                    -- here
                    ao.send({Target =parentProcess,Action = 'Ping'})
                    -- 
                    msg.reply({ Data = "NPC has died." })
                end
            )

the ao.send didn‘t work under implicit mode in my test, BUT works when called as a standard handler like SUS@aos-2.0.0[Inbox:91]> Send({Target=:NEWPROCESSID,Action="Cron"})

twilson63 commented 1 week ago

do you have a test script you can share?

On Wed, Sep 11, 2024 at 5:59 PM farmer @.***> wrote:

yes, signer,if cron messages are generated implicitly,the

Handlers.add( 'CronTick', Handlers.utils.hasMatchingTag('Action', 'Cron'), function(msg) ao.send({Target ='l7ph6gka-mmrVE5P23lRa-1BWgQC8MXH8FcTbs6wk_U',Action = 'Ping'}) msg.reply({ Data = "NPC has died." }) end )

the ao.send didn‘t work in my test

— Reply to this email directly, view it on GitHub https://github.com/permaweb/ao-cookbook/issues/225#issuecomment-2344776266, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAFGLGYKAVF3BLJFDVUSM3ZWC4LPAVCNFSM6AAAAABN55L7QOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBUG43TMMRWGY . You are receiving this because you commented.Message ID: @.***>

xgocn commented 6 days ago

test.zip

the ao.send in Cron doesn't work for unknown reason

xgocn commented 6 days ago

And I‘m not the only one who met the problem

twilson63 commented 5 days ago

Sorry, traveling, I will review your code as soon as possible.

Thanks

Tom

On Thu, Sep 12, 2024 at 11:35 PM farmer @.***> wrote:

And I‘m not the only one who met the problem

— Reply to this email directly, view it on GitHub https://github.com/permaweb/ao-cookbook/issues/225#issuecomment-2348015068, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAFGLD23CLNDNA7GNZNHS3ZWJTQ7AVCNFSM6AAAAABN55L7QOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBYGAYTKMBWHA . You are receiving this because you commented.Message ID: @.***>

xgocn commented 5 days ago

Have a good time!

antaintan commented 3 days ago

I had the same problem

windTrace commented 1 day ago

yes, bug exists when call ao.send in Cron of spawned sub-process